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

2024-04-19

我有一个混合模式 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

该代码编译成功,但是如果我取消注释最后一行(#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>          ]

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

据我所理解:

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

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


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C++/CLI:#pragma 托管/非托管范围 的相关文章

随机推荐

  • 使用 @Assisted 注入与多个相同类型的参数(@Named params)

    我的问题归结为使用 Assisted 和工厂的两个字符串参数 问题是 因为 Guice 将类型视为参数的识别机制 所以两个参数是相同的 并且我收到配置错误 一些代码 public class FilePathSolicitingDialog
  • 将 UIView 保持在 Circle Objective-C 内

    我怎样才能拥有我的颜色选择器 UIView 留在 300px 300px 的色轮内 我正在使用UIPanGestureRecognizer将颜色选择器拖动到色轮周围UIImageView 色轮的直径为300px 这是我的图像UIViewCo
  • 使用 SqlBulkCopy 插入 sql_variant 列时如何设置列类型

    我正在使用 SqlBulkCopy 从 net DataTable 对象插入 更新到包含 sql variant 列的 SQL Server 表 然而 当我需要的是 datetime2 时 SqlBulkCopy 坚持将放入该列的 Date
  • Devise 和 I18n - 重置密码路由问题

    我将 I18n 添加到使用 Devise 的 RoR 应用程序中 如果我尝试重置密码 现在会收到错误消息 错误是 Routing Error No route matches action gt edit controller gt dev
  • 如何从子活动中完成父活动

    我是 Android 开发新手 我创建了一个主 Activity gt A 它有 4 个按钮 4 个按钮之一是 EXIT 按钮 单击退出按钮后 我开始另一个活动 gt B 这将通过 A 活动的意图打开 B 活动 活动 B 包含 您想退出吗
  • 删除扰乱格式的特殊字符

    我目前正在创建一个聊天 似乎无法找到一种方法来阻止用户发布特殊字符 这些字符会扰乱聊天的格式并使最终用户退出聊天 我基本上是想完全删除它们 我知道我现在拥有的代码 从技术上讲 如果它有效 应该只能替换它们 但是我只是想让它首先工作 这是我用
  • 在ESC/POS命令中选择字符代码表

    我需要在打印收据上打印非英文字符 使用热敏 POS 收据打印机 Xprinter XP 58III 热敏 POS 收据打印机支持通用 ESC POS 命令 据我所知 这应该通过设置字符代码表来完成 就我而言 目标代码页是 21 用于设置代码
  • 从 Go 连接到 MySQL 的推荐方式是什么? [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找一个可靠的解决方案来从 Go 连接到 MySQL 数据库 我见过一些库 但很难确定完整性和当前
  • TransactionScope 无法与 DB2 提供程序一起使用

    我一直在尝试使用交易范围 with a DB2数据库 使用DB2 Net提供商 v 9 0 0 2 和 c 2 0 根据 IBM 的说法 应该支持它们 我已经尝试了在 IBM 论坛上可以找到的所有建议 例如here http www ibm
  • nginx conf 文件:检测浏览器语言是否为“de”,然后重定向到页面....否则重定向到其他页面

    我想在横幅中创建一个链接以重定向到两个页面之一 该链接指向subdomain example com email 如果浏览器语言是 de 则转到www example de banner否则去www example com banner 我
  • 在SURF中使用欧几里德距离

    在我的代码中 我根据最近邻距离比过滤好图像 如下所示 for int i 0 i lt min des image rows 1 int matches size i if matches i 0 distance lt 0 6 match
  • 如何杀死 Linux 中特定端口上运行的进程?

    我尝试使用关闭tomcat shutdown sh来自雄猫 bin目录 但发现服务器没有正常关闭 因此我无法重新启动我的 tomcat 正在端口上运行8080 我想杀死正在运行的tomcat进程8080 我首先想要获得在特定端口 8080
  • 为什么我不能将反向迭代器转换为正向迭代器?

    嗯 我知道为什么 是因为没有转化 但是为什么没有转化呢 为什么正向迭代器可以变成反向迭代器 但反之则不行 更重要的是 如果我想这样做 我该怎么办 是否有一些适配器允许您使用前向迭代器向后迭代 std vector
  • 在 Android 中隐藏 RadioButton

    我想将 RadioButton 的可见性设置为 INVISIBLE 或 GONE 由于某种原因 这不起作用 RadioButton myRadioButton RadioButton findViewById R id my radio b
  • 没有什么可以帮助解决 Qt 中的“对 vtable 的未定义引用”

    我无法构建此错误 未定义对 CustomUndoStack 的 vtable 的引用 这是代码 class CustomUndoStack public QObject Q OBJECT public int main int argc c
  • ANSI SQL 问题 - 如果记录已存在,如何插入或更新记录?

    虽然我 目前 使用 mySQL 但我不需要任何数据库特定的 SQL 我试图插入一条记录 如果不存在 并更新一个字段 如果存在 我想使用 ANSI SQL 该表看起来像这样 create table test table id int nam
  • Greasemonkey:XPath 未返回 .xhtml 页面的结果

    在 xhtml 页面上运行时 xpath 似乎没有返回任何结果 var result document evaluate a img document body null XPathResult ORDERED NODE SNAPSHOT
  • 在 Windows Azure 上使用quartz.net

    我在我的 asp net 应用程序中使用quartz net 当我在本地主机上运行时 quartz 调度程序可以工作 但是当我将站点发布到 windows azure 时 它 不再工作 任何帮助将非常感激 问题不在于它没有在天蓝色站点上运行
  • /etc/fstab 中的动态 IP

    我正在尝试在 AWS 中使用弹性文件系统 EFS 我的目标是使用 etc fstab 自动挂载它 由于 EC2 实例会跨可用区域自动扩展 因此 EFS 挂载 IP 会根据实例的区域而变化 目前AWS提供了这个命令来将其安装到正确的区域 su
  • C++/CLI:#pragma 托管/非托管范围

    我有一个混合模式 DLL 其中有一个包含托管和非托管代码的 cpp 文件 一个简化的重现示例如下所示 include stdafx h pragma managed Just for explicitness doesn t influen