Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Intermediate
Aug 26, 1999

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 Long

Private 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:\test.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 IniString
End Sub
R. Van
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date