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\stw lm.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(st rSECTION, 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("W indows", strSECTION, strKey, gstrNull, _
' strBuffer, gIntMax_SIZE, strIniFile) > 0 Then
If GetPrivateProfileString("W indows", strSECTION, strKey, _
strBuffer, gIntMax_SIZE, strIniFile) > 0 Then
ReadIniFile = strBuffer
TextBox1.Text = strBuffer
Else
ReadIniFile = gstrNull
End If
End Function
here is my code which returns lpReturnedString => 'ID=' which is lpDefault (my key)
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias _
"GetPrivateProfileStringA"
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\stw
"[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(st
' 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("W
' strBuffer, gIntMax_SIZE, strIniFile) > 0 Then
If GetPrivateProfileString("W
strBuffer, gIntMax_SIZE, strIniFile) > 0 Then
ReadIniFile = strBuffer
TextBox1.Text = strBuffer
Else
ReadIniFile = gstrNull
End If
End Function
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
);
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
);
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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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