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
Vadim Melnik - 28 Aug 2003 21:18 GMT
Hi,
> [MarshalAs(UnmanagedType.LPStr)]string file);
> [DllImport("kernel32")]
> public static extern ulong GetLastError();
>
> Now, I don't
> know if it is even valid to call GetLastError through interop
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 - 28 Aug 2003 22:55 GMT
> Hi,
>
[quoted text clipped - 12 lines]
> Regards,
> Vadim.
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 - 29 Aug 2003 08:51 GMT
Hi,
> 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 .
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.
Daniel F. Devine - 29 Aug 2003 14:20 GMT
> Hi,
>
[quoted text clipped - 16 lines]
>
> [1]
http://msdn.microsoft.com/library/en-us/cpguide/html/cpconplatforminvokedatatypes.asp
> ..
> Regards,
> Vadim.
Yes it does - thank you very much - I didn't realize the C# ulong was 64
bits and not 32bits. Thanks for the explanation.