Click to See Complete Forum and Search --> : API funcGetPrivateProfileString


JohnDigweed
July 26th, 2002, 08:52 AM
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!



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

DSJ
July 26th, 2002, 09:28 AM
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(" ")

JohnDigweed
July 26th, 2002, 11:25 AM
Thanx for your response but this also does not work. If you have any other ideas please let met know.

John

DSJ
July 26th, 2002, 02:35 PM
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

JohnDigweed
July 29th, 2002, 03:21 AM
Thanx!