我如何在 Win32 C++ 中使用 C# dll项目?

发布于 2024-08-24 23:45:40 字数 198 浏览 4 评论 0原文

我正在研究一个解决方案,它的大部分核心引擎都是作为 Win32 C++ 开发的(并且与平台无关,也在 OS X 上使用),前段时间我们需要从 C# 调用核心引擎的 C++ dll,我能够在 C# 中加载主解决方案的 DLL(在 SO 上的一些线程的帮助下)。但现在我们在托管 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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

止于盛夏 2024-08-31 23:45:40

您可以创建托管 C++ 互操作 DLL 来充当 C# 库的包装器。

遗憾的是,大多数有关托管 C++ 的教程仅解释如何包装非托管 C++ 以在 C# 中使用。但它也可以以其他方式发挥作用。

在本机 C++ 代码中定义一个抽象接口类,然后在托管 C++ DLL 中创建一个具体子类。在方法实现中调用 C# 对象。

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

下面是一个简单的示例:

首先,在本机 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)。

添加对 C# 库(我们将其称为 ManagedLibrary)的引用后,我们就可以实现互操作类:

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 并调用导出的函数。

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

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.

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:

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;
};

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).

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();
}

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

小嗷兮 2024-08-31 23:45:40

一种解决方案是 COM Interop。可能是唯一的解决方案。这是一个很大的话题。我在工作时使用一本厚厚的蓝皮书。数百页除了 COM 互操作之外什么都没有。

简而言之,您在托管端标记一些类和接口,并创建看起来像 COM dll 的互操作程序集,但实际上是托管程序集的代理。互操作程序集像 COM dll 一样注册,然后就可以使用了。

MSDN 有很多相关信息。

这可能是一个很好的起点。
“向 COM 公开 .NET Framework 组件”
http://msdn.microsoft.com/en- us/library/aa720072%28VS.71%29.aspx

可以非常简单,但尽量保持简单。

One solution is COM Interop. Possibly the only solution. It a big topic. There's a big fat blue book at work I use. Hundreds of pages of nothing but COM Interop.

The short version is that you label some classes and interfaces in the managed side, and have interop assemblies created which look like COM dlls, but are really proxies to the managed assemblies. The interop assemblies are registered like COM dlls and off you go.

MSDN has a lot of information about it.

This is probably a good starting spot.
"Exposing .NET Framework Components to COM"
http://msdn.microsoft.com/en-us/library/aa720072%28VS.71%29.aspx

It can be surprisingly easy, but try to keep it simple.

叫嚣ゝ 2024-08-31 23:45:40

要创建托管对象并调用其方法,您需要在 C++ 进程中运行 CLR。

在 Windows 下,您可以通过引用 mscoree.dll 并在进程中托管 CLR 来托管 CLR。

http://msdn.microsoft.com/en-us/magazine/cc163567。 .aspx
http://msdn.microsoft.com/en-us/library/ms230997。 aspx

对于 Mono,您可以将 Mono 运行时嵌入到您的 C++ 应用程序中。

http://www.mono-project.com/Embedding_Mono

To create a managed object and call methods on it you need to have the CLR running in your C++ process.

Under windows you can host the CLR by referencing mscoree.dll and hosting the CLR in process.

http://msdn.microsoft.com/en-us/magazine/cc163567.aspx
http://msdn.microsoft.com/en-us/library/ms230997.aspx

For Mono, you can embed the Mono runtime within your C++ application.

http://www.mono-project.com/Embedding_Mono

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文