Documentation error

  • Thread starter Thread starter Can
  • Start date Start date
C

Can

From the MSN documentation;

"By default, /CLRUNMANAGEDCODECHECK is in effect, which means
SuppressUnmanagedCodeSecurityAttribute is applied to linker-generated
PInvoke calls. Specify /CLRUNMANAGEDCODECHECK:NO to not apply this
attribute."

But it seems it's just the opposite (which sounds more logical). By default
no SuppressUnmanagedCodeSecurityAttribute is applied to the C++ interop
calls. And /CLRUNMANAGEDCODECHECK:NO must be specified to apply the
attribute.

There is also a "bug" in Visual Studio. If you specify
AllowPartiallyTrustedCallers attribute, you must explicitly set the
/CLRUNMANAGEDCODECHECK[:NO] switch, but there is no way to set it as
/CLRUNMANAGEDCODECHECK:NO.

Can
 
Hi Can!
"By default, /CLRUNMANAGEDCODECHECK is in effect, which means
SuppressUnmanagedCodeSecurityAttribute is applied to linker-generated
PInvoke calls. Specify /CLRUNMANAGEDCODECHECK:NO to not apply this
attribute."

Yes, this seems to be a docu bug...
But it seems it's just the opposite (which sounds more logical). By default
no SuppressUnmanagedCodeSecurityAttribute is applied to the C++ interop
calls.

I can't confirm that...
If I specify *nothing* in the linker options it will generate the
following PInvoke declaration:

[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)*);

=> SuppressUnmanagedCodeSecurity is applied!

If I specify "/CLRUNMANAGEDCODECHECK", it will *not* apply the attribute:

[MethodImpl(MethodImplOptions.Unmanaged,
MethodCodeType=MethodCodeType.Native),
DllImport("", EntryPoint="", CallingConvention=CallingConvention.StdCall,
SetLastError=true)]
public static extern unsafe int modopt(CallConvStdcall)
GetUserNameW(char*, uint modopt(IsLong)*);


If I specify "/CLRUNMANAGEDCODECHECK:NO", it will *apply* the attribute:
[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)*);


And /CLRUNMANAGEDCODECHECK:NO must be specified to apply the
attribute.

Or you do not apply this option at all.

In short:

No linker option: => SuppressUnmanagedCodeSecurity
/CLRUNMANAGEDCODECHECK => *no* SuppressUnmanagedCodeSecurity
/CLRUNMANAGEDCODECHECK:NO => SuppressUnmanagedCodeSecurity

Tested with VS2005-SP1 and the following code:
#include <windows.h>
#pragma comment(lib, "Advapi32.lib")

namespace Foo
{
ref class Bar
{
void Test()
{
TCHAR szStr[100];
DWORD dwSize = 100;
BOOL bRet = GetUserName(szStr, &dwSize);
DWORD dw = GetLastError();
}
};
}

int main()
{
}

Greetings
Jochen
 
Thank you for your reply.

I guess I made a mistake by the "no switch" test. Sorry for that.

How do you specify "/CLRUNMANAGEDCODECHECK:NO" in Visual Studio? Because
when I try to set it through the project properties dialog (by selecting
"No" for the "CLR unmanaged code check" field) , VS just removes the switch
from the command line instead of appending "/CLRUNMANAGEDCODECHECK:NO". If
you do not use the AllowPartiallyTrustedCallers attribute, it does not
matter, because the default is NO switch anyway. But otherwise the linker
gives me a warning.

Again from the MSDN doc;

"Note that if you use AllowPartiallyTrustedCallersAttribute in your code,
you should explicitly set /CLRUNMANAGEDCODECHECK:NO. It is potential
security vulnerability if an image contains both the
SuppressUnmanagedCodeSecurity and AllowPartiallyTrustedCallers attributes."

For now I manually append the switch.

Regards;
Can
 
Hi Can!
How do you specify "/CLRUNMANAGEDCODECHECK:NO" in Visual Studio?

I added it in
"Linker|Command Line|Additonal Options"
;-)

Greetings
Jochen
 
Back
Top