-
Apr 7th, 2002, 03:49 AM
#1
GetPrivateProfileSection
I want to read from ini file inside Terminal Server .
I want that every user will have another ini file.
How can I do It ?
-
Apr 7th, 2002, 07:12 AM
#2
Some examples
VB 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()
'KPD-Team 1999
'URL: [url]http://www.allapi.net/[/url]
'E-Mail: [email]KPDTeam@Allapi.net[/email]
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
'----------------------------------------------------------------------------------
'Example by Robin (rbnwares@edsamail.com.ph)
'Visit his homepage at [url]http://members.fortunecity.com/rbnwares1[/url]
'Note : Need one listbox named List1
Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Sub Form_Load()
Dim szBuf As String, Length As Integer
Dim SectionArr() As String, m As Integer
szBuf = String$(255, 0)
Length = GetPrivateProfileSectionNames(szBuf, 255, vbNullChar)
szBuf = Left$(szBuf, Length)
SectionArr = Split(szBuf, vbNullChar)
For m = 0 To UBound(SectionArr)
List1.AddItem SectionArr(m)
Next m
End Sub
'----------------------------------------------------------------------------------
'Example by Robin (rbnwares@edsamail.com.ph)
'Visit his site at [url]http://members.fortunecity.com/rbnwares1[/url]
Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private 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
Private Sub Form_Load()
' We will create a new section in the INI-file
' It's output will be:
'
' [SectionName]
' Key=Value
'
' Note that used this ONLY if you are creating a new
' section on the INI file, unless you wanted to erase
' its existing keys.
Call WritePrivateProfileSection("SectionName", "Key=Value", App.Path & "\sample.ini")
Dim szBuf As String * 255
Call GetPrivateProfileSection("SectionName", szBuf, 255, App.Path & "\sample.ini")
MsgBox szBuf
End Sub
-
Dec 9th, 2017, 03:11 PM
#3
New Member
Re: GetPrivateProfileSection
Hi, I know this is an old thread. But i think my reply will help new readers.
When you trying to use api functions like "GetPrivateProfileSection", you need to know something.
1. The return value if this function
2. The result of this function.
Return value is equal to the string length the result contains.
Result is a string which contains more than one null terminated string.
So we can assume the return string like this. -- "Key=Val1 /0 Key=Val2 /0 Key=Val3 /0 Key=Val4 /0/0"
"/0" is for drawing null string. We cant use a stringbuilder to get this string in vb.net. It will stop reading this string when it encounter the first null string. So we only get "Key=Val1" with StringBuilder.
Instead we need to use marshal class for this purpose.
See this.
Code:
Dim MyString As String = String.Empty
Dim RetVal As Integer
Dim BufferSize As Integer = 1024
Dim PtrToString As IntPtr = IntPtr.Zero
'Now, we can use the PtrToString in GetPrivateProfileSection function's second parameter.
RetVal = GetPrivateProfileSection(SectionName, PtrToString, BufferSize, FileNameAndPah)
' If our function call succeeds, we will get the result in PtrToString memory address and RetVal will contain the length of the string in PtrToString. Else if this function failed due to the lack of enough BufferSize Then RetVal will contain BufferSize - 2. So we can check it and call this function again with larger BufferSize.
'Now, here is how we can get the string from memory address.
MyString = Marshal.PtrToStringAuto(PtrToString, RetVal - 1)
'Here i use " RetVal - 1 " to avoid the extra null string.
' Now, we need to split the string where null chars coming.
Dim MyStrArray() As String = MyString.Split(vbNullChar)
'So this array contains all your keyvalue pair in that specific section.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|