Hi Vedo!
The MSDN documentation says that the GetLastError function can give
undefined behavior when compiling with /clr:pure switch. Does this statement
apply to both C++ interop and P/Invoke? If yes, does it mean that we have to
use Marshal::GetLas tWin32Error to check the last error code?
If you use DllImportAttrib ute, you can set "SetLastErr or" to true, this
will retrive the GetLastError value right after the call and you can use
the Marshal::GetLas tWin32Error 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(szS tr, &dwSize);
DWORD dw = GetLastError();
}
};
}
generates the following DllImport-Statement in VC2005:
[MethodImpl(Meth odImplOptions.U nmanaged,
MethodCodeType= MethodCodeType. Native), SuppressUnmanag edCodeSecurity,
DllImport("", EntryPoint="",
CallingConventi on=CallingConve ntion.StdCall, SetLastError=tr ue)]
public static extern unsafe int modopt(CallConv Stdcall)
GetUserNameW(ch ar*, uint modopt(IsLong)* );
Greetings
Jochen