C#调用C/C++,的Dll 包括指针,回调函数。

//xxx.h
typedef BOOL (CALLBACK* MYNOTIFY)( HANDLE h );
FXCALL_API HANDLE InitMd( LPCWSTR MdName, MYNOTIFY myNotify );
//VC实例调用为:#define H_Init() InitMd(L"loadSomething.txt",null)
请问:C#中应该怎样声明和调用?上面调用时回调函数为空,C#中请给出一个不为空的
示例(好像是用delegate)谢谢。。。

第1个回答  2015-12-12
[DllImport("FaceLib.dll", EntryPoint = "saveIntImage", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]public unsafe static extern int saveIntImage(string filename, IntImage* intImg);[StructLayout(LayoutKind.Sequential)]public unsafe struct IntImage{ public int height; public int width; public int** data; public int* buf; public int variance; public int label; public int bFlip; ~IntImage() { if (data == (int**) 0) Marshal.FreeCoTaskMem(new IntPtr(data)); if (buf == (int*) 0) Marshal.FreeCoTaskMem(new IntPtr(buf)); }}public static void Convert_to_IntImage2(ref IntImage intImage, Bitmap bmp){ intImage.height = bmp.Height; intImage.width = bmp.Width; Color c = new Color(); int size = (intImage.width) * (intImage.height); byte[] buffer = new byte[size]; int m = 0; for (int i = 0; i < bmp.Width; i++) for (int j = 0; j < bmp.Height; j++) { c = bmp.GetPixel(i, j); buffer[m] = (byte)((39 * c.R + 11 * c.G + 50 * c.B) / 100); m++; } unsafe { intImage.buf = (int*) Marshal.AllocCoTaskMem(size * sizeof(int)).ToPointer(); int* pDst = intImage.buf; fixed (byte* pBuf = &buffer[0]) { for (int i = 0; i < size; ++i) pDst[i] = pBuf[i]; } intImage.data = (int**)Marshal.AllocCoTaskMem(bmp.Width * sizeof(int*)).ToPointer(); int** pData = intImage.data; int bmpHeight = bmp.Height; for (int i = 0; i < bmp.Width; i++) { pData[i] = &pDst[i * bmpHeight]; } }
第2个回答  推荐于2016-08-20
声明一个delegate,然后把相应型构的函数传进去就行了;
public delegate void CallBackFunc();
public void xx()
{
console.writeln("call back");
}
private CallBackFunc func;//func=new CallBackFunc(xx);
导入dll的函数后,调用:
RegReLoginSuccessCallBackFunc(func);
第3个回答  推荐于2016-05-06
delegate Boolean MyNotify(IntPtr h);
IntPtr InitModule(String moduleName, MyNotify myNotify);
// InitModule("loadSomething.txt", null);追问

正解,谢谢;
我还想问一下,Dll中还有一个函数:
int ctrlFun(HANDLE Md, const void* StructParam); 其中第二个参数传入的是任意结构体;
其一结构体:
struct StuFunA

{
int a;

int b;

};
//VC实例: StuFun stFunA = {1, 5}; int relt = ctrlFun(hdModule, &stFun);
请问C#中该如何声明ctrFun 并传入参数? 谢谢...

本回答被提问者采纳
第4个回答  2015-11-28
c#没用过,不过你可以用c++写一个dll再内嵌一点汇编就可以实现以上功能了
相似回答