【问题标题】:How to pass a C++ callback function to a VB.NET program?如何将 C++ 回调函数传递给 VB.NET 程序?
【发布时间】:2011-11-29 00:13:37
【问题描述】:

我正在尝试将回调函数从 C++ dll 传递到 VB.NET 应用程序。 这是我当前的 C++ 代码:

void DLL_EXPORT registerEvent( void (*callBackFunction)(string str),string str) 
    {
        callBackFunction(str);
    }

void  test(string str)
    {
      MessageBoxA(0,str.c_str(), "",MB_OK);
    }

LRESULT CALLBACK keyHandler(int nCode, WPARAM wParam, LPARAM lParam)

    {
      ...
     registerEvent(&test, txt); //txt = the text received after user input with some additions
     return CallNextHookEx(hookHandle, nCode,
        wParam, lParam);
    }

这是在 C++ dll 中工作的(使用正确的文本调用消息框) 但我想用文本“触发”VB 应用程序中的测试过程,以使用 VB 消息框为例。

这是我当前的 VB.NET 代码:

 Public Delegate Sub Callback(ByVal str As String)

 Private Declare Sub registerEvent Lib "path\mycppdll.dll" _
    (ByVal cb As Callback, ByVal str As String)

 Dim cb As New Callback(AddressOf CallBackFunc)
 Public Sub CallBackFunc(ByVal str As String)
  'this should be the equivalent of the "test" proc in c++ but it's not triggered

 End Sub

我想我错过了什么??! 任何帮助将不胜感激!

【问题讨论】:

    标签: c++ vb.net delegates callback


    【解决方案1】:

    C++ 代码必须有extern C 链接,否则VB 代码将看不到它。用

    将相关声明包装在标头中
    extern "C" {
        // declarations here
    }
    

    【讨论】:

    • 感谢 Ernest 的回答,但我已经使用了 extern C 并且没有任何改变:/
    猜你喜欢
    • 1970-01-01
    • 2013-04-12
    • 2019-03-20
    • 2013-06-06
    • 2021-02-23
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    相关资源
    最近更新 更多