C ++ / CLI包装管理code非托管使用 [英] C++/CLI Wrapping Managed Code for Unmanaged Use

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

问题描述

我们有一个的编译为本地非托管code大C ++项目。我们需要使用的功能,从管理code,但我们不希望编译/ 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:管理目标code要求,因为vcclr的'/ CLR选项。 ^ h包括

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.

我想知道是否有创造某种接口,将设法code不受管理方法中的一种方式。

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

下面是我的code:

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

ref class B;

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

    private:
        gcroot<B^> _device;
};

我设法让一个cpp中gcnew的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,才可以执行任何管理code。有几种方法可以做到这一点,从最灵活的排名至少到:

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并执行任意管理code。对于基本起动器是这个 MSDN页面和许多例子,你可以找到像$ C网站$ cProject.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 ++ code,那么可以使用标准的COM编程技术来创建托管类的实例,并调用它的方法(和的CoInitializeEx CoCreateInstance的,#import指令)。在COM管道确保CLR会自动加载并加载正确的组装,不需要额外的code要求来管理自己。当你已经拥有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.

上面的两种技术允许任何一种管理code的要执行,不只是C ++ / CLI code。具体到C ++ / CLI,你可以写一个免费的功能和__declspec(dllexport)的属性应用到它。编译器将生成导出功能的存根,以便你可以从你的C ++ code。与调用LoadLibrary + GetProcAddress的调用它。存根自动加载CLR。这是很容易得到持续,但为pretty缺乏灵活性,因为你只露出一个简单的功能,而不是一类。

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.

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

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