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

时间:2022-09-01 19:20:46

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.

我希望有人可以帮助我,我主要是一个C#开发人员,所以我的C和C ++技能很糟糕。我有一个原生C dll,它是一个更大的应用程序的插件。我使用gcc在linux上交叉编译这个dll for windows。

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.

在我创建D3DSurface的原生dll中,我想在混合模式C ++ dll中调用一个函数,并将指针传递给表面以及Hwnd / handle。那混合模式C ++应该调用我的C#托管代码。

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

举个例子,在C中我想做以下事情;

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

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

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

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.

由于我吮吸C ++,我只想知道是否有人能给我一个我需要为混合模式dll编码的例子。我也不想用directx头编译混合模式dll,那么有没有办法将'C'LPDIRECT3DSURFACE转换为通用指针?在C#中我只是使用IntPtr。

3 个解决方案

#1


2  

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.

创建一个托管C ++(C ++ / CLI)DLL项目,该项目可以从C调用,也可以引用其他.Net程序集(即您的C#事物)。那么你所有的C ++ / CLI桥必须做的是将数据类型从HWND转换为IntPtr等。

#2


1  

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

你看过微软XNA了吗?它应该是DirectX的托管包装器。

#3


0  

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.

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

#1


2  

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.

创建一个托管C ++(C ++ / CLI)DLL项目,该项目可以从C调用,也可以引用其他.Net程序集(即您的C#事物)。那么你所有的C ++ / CLI桥必须做的是将数据类型从HWND转换为IntPtr等。

#2


1  

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

你看过微软XNA了吗?它应该是DirectX的托管包装器。

#3


0  

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.

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