我如何使用C#DLL在Win32 C ++项目? [英] How can i use a C# dll in a Win32 C++ project?

查看:113
本文介绍了我如何使用C#DLL在Win32 C ++项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个解决方案时,最核心的发动机的开发是Win32 C ++(并且是独立于平台,并且也用于OS X),前一段时间,我们需要调用C ++核心引擎的DLL从C#和我能够(在SO此地某些线程的帮助下)加载主要解决在C#DLL。但现在我们已经在托管C#DLL中实现某些事情,需要在Win32中C ++项目使用它? (并且只提供了功能定义和DLL)

I am working on a solution, most of its core engine is developed as Win32 C++ (and is Platform independent and is also used on OS X), some time ago we needed to call C++ dll's of core engine from C# and I was able to Load main solution's DLL in C# (by the help of some threads here on SO). but now we have certain things implemented in Managed C# dll and need to use it in Win32 C++ project? (and just function definitions and dll are provided)

推荐答案

您可以创建一个托管C ++的互操作DLL充当围绕包装C#库。

You can create a managed C++ interop DLL to act as a wrapper around the C# library.

在托管C教程大多不幸++只能解释如何包装非托管C ++在C#中使用。但它可以同时工作的其他方式。

Most tutorials on managed C++ unfortunately only explain how to wrap unmanaged C++ for use in C#. But it can work the other way also.

在您的本地C ++代码定义一个抽象接口类,然后创建托管C ++ DLL里面的一个具体子类。调入的方法实现你的C#的对象。

Define an abstract interface class in your native C++ code, then create a concrete subclass inside the managed C++ DLL. Call into your C# objects in the method implementations.

最后,导出一个工厂函数,将实例化实现类,并返回一个基类指针,你的本机代码可以使用

Finally, export a factory function that will instantiate the implementation class and return a base-class pointer that your native code can use.

下面是一个简单的例子:

Here's a quick example:

首先,定义在你的本机DLL类接口

First, define the class interface in your native DLL.

interopclassbase.h

class InteropClassBase
{
public:
    virtual void doStuff() = 0;
    virtual int getValue() = 0;
    virtual void getString(CString* outStr) = 0;
};

现在你需要创建一个C ++ / CLI DLL,将允许你混合本机和托管代码一个单一的组装。一个新的C ++项目添加到您的解决方案,并在设定的公共语言运行库支持选项的混合(/ CLR)。

Now you need to create a C++/CLI DLL that will allow you to mix native and managed code in a single assembly. Add a new C++ project to your solution, and in the project configuration set the "Common Language Runtime Support" option to Mixed (/clr).

一旦你添加到您的C#库的引用(我们称之为ManagedLibrary),我们可以实现互操作的类:

Once you've added a reference to your C# library (which we'll call ManagedLibrary) we can implement the interop class:

interopclass.cpp

#include "interopclassbase.h"
#include <vcclr.h>

public class InteropClass : public InteropClassBase
{
protected:
    gcroot<ManagedLibrary::ManagedObject^> m_managedObject;

public:
    InteropClass()
    {
        m_managedObject = gcnew ManagedLibrary::ManagedObject();
    }

    virtual void doStuff()
    {
        m_managedObject->doStuff();
    }

    virtual int getValue()
    {
        return m_managedObject->getValue();
    }

    virtual void getString(CString* pOutStr)
    {
        System::String^ managedString = m_managedObject->getString();
        CString nativeString(managedString); // overloaded CString constructor
        if (pOutStr) *pOutStr = nativeString;
    }
};

__declspec(dllexport) InteropClassBase* getImplementationPointer()
{
   return new InteropClass();
}

现在你只需要在你的本地项目加载互操作DLL并调用导出的函数。

Now you just need to load the interop DLL from your native project and call the exported function.

这篇关于我如何使用C#DLL在Win32 C ++项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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