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
Can - 15 Feb 2007 14:55 GMT
Thank you for your reply.
>> I can't confirm that...
>> If I specify *nothing* in the linker options it will generate the
>> following PInvoke declaration:
I guess I made a mistake by the "no switch" test. Sorry for that.
>> If I specify "/CLRUNMANAGEDCODECHECK:NO", it will *apply* the attribute:
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
Jochen Kalmbach [MVP] - 15 Feb 2007 15:02 GMT
Hi Can!
> How do you specify "/CLRUNMANAGEDCODECHECK:NO" in Visual Studio?
I added it in
"Linker|Command Line|Additonal Options"
;-)
Greetings
Jochen
Can - 16 Feb 2007 10:19 GMT
Oh I see :)
Thank you very much...
Regards
Can
> Hi Can!
>> How do you specify "/CLRUNMANAGEDCODECHECK:NO" in Visual Studio?
[quoted text clipped - 5 lines]
> Greetings
> Jochen