Strip Comments Off Strings Returned by GetPrivateProfileString

Strip Comments Off Strings Returned by GetPrivateProfileString

Windows doesn’t treat comments in INI files the same way VB does in code. Typically, comments must be on a single line of their own. When calling the GetPrivateProfileString function, if the requested entry contains a Rem statement-such as “; this is a rem’d statement”-the entire entry, value and comment, will be returned.

If your code anticipates the entry without comment, confusion could result from a user entering a comment following the entry rather than on a separate line. Use this VB code example to properly receive a return using the GetPrivateProfileString function call under any circumstance:

 Private Declare Function GetPrivateProfileString _	Lib "kernel32" Alias _	"GetPrivateProfileStringA" (ByVal _	lpApplicationName As Any, ByVal lpKeyName As _	Any, ByVal lpDefault As String, ByVal _	lpReturnedString As String, ByVal nSize As _	Long, ByVal lpFileName As String) As LongPrivate Sub Form_Click()	Dim IniString As String	Dim sDefault As String	Dim lReturn As Long	sDefault = "n/a"	' allocate sufficient buffer	IniString = String$(260, 0)	lReturn = GetPrivateProfileString("DB", _		"Path", sDefault, IniString, _		Len(IniString), "c:	est.ini")	If lReturn > 0 Then		IniString = Left$(IniString, lReturn)		Debug.Print IniString		' added to strip out trailing comments		If InStr(IniString, ";") > 0 Then			IniString = Trim$(Left$(IniString, _				InStr(IniString, ";") - 1))			If InStr(IniString, vbTab) > 0 Then				IniString = Trim$(Left$(IniString, _					InStr(IniString, vbTab) - 1))			End If		End If	Else		IniString = sDefault	End If	Debug.Print IniStringEnd Sub
See also  Why ChatGPT Is So Important Today

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist