Hi Vedo!
The MSDN documentation says that the GetLastError function can give
undefined behavior when compiling with /clr
ure switch. Does this
statement apply to both C++ interop and P/Invoke? If yes, does it mean
that we have to use Marshal::GetLastWin32Error to check the last error
code?
If you use DllImportAttribute, you can set "SetLastError" to true, this
will retrive the GetLastError value right after the call and you can use
the Marshal::GetLastWin32Error method to query these value.
And it seems that it also works for "C++ interop":
The following code:
namespace Foo
{
ref class Bar
{
void Test()
{
TCHAR szStr[100];
DWORD dwSize = 100;
BOOL bRet = GetUserName(szStr, &dwSize);
DWORD dw = GetLastError();
}
};
}
generates the following DllImport-Statement in VC2005:
[MethodImpl(MethodImplOptions.Unmanaged,
MethodCodeType=MethodCodeType.Native), SuppressUnmanagedCodeSecurity,
DllImport("", EntryPoint="", CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
public static extern unsafe int modopt(CallConvStdcall)
GetUserNameW(char*, uint modopt(IsLong)*);
Greetings
Jochen