Results 1 to 3 of 3

Thread: GetPrivateProfileSection

  1. #1
    hasandak
    Guest

    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 ?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Some examples
    VB Code:
    1. 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
    2. 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
    3. Private Sub Form_Load()
    4.     'KPD-Team 1999
    5.     'URL: [url]http://www.allapi.net/[/url]
    6.     'E-Mail: [email]KPDTeam@Allapi.net[/email]
    7.     Dim Ret As String, NC As Long
    8.     'Write the setting to the file (c:\test.ini) under
    9.     '   Project1 -> Keyname
    10.     WritePrivateProfileString App.Title, "KeyName", "This is the value", "c:\test.ini"
    11.     'Create a buffer
    12.     Ret = String(255, 0)
    13.     'Retrieve the string
    14.     NC = GetPrivateProfileString(App.Title, "KeyName", "Default", Ret, 255, "C:\test.ini")
    15.     'NC is the number of characters copied to the buffer
    16.     If NC <> 0 Then Ret = Left$(Ret, NC)
    17.     'Show our string
    18.     MsgBox Ret
    19.     'Delete the file
    20.     Kill "c:\test.ini"
    21. End Sub
    22. '----------------------------------------------------------------------------------
    23. 'Example by Robin (rbnwares@edsamail.com.ph)
    24. 'Visit his homepage at [url]http://members.fortunecity.com/rbnwares1[/url]
    25. 'Note : Need one listbox named List1
    26. Private Declare Function GetPrivateProfileSectionNames Lib "kernel32.dll" Alias "GetPrivateProfileSectionNamesA" (ByVal lpszReturnBuffer As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    27. Private Sub Form_Load()
    28.     Dim szBuf As String, Length As Integer
    29.     Dim SectionArr() As String, m As Integer
    30.     szBuf = String$(255, 0)
    31.     Length = GetPrivateProfileSectionNames(szBuf, 255, vbNullChar)
    32.     szBuf = Left$(szBuf, Length)
    33.     SectionArr = Split(szBuf, vbNullChar)
    34.     For m = 0 To UBound(SectionArr)
    35.         List1.AddItem SectionArr(m)
    36.     Next m
    37. End Sub
    38. '----------------------------------------------------------------------------------
    39. 'Example by Robin (rbnwares@edsamail.com.ph)
    40. 'Visit his site at [url]http://members.fortunecity.com/rbnwares1[/url]
    41. Private Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    42. 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
    43. Private Sub Form_Load()
    44.     ' We will create a new section in the INI-file
    45.     ' It's output will be:
    46.     '
    47.     ' [SectionName]
    48.     ' Key=Value
    49.     '
    50.     ' Note that used this ONLY if you are creating a new
    51.     ' section on the INI file, unless you wanted to erase
    52.     ' its existing keys.
    53.     Call WritePrivateProfileSection("SectionName", "Key=Value", App.Path & "\sample.ini")
    54.     Dim szBuf As String * 255
    55.     Call GetPrivateProfileSection("SectionName", szBuf, 255, App.Path & "\sample.ini")
    56.     MsgBox szBuf
    57. End Sub

  3. #3
    New Member
    Join Date
    Nov 2016
    Posts
    6

    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
  •  



Click Here to Expand Forum to Full Width