GetPrivateProfileString

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Has any got GetPrivateProfileString to work in vb.net? I am upgrading from VB6.

Code snippet:

Public 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



Dim sBuf As String, lreturn As Long
Dim strTemp As String
Dim strTemp2 As String
Dim lSize As Long


strTemp = New String(CChar(" "), BufferLen)
lSize = Len(strTemp)

Try
lreturn = GetPrivateProfileString(SectionName, KeyName, strDef, strTemp, lSize, IniFile)
 
Has any got GetPrivateProfileString to work in vb.net? I am upgrading from VB6.

Code snippet:

Public 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



Dim sBuf As String, lreturn As Long
Dim strTemp As String
Dim strTemp2 As String
Dim lSize As Long


strTemp = New String(CChar(" "), BufferLen)
lSize = Len(strTemp)

Try
lreturn = GetPrivateProfileString(SectionName, KeyName, strDef, strTemp, lSize, IniFile)

Can you convert from C#?

Prototype:

// GetPrivateProfileString
[DllImport("kernel32.DLL",
EntryPoint="GetPrivateProfileString",
SetLastError=true,CharSet=CharSet.Auto)]
public static extern int GetPrivateProfileString(string
lpSectionName, string lpKeyName, string lpDefault, IntPtr
lpReturnedString, int nSize, string lpFileName);

Usage:
private string JINIGetKeyValue(string strSection, string
strKeyName, string strFileName)
{
int intReturn = 0;
int BufferSize = 2048;

IntPtr ipReturn = Marshal.AllocHGlobal(BufferSize);

intReturn = GetPrivateProfileString(strSection, strKeyName,
"", ipReturn, BufferSize, strFileName);
if(intReturn == 0)
return "";

string strReturn = Marshal.PtrToStringAuto(ipReturn,
intReturn);
Marshal.FreeHGlobal(ipReturn);

return strReturn;
}
 
Go to www.mentalis.org, and look for a project named IniReader in the
'useful classes' section. He has a complete set of managed wrappers around
the ini file access API. It's downloadable and free, if I recall correctly.

HTH,
Tom Dacon
Dacon Software Consulting


Neil G said:
Has any got GetPrivateProfileString to work in vb.net? I am upgrading from VB6.

Code snippet:

Public 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



Dim sBuf As String, lreturn As Long
Dim strTemp As String
Dim strTemp2 As String
Dim lSize As Long


strTemp = New String(CChar(" "), BufferLen)
lSize = Len(strTemp)

Try
lreturn = GetPrivateProfileString(SectionName, KeyName,
strDef, strTemp, lSize, IniFile)
 
Back
Top