Link to home
Start Free TrialLog in
Avatar of rapace3
rapace3

asked on

GetPrivateProfileString using VB net.

GetPrivateProfileString using VB net.
here is my code which returns lpReturnedString => 'ID=' which is lpDefault (my key)

    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
    "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal _
    lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString _
    As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

    '    Private Declare Function GetPrivateProfileString _
    '     Lib "kernel32" Alias "GetPrivateProfileStringA" _
 '     (ByVal lpSectionName As String, _
    '    ByVal lpKeyName As String, _
    '   ByVal lpDefault As String, _
    '  ByVal lpReturnedString As String, _
    ' ByVal nSize As Long, _
    'ByVal lpFileName As String) As Long



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ReadIniFileStr As String


        ReadIniFileStr = ReadIniFile("c:\1utils\stwlm.ini", _
                         "[RackPC]", "ID=")

    End Sub

    Function ReadIniFile(ByVal strIniFile As String, _
                         ByVal strSECTION As String, ByVal strKey As String) As String

        Dim strBuffer As String
        Dim intPos As Integer
        Const gstrNull = ""
        Dim gIntMax_SIZE As Integer
        gIntMax_SIZE = 255
        strBuffer = Space$(gIntMax_SIZE)

        '        If GetPrivateProfileString(strSECTION, strKey, gstrNull, _
        '             strBuffer, 255, strIniFile) > 0 Then

        '    Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
        '   "GetPrivateProfileStringA" (
        'ByVal lpApplicationName As String,
        'ByVal lpKeyName As String,
        'ByVal lpDefault As String,
        'ByVal lpReturnedString As String,
        'ByVal nSize As Long,
        'ByVal lpFileName As String) As Long

        '        If GetPrivateProfileString("Windows", strSECTION, strKey, gstrNull, _
        '             strBuffer, gIntMax_SIZE, strIniFile) > 0 Then
        If GetPrivateProfileString("Windows", strSECTION, strKey, _
              strBuffer, gIntMax_SIZE, strIniFile) > 0 Then
            ReadIniFile = strBuffer
            TextBox1.Text = strBuffer
        Else
            ReadIniFile = gstrNull
        End If
    End Function
Avatar of rapace3
rapace3

ASKER

I expect that the GetPrivateProfileString API call does not work for VB.net.
Help offer the API call below which does not work either. What does work to read an INI file in VB.NET?

Private Declare Function _
   GetPrivateProfileString Lib _
   "kernel32" Alias _
   "GetPrivateProfileStringA" _
   (ByVal SectionName As String, _
   ByVal KeyName As String, _
   ByVal Default As String, _
   ByVal ReturnedString As String, _
   ByVal StringSize As Long, _
   ByVal FileName As String) As Long
I don't think you want the brackets around the strSECTION value.
DWORD GetPrivateProfileString(
  LPCTSTR lpAppName,                      Don't you want strSECTION here, not "WINDOWS"
  LPCTSTR lpKeyName,                      strKey here
  LPCTSTR lpDefault,                         gstrNull
  LPTSTR lpReturnedString,            the rest OK
  DWORD nSize,
  LPCTSTR lpFileName
);

Avatar of Ryan Chong
Try change As Long to As Integer (In the condition that suspect there is NO error on your codes):

Private Declare Function _
  GetPrivateProfileString Lib _
  "kernel32" Alias _
  "GetPrivateProfileStringA" _
  (ByVal SectionName As String, _
  ByVal KeyName As String, _
  ByVal Default As String, _
  ByVal ReturnedString As String, _
  ByVal StringSize As Integer, _
  ByVal FileName As String) As Integer
ASKER CERTIFIED SOLUTION
Avatar of DanRollins
DanRollins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial