CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2002
    Location
    Amsterdam
    Posts
    6

    Question Read INI file with GetPrivateProfileString

    Hello,

    I'm trying to read an ini file with the API Call GetPrivateProfileString. The code below works in VB6, but not in VB.NET. Does anybody know why. Please let me know if u do!


    Code:
    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 Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim strRetString As String
            strRetString = "          "
            Dim lngRetval As Long
            lngRetval = GetPrivateProfileString("SECTIONNAME", "KEYNAME", "Default Value", strRetString, Len(strRetString), "C:\AnIniFile.ini")
    
    End Sub
    Last edited by JohnDigweed; July 26th, 2002 at 09:11 AM.
    Regards,

    John Digweed

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    I haven't tested, but try changing you DIM of strRetString to:

    Dim strRetString As New String(Space(100))

    or

    Dim strRetString as New String(" ")

  3. #3
    Join Date
    Jun 2002
    Location
    Amsterdam
    Posts
    6
    Thanx for your response but this also does not work. If you have any other ideas please let met know.

    John
    Regards,

    John Digweed

  4. #4
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    Classic beginner mistake for both of us... VB 6's LONG should now be INTEGER, try this:

    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 Integer, ByVal _
    lpFileName As String) As Long

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim strRetString As New String(Space(50))
    Dim lngRetval As Long
    lngRetval = GetPrivateProfileString("SECTIONNAME", "KEYNAME", "Default Value", strRetString, strRetString.Length, "C:\AnIniFile.ini")
    If lngRetval <> 0 Then
    MsgBox(strRetString.Trim)
    End If
    End Sub

  5. #5
    Join Date
    Jun 2002
    Location
    Amsterdam
    Posts
    6

    GetPrivateProfileString

    Thanx!
    Regards,

    John Digweed

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

Featured