当前位置:编程学习 > C#/ASP.NET >>

C#如何用delegate调用C++的DLL中以回调函数为参数的函数(比较拗口上代码。)

extern"C" __declspec(dllexport) BOOL OEMISO15693UID(void (*pUIDCallBack)(char*));
这个是DLL头文件的声明。
void (*pUIDCallBack)(char*)
这个是DLL中声明的回调函数。
我在C#中要调用这个函数,但是没有遇到过以回调函数作为参数的。看了网上的好多帖子,都说要用到delegate。
但是没看懂,希望高手指点。 --------------------编程问答-------------------- 貌似不行吧!帮顶一下! --------------------编程问答-------------------- 完整例子:
C++:
void TestCallBack2(FPTR2 pf2, char* value);

C#:
public delegate bool FPtr2( String value );

   [ DllImport( "..\\LIB\\PinvokeLib.dll" )]
   public static extern void TestCallBack2( FPtr2 cb2, String value );  

   FPtr2 cb2 = new FPtr2( App.DoSomething2 );
   TestCallBack2( cb2, "abc" );

--------------------编程问答-------------------- Registering Callback Methods

To register a managed callback that calls an unmanaged function, declare a delegate with the same argument list and pass an instance of it via PInvoke. On the unmanaged side it will appear as a function pointer. For more information about PInvoke and callback, see A Closer Look at Platform Invoke.

For example, consider the following unmanaged function, MyFunction, which requires callback as one of the arguments:

typedef void (__stdcall *PFN_MYCALLBACK)();
int __stdcall MyFunction(PFN_ MYCALLBACK callback);
To call MyFunction from managed code, declare the delegate, attach DllImport to the function declaration, and optionally marshal any parameters or the return value:

public delegate void MyCallback();
[DllImport("MYDLL.DLL")]
public static extern void MyFunction(MyCallback callback);
Also, make sure the lifetime of the delegate instance covers the lifetime of the unmanaged code; otherwise, the delegate will not be available after it is garbage-collected.

来自:
Registering Callback Methods小节
http://msdn.microsoft.com/en-us/library/aa288468(v=vs.71).aspx#pinvoke_registeringcallback --------------------编程问答-------------------- 帮顶一下! --------------------编程问答--------------------
引用 2 楼 sdl2005lyx 的回复:
完整例子:
C++:
void TestCallBack2(FPTR2 pf2, char* value);

C#:
public delegate bool FPtr2( String value );

   [ DllImport( "..\\LIB\\PinvokeLib.dll" )]
   public static extern void TestCallBack……

你的FPTR2是回调函数?TestCallBack2是以回调函数FPTR2为参数的DLL函数? --------------------编程问答-------------------- 是的,FPTR2就是回调函数,在C#对应为委托! --------------------编程问答--------------------
引用 3 楼 findcaiyzh 的回复:
Registering Callback Methods

To register a managed callback that calls an unmanaged function, declare a delegate with the same argument list and pass an instance of it via PInvoke. On the unmanage……

void (*pUIDCallBack)(char*)
我要利用这个回调函数获取char的值,可是你们提供的方法最后怎么回调函数的参数都没了。。。。。 --------------------编程问答--------------------
引用 6 楼 sdl2005lyx 的回复:
是的,FPTR2就是回调函数,在C#对应为委托!

多谢了,不管管用不,分送你了~ --------------------编程问答--------------------
引用 2 楼 sdl2005lyx 的回复:
完整例子:
C++:
void TestCallBack2(FPTR2 pf2, char* value);

C#:
public delegate bool FPtr2( String value );

   [ DllImport( "..\\LIB\\PinvokeLib.dll" )]
   public static extern void TestCallBack……

我这个回调函数是又参数的,void (*pUIDCallBack)(char*)
char*,我其实就像获取这个值,但是你像你的例子这样儿,FPTR2 pf2
就把这个回调函数实例化了啊,那里面的参数就取不到了啊。 --------------------编程问答-------------------- 还漏了一个代码:
   public static bool DoSomething2( String value )
   {
      Console.WriteLine( "\nCallback called with param: {0}", value );
      …
   }   
--------------------编程问答-------------------- 哈哈、我知道、前几天才碰到这个问题
以回调函数类型为参数的函数、是用来注册回调函数的函数、确实绕口
委托是要委托回调函数、而不是委托注册函数
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,