How do you use this API? GetPrivateProfileSection
Printable View
How do you use this API? GetPrivateProfileSection
Here is an explanation and some code:
"The GetPrivateProfileString function retrieves a string from the specified section
in an initialization file. This function is provided for compatibility with 16-bit
Windows-based applications."
Code:Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, _
ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias _
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName _
As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Private Sub Form_Load()
Dim Ret As String, NC As Long
'Write the setting to the file (c:\test.ini) under
' Project1 -> Keyname
WritePrivateProfileString App.Title, "KeyName", "This is the value", "c:\test.ini"
'Create a buffer
Ret = String(255, 0)
'Retrieve the string
NC = GetPrivateProfileString(App.Title, "KeyName", "Default", Ret, 255, "C:\test.ini")
'NC is the number of characters copied to the buffer
If NC <> 0 Then Ret = Left$(Ret, NC)
'Show our string
MsgBox Ret
'Delete the file
Kill "c:\test.ini"
End Sub
I know how to use those two API. I just want to know how and what GetPrivateProfileSection do.
Basically it returns the information from the old .INI files that Windows 3.1 and other
earlier programs used to store information about themselves. Now we have functions
for doing this with the registry, where this type of information is now stored.
Is that all it does? Thank you for your explanation.
Two more question. Is it useless to learn since I know the two you posted? Do you know how to get the # of fields under a particular section(meaning the one with the brackets, example:[section])?
The GetPrivateProfileSection returns all the items (keys) under a specific bracketed group (section); e.g.
[Windows]
Screensaver="c:\windows\somefile.scr"
Mapi=1
Then it returns:
"Screensaver=""c:\windows\somefile.scr"" Mapi=1"
as a string.
If you specify a null string as the lpAppName it is supposed to return all of the sections
keys, but in experimenting with it, it doesn't return anything if you give it null!
I'm not sure why yet. So you have to know what section name you want.
Check out this thread for Microsoft's API reference.
[Edited by dsy5 on 09-23-2000 at 09:20 PM]
Update!
GetPrivateProfileSectionNames will return all the sections in the .INI file.
Therefore you'll have to use a combination of the other APIs in order to enumerate the entire file.
Shouldn't be too hard. If I get a chance I'll try to whip something up tomorrow.
P.S. That MS reference site is pretty good - I didn't even know that API existed.
Make a test file call test.txt. Place it in the same directory as the module.Code:Option Explicit
Public Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public 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
Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (ByVal lpSectionNames As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
''NOTICE: The following are made for Win.ini file only. - No File Path are required
'' It also doesn't have the word "Private" in the API name
'Public Declare Function GetProfileInt Lib "kernel32" Alias "GetProfileIntA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal nDefault As Long) As Long
'Public Declare Function GetProfileString Lib "kernel32" Alias "GetProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
'Public Declare Function WriteProfileString Lib "kernel32" Alias "WriteProfileStringA" (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
'Public Declare Function GetProfileSection Lib "kernel32" Alias "GetProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
'Public Declare Function WriteProfileSection Lib "kernel32" Alias "WriteProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String) As Long
Sub Main()
Call p_GetInteger(App.Path & "\Test.txt")
Call p_GetString(App.Path & "\Test.txt")
Call p_Write(App.Path & "\Test.txt")
Call p_GetSection(App.Path & "\Test.txt")
Call p_WriteSection(App.Path & "\Test.txt")
Call p_GetSectionName(App.Path & "\Test.txt")
End Sub
Sub p_GetInteger(str_FilePath As String)
'OVERALL: Obtain INTEGERS ONLY from file
' Read the value from the INI file, returning -1 if it can't find the value
MsgBox GetPrivateProfileInt("Info", "Time", -1, str_FilePath), , "p_GetInteger"
End Sub
Sub p_GetString(str_FilePath As String)
'OVERALL: Obtain String from file
' Read from the INI file
Dim str_Data As String
str_Data = Space(50)
Call GetPrivateProfileString("Info", "WindowText", "Error", str_Data, Len(str_Data), str_FilePath)
str_Data = Left(str_Data, InStr(1, str_Data, vbNullChar) - 1)
MsgBox str_Data, , "p_GetString"
End Sub
Sub p_Write(str_FilePath As String)
'OVERALL: Write data out
Dim str_Data As String
str_Data = "Data Value"
Call WritePrivateProfileString("New Bracket Section", "Field1", str_Data, str_FilePath)
MsgBox "", , "p_Write"
End Sub
Private Sub p_GetSection(str_FilePath As String)
'OVERALL: Get all fields in a specific bracket section
Dim str_Data As String
str_Data = Space(1024)
Call GetPrivateProfileSection("Info", str_Data, Len(str_Data), str_FilePath)
'PURPOSE:Break up string basing on vbNullChar and stick into an array
Dim str_Split() As String
str_Split = Split(str_Data, vbNullChar)
'PITFALL: Subtract two from the UBOUND because
' the last two are just vbNullChar
Dim int_X As Integer
For int_X = LBound(str_Split) To UBound(str_Split) - 2
MsgBox str_Split(int_X), , "p_GetSection"
Next
End Sub
Private Sub p_WriteSection(str_FilePath As String)
'OVERALL: Write a list of data out for a specific bracket section
'PITFALL: Add two vbNullChar to the end of the list - this is a STOP flag
' to signal to WritePrivateProfileSection api that the list has ended
Dim str_Data As String
str_Data = "Field1=Data Value1" & vbNullChar & "Field2=Data Value2" & vbNullChar & vbNullChar
Call WritePrivateProfileSection("WriteSection", str_Data, str_FilePath)
MsgBox "", , "p_WriteSection"
End Sub
Private Sub p_GetSectionName(str_FilePath As String)
'OVERALL: Get all bracket section name for the specify file
Dim str_Data As String
str_Data = Space(1024)
Call GetPrivateProfileSectionNames(str_Data, Len(str_Data), str_FilePath)
'PURPOSE:Break up string basing on vbNullChar and stick into an array
Dim str_Split() As String
str_Split = Split(str_Data, vbNullChar)
'PITFALL: Subtract two from the UBOUND because
' the last two are just vbNullChar
Dim int_X As Integer
For int_X = LBound(str_Split) To UBound(str_Split) - 2
MsgBox str_Split(int_X), , "p_GetSectionName"
Next
End Sub
The test file should look like this.
[Info]
WindowText="Shark"
WindowClass="Shark"
Time=5
X=.5
Y=.85
End=False
Tell me if you need anything else.
Nitro(Batman), how come in your p_WriteSection procedure, you need to end the list with two vbNullChar? Is that the way the api works?
Thanks you again to the both of you.
I don't believe they are necessary - you are only sending them to a file; they won't really show there.
Perhaps he did it because that is what is returned.
The function for GETting will always stick those nulls in. You could see for yourself when you wrote
your own .INI (you didn't put any Nulls in it.)
Batman??? :confused: :confused: :confused:
My name is Nitro.
You need the two vbnullchar at the end of the list to signal to the api that the list has ended.
Check this site out.
http://www.vbexplorer.com/focus/ini_tutorial.asp
It said the following.
"The list of entries to write. The entries in the list must be delimited by a NULL character; the list must be terminated by two NULL characters."
Thanks again!
Thanks Nitro for the explanation. There are some APIs that are difficult to find information
for.
You welcome! Glad that I can be of help.