I am not able to understand how to fix CA2123 for a C++/CLI project. Here is a sample project to demonstrate the issue:
1) Create a C# (.NET 4) Class Library
ManagedClass.cs
namespace CSharpLibrary {
public interface IManagedClass
{
void WriteSomething();
}
public class ManagedClass : IManagedClass
{
public void WriteSomething()
{
}
}
}
2) Create a C++/CLI Console App (VS 2010):
AssemblyInfo.cpp
#include "stdafx.h"
using namespace System;
using namespace System::Reflection;
using namespace System::Runtime::CompilerServices;
using namespace System::Runtime::InteropServices;
using namespace System::Security;
[assembly:AssemblyTitleAttribute("CPlusPlusCLIConsoleApp")];
[assembly:AssemblyDescriptionAttribute("")];
[assembly:AssemblyVersionAttribute("1.0.*")];
[assembly:ComVisible(false)];
[assembly:CLSCompliantAttribute(false)];
[assembly:SecurityCritical];
CPlusPlusCLIConsoleApp.h
#pragma once
using namespace CSharpLibrary;
using namespace System::Security;
typedef void* (__cdecl FACTORY_PROC)();
namespace CPlusPlusCLIConsoleApp
{
public ref class MainClass : public IManagedClass
{
public:
[SecurityCritical]
virtual void WriteSomething();
};
};
CPlusPlusCLIConsoleApp.cpp
#include "stdafx.h"
#include "CPlusPlusCLIConsoleApp.h"
using namespace System;
int main(){};
namespace CPlusPlusCLIConsoleApp
{
[SecurityCritical]
void MainClass::WriteSomething()
{
}
};
After enabling all Microsoft Security Rules, I get this warning:
CA2123 Override link demands should be identical to base
Add the following security attribute to 'MainClass::WriteSomething(void)' in order to match a LinkDemand on base method 'IManagedClass::WriteSomething(void)': 'SecurityCriticalAttribute'.
CPlusPlusCLIConsoleApp cpluspluscliconsoleapp.cpp 13
I tried to follow what this StackOverflow answer suggested but it did not fix the error.
I understand that the managed dll is by default SecurityCritical (I do not want to change this in my original project) since I don't specify any SecurityAttribute. Why isn't the C++ CLI dll follow the same default?
What steps should I follow to fix this error? (Basically how can I make WriteSomething method SecurityCritical in C++ CLI)
EDIT 1: I have asked the same question on MSDN.
EDIT 2: Contacted Microsoft and it is an as designed behaviour. The C++\CLI team just did not have time to implement Level2 Security for C++\CLI. Hence C++\CLI is always stuck at Level1 Security. One can safely suppress the code analysis warning for the same.