none
CLI中,如何把C#的委托交给C++的回调函数? RRS feed

  • 问题

  • 在CLI中,可以辨认出C#的托管函数,也可以辨认出C++的函数指针,但是,我要如何把C#传来的委托交给C++呢?

    我知道如果直接DllImport的话,可以把函数参数中的C++函数指针类型,写为托管函数类型,就可以成功扔回调:

    例如:

    C++导出函数:

    typedef void(__stdcall *pfnCallback)();

    void SetCallback(pfnCallback callback);

    C#声明:

    [UnmanagedFunctionPointerAttribute(CallingConvention.StdCall, CharSet = CharSet.Unicode)]
    public delegate int pfnCallback(int result);

    [DllImport("xxxx.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Winapi)]
    internal static extern void SetCallback(pfnCallback callback);

    那么,如果在CLI中。

    C#扔过来的函数如何交给C++代码呢?

    希望能获得解答,谢谢

    2019年8月4日 3:09

答案

  • Hi   8008208820Hell,

    我发现你已经在C++中文论坛提出了同样的问题。请在那边等待回复。谢谢。

    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月6日 1:39
    版主

全部回复


  • Hi  8008208820Hell,

    据我了解,在NET 中已经实现了CLR规范。

     >> C#扔过来的函数如何交给C++代码呢?

    这个你可能需要自己考虑在C++中怎么访问NET的库。

    使用 C++/CLI (Visual C++) 进行 .NET 编程:

    有关C++的问题,可以到Visual C++ forum

    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月5日 6:50
    版主

  • Hi  8008208820Hell,

    据我了解,在NET 中已经实现了CLR规范。

     >> C#扔过来的函数如何交给C++代码呢?

    这个你可能需要自己考虑在C++中怎么访问NET的库。

    使用 C++/CLI (Visual C++) 进行 .NET 编程:

    有关C++的问题,可以到Visual C++ forum

    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    可能我描述的过于复杂了,简单来说,就是

    [System::Runtime::InteropServices::UnmanagedFunctionPointerAttribute(System::Runtime::InteropServices::CallingConvention::StdCall, CharSet = System::Runtime::InteropServices::CharSet::Unicode)]
    public delegate int pfnCallback(int Result);

    我需要将这个类型的变量赋值给

    typedef int(__stdcall* pfnNativeCallback)(int Result);

    //伪代码

    void SetCallback(pfnCallback^ callback)

    {

           pfnNativeCallback pNativeCallback = callback; //我要怎样才能实现这样的操作?

    }

    期待您的回复

    我看了您给的 《使用 C++/CLI (Visual C++) 进行 .NET 编程:》这里面写的要进行这样的操作,必须封装dll,我想知道能否不封装dll,因为如果封装dll,C++/CLI的意义就不存在了

    2019年8月5日 13:31
  • Hi   8008208820Hell,

    我发现你已经在C++中文论坛提出了同样的问题。请在那边等待回复。谢谢。

    Best regards

    Yong Lu

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2019年8月6日 1:39
    版主