Whenever I use P/Invoke I write a method to wrap the API method:
\[
System.Runtime.InteropServices.DllImportAttribute
(
"Kernel32"
,
SetLastError=true
,
EntryPoint="GetPrivateProfileString"
)
\]
private unsafe static extern uint
API\_GetPrivateProfileString
(
string lpAppName
,
string lpKeyName
,
string lpDefault
,
byte\* lpReturnedString
,
int nSize
,
string lpFileName
) ;
public unsafe static int
GetPrivateProfileString
(
string lpAppName
,
string lpKeyName
,
string lpDefault
,
out string lpReturnedString
,
int nSize
,
string lpFileName
)
{
int result ;
byte\[\] temp = new byte \[ nSize \] ;
fixed ( byte\* ptemp = temp )
{
result = (int) API\_GetPrivateProfileString
(
lpAppName
,
lpKeyName
,
lpDefault
,
ptemp
,
nSize
,
lpFileName
) ;
}
lpReturnedString = System.Text.Encoding.Unicode.GetString ( temp ).Substring ( 0 , result ) ;
return ( result ) ;
}
I wrote this for someone else when I was just learning C#, I don't use it. I use XML for configuration files.