如何通过 C++ VB.NET 程序的回调函数?

发布于 2024-12-08 03:57:31 字数 1070 浏览 0 评论 0原文

我正在尝试将回调函数从 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 Messagebox 为例。

这是我当前的 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

我想我错过了一些东西??! 任何帮助将不胜感激!

I'm trying to pass a callback function from a C++ dll to a VB.NET application.
Here is my current C++ code :

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);
    }

This is working inside the C++ dll (the messagebox is called with the correct text)
But i'd like to "trigger" the test procedure in the VB application with the text to use the VB Messagebox for example.

Here is my current VB.NET code :

 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

I think i'm missing something ??!
Any help will be appreciated !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2024-12-15 03:57:31

C++ 代码必须具有 extern C 链接,否则 VB 代码将无法看到它。将相关声明包含在标头中

extern "C" {
    // declarations here
}

The C++ code must have extern C linkage or the VB code won't be able to see it. Wrap the relevant declarations in the header with

extern "C" {
    // declarations here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文