/clr:pure & GetLastError

V

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::GetLastWin32Error to check the last error code?
 
J

Jochen Kalmbach [MVP]

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::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
 
V

Vedo

thank you...

Jochen Kalmbach said:
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::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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top