C ++ / CLI:#pragma托管/非托管范围

时间:2015-01-12 13:47:35

标签: .net c++-cli clr pinvoke mixed-mode

我有一个混合模式DLL,其中包含托管代码和非托管代码的.cpp文件。简化的repro示例如下所示:

#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

此代码编译成功,但如果我取消注释最后一行(#pragma unmanaged),则会产生以下错误:

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>          ]

我发现我可以通过在.cpp文件的末尾添加#pragma managed来消除错误。但我仍然不明白为什么错误发生。 #pragma unmanaged的这种行为对我来说似乎绝对违反直觉。有人可以解释一下吗?

据我所知:

  • #pragma (un)managed影响代码编译 。因此,如果它是在.cpp文件的末尾定义的(下面没有任何内容),它应该没有任何效果。
  • 同样 http://msdn.microsoft.com/en-us/library/0adb9zxe.aspx说:

      

    当实例化模板函数时,模板定义时的编译指示状态确定它是受管理还是非受管。

  •   
  marshal_context模板在包含的文件marshal.h中定义,并且应该被编译为托管(因为它在前一行上有明确的#pragma),而不管文件底部的#pragma。

我错了吗?

0 个答案:

没有答案