Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading INI file problem...

Status
Not open for further replies.

Fursten

Programmer
Dec 27, 2000
403
PT
Hi,

I´m reading some information from a INI file. I´m using this declare:

Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Then I make:

Dim lcl_strini As String
Dim nc As Long

nc = GetPrivateProfileString("est", "DataSource", "Default", lcl_strini, 255, App.Path & "\initapp.ini")


The problem is that when I use it, my vb crashes! To solve the problem, I needed to define the lcl_strini variable with a fixed length! Like: Dim lcl_strini as string * 100 (for instance)

Why does I need to declare a variable in that way? Why can´t it be a nor mal string?

Then, another problem that arises is that my return string will have a lot of junk inside it... How can I take it away?

Thank you in advance,

Sergio Oliveira
 
You can use a normal nonfixed length string, but the API call does expect a buffer in which to insert its results. Almost all APIs dealing with strings work this way. If you use a normal string, then you need to allocate space for the answer using the Space$ command. For example . . .


dim strData as string


strData = Space$(1024)
nc = GetPrivateProfileStrin("est", "DataSource", "Default", strData , 255, App.Path & "\initapp.ini")

As for the junk you see after the answer, that is because the API retuns the entire buffer it was given even if the space was not used. The API also returns the length of the "real" answer. You can trim the garbage off by using th left$ command with the number returned by the API. - Jeff Marler B-)
 
Save yourself some headaches in the future. Either copy or develop an INI class. FAQ222-1198 has a start.

Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top