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

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

问题描述

我们有一个很大的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天全站免登陆