Groups | Blog | Home
all groups > dotnet interop > august 2003 >

dotnet interop : PInvoke and GetPrivateProfileString


Daniel F. Devine
8/28/2003 2:52:21 PM
Hi -

The following snippet of code:

class Class1
{

[DllImport("kernel32")]
public static extern ulong
GetPrivateProfileString([MarshalAs(UnmanagedType.LPStr)]string app,

[MarshalAs(UnmanagedType.LPStr)]string key,

[MarshalAs(UnmanagedType.LPStr)]string def,

StringBuilder retval,

ulong size,

[MarshalAs(UnmanagedType.LPStr)]string file);
[DllImport("kernel32")]
public static extern ulong GetLastError();


[STAThread]
static void Main(string[] args)
{
string appname = "LOGON";
string key = "USER";
string df = "Helpless";
string fn = @"C:Work\ReadINI\ReadIni\bin\varisol.ini";

StringBuilder sb = new StringBuilder(20);

ulong errorcode = 0UL;

errorcode = GetPrivateProfileString(appname,key,df,sb,21UL,fn);

errorcode = GetLastError();

}
}


is intended to read the user name from the logon section of an ini file.
The StringBuilder variable contains the default value of "Helpless" after
invoking the function - so that it appears the function was at least
invoked. However, the correct value is never returned and the last error
function indicates that the specified function was not found. Now, I don't
know if it is even valid to call GetLastError through interop but
GetPrivateProfileString doesn't return an error code - so I have resorted to
this.

Does anybody know what I am doing wrong ?

Thanks for any help

The ini file looks like this:

[LOGON]
USER=Bill

Daniel F. Devine
8/28/2003 5:55:22 PM

[quoted text, click to view]

Vadim -

Thanks for the reply - I finally got the function to work by changing the
size paramater from an unsigned long to an int. The GetPrivateProfileString
Docs state that this is a DWORD and the Framework Docs state that a DWORD is
a ulong - so I don't know why this was failing. Does anyone have any
insight - are the Docs wrong? I mean that seems unlikely .

Vadim Melnik
8/28/2003 11:18:04 PM
Hi,

[quoted text, click to view]

No, it's not valid. See Marshal.GetLastWin32Error method to retrieve Win32
error code (note unmanaged function called via P/Invoke should have
DllImportAttribute.SetLastError field set to true).

...
Regards,
Vadim.

Daniel F. Devine
8/29/2003 9:20:14 AM

[quoted text, click to view]

Yes it does - thank you very much - I didn't realize the C# ulong was 64
bits and not 32bits. Thanks for the explanation.


Vadim Melnik
8/29/2003 10:51:33 AM
Hi,

[quoted text, click to view]

As far as I know System.UInt32 or System.Int32 are valid .NET data types for
Win32 DWORD type (more exactly only first variant is valid) . There is no
error in documentation [1], "unsigned long" is C equivalent, and
System.UInt32 mentioned as managed variant.

Your code failed because ulong in C# is unsigned 64-bit integer
(System.UInt64), while GetPrivateProfileString accepts 32-bit parameter.
Hope this explanation helps somehow.


[1]
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconplatforminvokedatatypes.asp

...
Regards,
Vadim.

AddThis Social Bookmark Button