INI file API call

jonatec
jonatec used Ask the Experts™
on
Anybody managed to get API GetPrivateProfileSection to work?

I just keep getting an empty lpReturnedString argument. Tried the obvious like correct INI path etc, Return long is 0 also.

Thanks, Ken.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Ken,

After setting up the variables, you need to set the return variable as below and then call the GetPrivateProfileString.  Once returned, you'll test the return value from the function and either you have something that matches, or not.

Dim sInibuffer As String * 255
Dim nRtn As Long
Dim IniSection$, IniPrompt$, IniFileName$
Dim sIniValue$

IniSection = "main"
IniPrompt = "path"
IniFileName = "c:\Project.Ini"

sInibuffer = String$(255, 0)
nRtn = GetPrivateProfileString(IniSection, IniPrompt, "", sInibuffer, 255, IniFileName)
If nRtn <> 0 Then
   sIniValue = Left$(sInibuffer, nRtn)
Else
   sIniValue = ""
End If

Where your .INI file looks like:

[main]
path=c:\somepath\path2\path3

or something like this...



Hope this helps, let me know if you need more,

Sweat
Commented:
Jon,

I have used it before here is a small sample of my code.  Remember that it returns everything in the 'section' so there are a few ways to parse out all the different values.

Public Sub ReadIni()

    Dim lResult As Long
    Dim Ret As String: Ret = Space(255)
    Dim x As Integer 'To count
    Dim UpdateSecond(25) as String

    lResult = GetPrivateProfileSection("Second Updates", Ret, Len(Ret), App.Path & "\MUM.ini")

    If lResult Then
        UpdateSecond() = Split(Ret, Chr$(0), , vbTextCompare)
       
        Do Until UpdateSecond(x) = ""
            UpdateSecond(x) = Mid(UpdateSecond(x), InStr(1, UpdateSecond(x), "=", vbTextCompare) + 1)
            x = x + 1
        Loop
       
    End If

End Sub

Hope that I understood your question properly.
G

Commented:
The way shown below works great for me. The important bit is the 'true' as the last parameter of the function call, otherwise it'll try to read from the registry. Remember that the call returns an array, so you'll have to accomodate that (varTemp in my case).

Hope it works for you


'Source:http://www.aboutvb.de/khw/artikel/khwini.htm
Private Declare Function GetPrivateProfileSection _
    Lib "kernel32" _
    Alias "GetPrivateProfileSectionA" _
    (ByVal Section As String, _
    ByVal Buffer As String, _
    ByVal Size As Long, _
    ByVal FileName As String) _
    As Long
'***************************************  
Public Sub getVals()
'Get values from ini-file
    Dim varTemp() As Variant
    Dim i As Integer
   
    On Error Resume Next
   
    ReDim varTemp(0)
    varTemp = GetAllSettings(App.Path & "\my.ini", "Section Name", True)
end sub
'****************************************
Public Function GetAllSettings(AppName As String, Section As String, _
 Optional ByVal FromIniFile As Boolean) As Variant
'Calls API function to retrieve settings from ini-file or registry
'Source:http://www.aboutvb.de/khw/artikel/khwini.htm

  Dim nBuffer As String
  Dim nRet As Long
  Dim nItems() As String
  Dim nItem() As String
  Dim l As Long
  Dim nSettings As Variant
 
  Const kBufferSize = 32767
 
  On Error GoTo ErrGetAll
 
  If FromIniFile Then
    nBuffer = Space$(kBufferSize)
    nRet = _
     GetPrivateProfileSection(Section, nBuffer, kBufferSize, AppName)
    nItems = Split(Left$(nBuffer, nRet), Chr$(0))
    If Len(nItems(UBound(nItems))) Then
      ReDim nSettings(0 To UBound(nItems), 0 To 1)
    Else
      ReDim nSettings(0 To UBound(nItems) - 1, 0 To 1)
    End If
    For l = 0 To UBound(nSettings)
      nItem = Split(nItems(l), "=")
      nSettings(l, 0) = nItem(0)
      nSettings(l, 1) = nItem(1)
    Next 'l
    GetAllSettings = nSettings
  Else
    GetAllSettings = VBA.GetAllSettings(AppName, Section)
  End If
 
  Exit Function
 
ErrGetAll:
    Resume Next
   

End Function
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
(Response to: [Sweat]. I can use GetPrivateProfileString ok thanks, its GetPrivateProfileSection that I need to use).

Others.

Tried both code snippets, but still can't get anything back in the return variable or the buffer. I am using VB6 SP5, with NT 4.0 Workstation. Other API calls I can get to work ok, but this one, any ideas?

Commented:
Jon,
Can you tell me more about the empty lpReturnedString argument error that you are getting.  When I run the code snipet I gave you I get a string of values in the Ret variable, what do you get?
G

Commented:
Ken,
I've been using that code on win2000,98, and ME (with vb6 SP5) extensively, and it works every time.
Can you supply the relevant parts of your code, perhaps we can pinpoint the problem then.

Regards, Lemmchen

Author

Commented:
A friend pursuaded me to re-instal the service pack. This worked, I now get data back from my INI file. The solutions offered were good, thanks. But I'll go with the code from Glowman because of simplicity and I am not using the registry.

Thanks again..

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial