问题
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.
回答1:
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.
回答2:
Have you looked into Microsoft XNA? It supposedly has managed wrappers for DirectX.
回答3:
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
.
来源:https://stackoverflow.com/questions/267057/creating-a-mixed-mode-c-bridge-from-c-to-c