创建混合模式 C++ 从 C 到 C# 的桥梁?

发布于 2024-07-08 14:39:11 字数 748 浏览 8 评论 0原文

我希望有人能帮助我解决这个问题,我主要是一名 C# 开发人员,所以我的 C和C++技能很差。 我有一个本机 C dll,它是一个插件 更大的应用。 我在Linux上为Windows交叉编译这个dll 使用海湾合作委员会。

在本机 dll 中,当我创建 D3DSurface 时,我想调用一个函数 在混合模式 C++ dll 中,并将指针传递到表面 带有 Hwnd/句柄。 然后混合模式 C++ 应该调用我的 C# 托管代码。

举个例子,在 CI 中想要执行以下操作;

Hwnd handle;
LPDIRECT3DSURFACE d3dtarg;
SurfaceCreated(handle, d3dtarg);

在 C# 中,我希望从混合模式程序集中调用它,

public static class D3DInterop
{
    public static void SurfaceCreated(IntPtr handle, IntPtr surface)
    {
        //do work
    }
}

因为我对 C++ 很烂,我只想知道是否有人可以给我一个 我需要为混合模式 dll 编写代码的示例。 我也想要 不必使用 directx 标头编译混合模式 dll,因此 有一种方法可以将 'C' LPDIRECT3DSURFACE 转换为通用的 指针? 在 C# 中我只是使用 IntPtr 。

I hope someone can help me with this, I'm mostly a C# developer so my
C and C++ skills are bad. I have a native C dll that is a plugin of a
larger application. I cross compile this dll for windows on linux
using gcc.

In the native dll when I create a D3DSurface I want to call a function
in a Mixed Mode C++ dll and pass in the pointer to the surface along
with a Hwnd/handle. That Mixed Mode C++ should then call my C#
managed code.

As an example, in C I want to do the following;

Hwnd handle;
LPDIRECT3DSURFACE d3dtarg;
SurfaceCreated(handle, d3dtarg);

In C# I want this called from the mixed mode assembly

public static class D3DInterop
{
    public static void SurfaceCreated(IntPtr handle, IntPtr surface)
    {
        //do work
    }
}

Since I suck at C++, I just want to know if someone can give me an
example of what I need to code for the mixed mode dll. I'd also like
to not have to compile the mixed mode dll with directx headers, so is
there a way I can cast the 'C' LPDIRECT3DSURFACE into a generic
pointer? In C# I just use the IntPtr anyway.

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

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

发布评论

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

评论(3

◇流星雨 2024-07-15 14:39:11

创建一个托管 C++ (C++/CLI) DLL 项目,该项目可从 C 调用,并且还能够引用其他 .Net 程序集(即您的 C# 事物)。 然后,所有 C++/CLI 桥接器所需要做的就是将数据类型从 HWND 转换为 IntPtr 等。

Create a managed C++ (C++/CLI) DLL project which will be callable from C and will also be able to reference other .Net assemblies (namely your C# thing). Then all your C++/CLI bridge would have to do is translate the data types from HWND to IntPtr, etc.

你与昨日 2024-07-15 14:39:11

您是否研究过Microsoft XNA? 据说它已经管理了 DirectX 的包装器。

Have you looked into Microsoft XNA? It supposedly has managed wrappers for DirectX.

妄司 2024-07-15 14:39:11

您可以在混合模式 DLL 中使用 void *。 存在从指向任何内容的指针(包括指向 IDirect3DSurface 的指针)到 void * 的隐式转换。 然后,您可以将该指针强制转换为 IntPtr

You can use void * in the mixed-mode DLL. There is an implicit cast from a pointer to anything (including a pointer to IDirect3DSurface) to void *. You can then cast that pointer to IntPtr.

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