C ++ / CLI混合模式DLL创建 [英] C++/CLI Mixed Mode DLL Creation

查看:260
本文介绍了C ++ / CLI混合模式DLL创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地C ++的DLL,我想有一个C ++ / CLI包装层。从我的理解,如果你简单的增加一个C ++ / CLI类项目,VS会编译为混合模式,但我是作为VS似乎并不甚至触及管理code显然是错误的。

I've got a native C++ DLL that I would like to have a C++/CLI wrapper layer for. From what I understood, if you simple added a C++/CLI class to the project, VS would compile as mixed mode, but I was apparently wrong as VS doesn't seem to be even touching the managed code.

所以,对于一个pre-现有的本地code-基地什么的完全的,一步一步的,你需要做的,创建一个混合模式DLL,从而使我可以从任何.NET语言链接到了code?

So, given a pre-existing native code-base what exactly, step-by-step, do you need to do to create a mixed mode DLL, so that I can can link into that code from any .NET language?

*我需要做的这一点,因为我的家乡code采用C ++类,我不能的P / Invoke到。

*I need to do this because my native code uses C++ classes that I cannot P/Invoke into.

推荐答案

哦,不,它不会是混合模式,直到你告诉你的遗留DLL是写在非托管$ C $的C ++ / CLI编译器C。这本来应该是明显的,你应该从非托管DLL导出得到链接错误。您需要使用的#pragma管理:

Well, no, it doesn't get to be mix-mode until you tell the C++/CLI compiler that your legacy DLL was written in unmanaged code. Which should have been noticeable, you should have gotten linker errors from the unmanaged DLL exports. You need to use #pragma managed:

#pragma managed(push, off)
#include "oldskool.h"
#pragma comment(lib, "oldskool.lib")
#pragma managed(pop)

using namespace System;

public ref class Wrapper {
private:
    COldSkool* pUnmanaged;
public:
    Wrapper() { pUnmanaged = new COldSkool; }
    ~Wrapper() { delete pUnmanaged; pUnmanaged = 0; }
    !Wrapper() { delete pUnmanaged; }
    void sampleMethod() { 
        if (!pUnmanaged) throw gcnew ObjectDisposedException("Wrapper");
        pUnmanaged->sampleMethod(); 
    }
};

这篇关于C ++ / CLI混合模式DLL创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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