extern"C" __declspec(dllexport) BOOL OEMISO15693UID(void (*pUIDCallBack)(char*));
这个是DLL头文件的声明。
void (*pUIDCallBack)(char*)
这个是DLL中声明的回调函数。
我在C#中要调用这个函数,但是没有遇到过以回调函数作为参数的。看了网上的好多帖子,都说要用到delegate。
但是没看懂,希望高手指点。

解决方案 »

  1.   

    完整例子:
    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" );
      

  2.   

    Registering Callback MethodsTo 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
      

  3.   

    你的FPTR2是回调函数?TestCallBack2是以回调函数FPTR2为参数的DLL函数?
      

  4.   

    是的,FPTR2就是回调函数,在C#对应为委托!
      

  5.   

    void (*pUIDCallBack)(char*)
    我要利用这个回调函数获取char的值,可是你们提供的方法最后怎么回调函数的参数都没了。。
      

  6.   

    我这个回调函数是又参数的,void (*pUIDCallBack)(char*)
    char*,我其实就像获取这个值,但是你像你的例子这样儿,FPTR2 pf2
    就把这个回调函数实例化了啊,那里面的参数就取不到了啊。
      

  7.   

    还漏了一个代码:
       public static bool DoSomething2( String value )
       {
          Console.WriteLine( "\nCallback called with param: {0}", value );
          …
       }