Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net C++/CLI:#pragma托管/非托管范围_.net_C++ Cli_Clr_Pinvoke_Mixed Mode - Fatal编程技术网

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

.net C++/CLI:#pragma托管/非托管范围,.net,c++-cli,clr,pinvoke,mixed-mode,.net,C++ Cli,Clr,Pinvoke,Mixed Mode,我有一个混合模式DLL和一个包含托管和非托管代码的.cpp文件。简化的复制示例如下所示: #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

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

#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
我发现可以通过在.cpp文件末尾添加
#pragma managed
来消除错误。但我仍然不明白为什么会出现错误。对我来说,
#pragma unmanaged
的这种行为似乎完全违反直觉。有人能解释一下吗

据我所知:

  • #pragma(un)managed
    影响下面的代码编译。因此,如果它是在.cpp文件末尾定义的(下面没有任何内容),则它不应该有任何效果
  • 还说:

    实例化模板函数时,定义模板时的pragma状态将确定它是托管函数还是非托管函数
marshal_context
模板在包含的文件
marshal.h
中定义,并且应该编译为托管(因为它在前一行有明确的#pragma),而不管文件底部的#pragma是什么


我在什么地方错了吗?

嗯,这是出乎意料的。显然,编译器的缺陷可能与模板的延迟扩展有关。只需保持愉快,并将#pragma managed放在源文件的底部即可。@HansPassant谢谢,我将保持这种方式。我怀疑我错过了什么。
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>          ]