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

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

问题描述

我有一个本机 C++ DLL,我想要一个 C++/CLI 包装层.据我了解,如果您简单地向项目添加 C++/CLI 类,VS 将编译为混合模式,但我显然错了,因为 VS 似乎甚至没有触及托管代码.

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.

那么,给定一个预先存在的本机代码库,确切地,您是否需要逐步创建混合模式 DLL,以便我可以链接到该代码库来自任何 .NET 语言的代码?

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?

*我需要这样做,因为我的本机代码使用我无法 P/Invoke 的 C++ 类.

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

推荐答案

好吧,不,除非您告诉 C++/CLI 编译器您的旧 DLL 是用非托管代码编写的,否则它不会成为混合模式.这应该很明显,您应该从非托管 DLL 导出中得到链接器错误.您需要使用#pragma managed:

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天全站免登陆