包装托管代码以供非托管使用 [英] Wrapping managed code for unmanaged use

查看:31
本文介绍了包装托管代码以供非托管使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个大型 C++ 项目,它被编译为原生非托管代码.我们需要使用托管代码中的功能,但我们不想在/clr 中编译整个项目.

We have a big C++ project that's compiled as native unmanaged code. We need to use a feature from managed code, but we don't want to compile the whole project in /clr.

所以我创建了一个 DLL,有一个名为 B 的引用类,它在导出的本机类 A 中公开.问题是我得到了一个 C1190:由于 vcclr.h 包含,托管目标代码需要一个 '/clr' 选项.

So I made a DLL, have a ref class named B, which is exposed in exported native class A. Problem is I get a C1190: managed targeted code requires a '/clr' option because of the vcclr.h include.

我想知道是否有办法创建某种接口,在非托管方法中包含托管代码.

I'd like to know if there is a way to create some kind of interface that will have managed code within unmanaged methods.

这是我的代码:

#pragma once
#include "EX_Port.h"
#include <vcclr.h>

ref class B;

class EX_API A
{
    public:
        A();        
        int DeviceCount();

    private:
        gcroot<B^> _device;
};

我设法通过 gcnew cpp 中的 B 类使其工作.但是后来我有一个本地对象,而我想在全局范围内使用它.我刚开始做 CLI 编程,所以我可能不知道一些实践.

I managed to make it work by gcnew the B class within the cpp. But then I have a local object while I'd like to have it in the global scope. I just began doing CLI programming so I might not be aware of some practices.

谢谢

推荐答案

您的大型 C++ 程序必须先加载并初始化 CLR,然后才能执行任何托管代码.有几种方法可以做到这一点,从最灵活到最不灵活:

Your big C++ program is going to have to load and initialize the CLR before it can execute any managed code. There are several ways to do this, ranked from most flexible to least:

  • 它可以使用 CLR 托管接口显式加载 CLR 并执行任意托管代码.对此的基本入门是这个 MSDN 页面以及您可以在 CodeProject 等网站上找到的许多示例.com

  • It can use the CLR hosting interface to explicitly load the CLR and execute arbitrary managed code. Basic starter for that is this MSDN page and the many examples you can find on sites like CodeProject.com

您可以使您的托管类 [ComVisible].然后,您的 C++ 代码可以使用标准 COM 编程技术创建托管类的实例并调用其方法(CoInitializeEx 和 CoCreateInstance,#import 指令).COM 管道确保自动加载 CLR 并加载正确的程序集,不需要额外的代码来管理它.如果您已经对 COM 进行了投资,请考虑此选项,否则如果您没有 COM 的工作知识,则不应考虑此选项.

You can make your managed classes [ComVisible]. Your C++ code then can use standard COM programming techniques to create an instance of the managed class and call its methods (CoInitializeEx and CoCreateInstance, the #import directive). The COM plumbing ensures that the CLR is automatically loaded and loads the proper assembly, no additional code is required to manage that yourself. Consider this option when you already have an investment in COM, not otherwise something you should consider if you have no working knowledge of COM.

上述两种技术允许执行任何类型的托管代码,而不仅仅是 C++/CLI 代码.特定于 C++/CLI,您可以编写一个免费函数并将 __declspec(dllexport) 属性应用于它.编译器将生成一个导出函数的存根,以便您可以使用 LoadLibrary + GetProcAddress 从 C++ 代码中调用它.存根自动加载 CLR.这很容易上手,但非常不灵活,因为您只公开一个简单的函数而不是一个类.

The two above techniques allow any kind of managed code to be executed, not just C++/CLI code. Specific to C++/CLI, you can write a free function and apply the __declspec(dllexport) attribute to it. The compiler will generate a stub that exports the function so you can call it from your C++ code with LoadLibrary + GetProcAddress. The stub automatically loads the CLR. This is very easy to get going but is pretty inflexible since you are only exposing a simple function and not a class.

这篇关于包装托管代码以供非托管使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆