C ++ / CLI: managed / unmanaged scope # pragma
I have a mixed DLL and a .cpp file in it with managed and unmanaged code. A simplified reproduction example looks like this:
#include "stdafx.h"
#pragma managed // Just for explicitness (doesn't influence results)
#include <msclr\marshal.h>
void Test()
{
System::String^ sName = "";
msclr::interop::marshal_context context;
context.marshal_as<const TCHAR*>(sName);
}
//#pragma unmanaged // uncomment this line to get errors
This code compiles successfully, however if I uncomment the last line ( #pragma unmanaged
) it throws the following errors:
2> Test.cpp
2>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\msclr\marshal.h(48): error C3280: 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler' : a member-function of a managed type cannot be compiled as an unmanaged function
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> _Needs_Context=true
2> ]
2> This diagnostic occurred in the compiler generated function 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler(void)'
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> _Needs_Context=true
2> ]
2>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\msclr\marshal.h(48): error C3642: 'System::Object::Object(void)' : cannot call a function with __clrcall calling convention from native code
2> This diagnostic occurred in the compiler generated function 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler(void)'
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> _Needs_Context=true
2> ]
2>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\msclr\marshal.h(48): error C3175: 'System::Object::Object' : cannot call a method of a managed type from unmanaged function 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler'
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> _Needs_Context=true
2> ]
2> This diagnostic occurred in the compiler generated function 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler(void)'
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> _Needs_Context=true
2> ]
2>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\msclr\marshal.h(48): error C3821: 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler(void)': managed type or function cannot be used in an unmanaged function
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> _Needs_Context=true
2> ]
2> This diagnostic occurred in the compiler generated function 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler(void)'
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> _Needs_Context=true
2> ]
2>C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\msclr\marshal.h(282): error C3645: 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler' : __clrcall cannot be used on functions compiled to native code
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> _Needs_Context=true
2> ]
2> This diagnostic occurred in the compiler generated function 'msclr::interop::marshal_context::internal_marshaler<_To_Type,_From_Type,_Needs_Context>::internal_marshaler(void)'
2> with
2> [
2> _To_Type=const wchar_t *,
2> _From_Type=System::String ^,
2> _Needs_Context=true
2> ]
I found that I can get rid of the error by appending #pragma managed
to the end of my .cpp file. But I still don't understand why the errors occur. This behavior #pragma unmanaged
seems completely intuitive to me. Can someone please explain?
As far as I understand:
-
#pragma (un)managed
affects the compilation of the code below . So if it is defined at the end of the .cpp file (nothing below), it should have no effect. - Also http://msdn.microsoft.com/en-us/library/0adb9zxe.aspx says:
When a template function is created, the state of the pragma at the time of definition for the template determines whether it is managed or unmanaged.
marshal_context
the template is listed in the included file
marshal.h
and must be compiled as managed (since it has an explicit #pragma on the previous line) regardless of the #pragma at the bottom of the file.
Am I wrong somewhere?
source to share
No one has answered this question yet
See similar questions:
or similar: