Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / February 2007

Tip: Looking for answers? Try searching our database.

Documentation error

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Can - 15 Feb 2007 12:03 GMT
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
Jochen Kalmbach [MVP] - 15 Feb 2007 12:15 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.