在外部 C++ Win32 应用程序中共享导出的 C++ DLL 事件

To share an exported C++ DLL event, within an external C++ Win32 Application

祝你有个愉快的一天!

出于可移植性原因,我仅通过搜索数千页、数百个编译错误更正和几个 Whosebug 问题就创建了一个几乎具有新手 C++ 知识的 C++ DLL。

它远不止稳定,但大部分时间都在工作:)

出于好奇和模块化的原因,没有将头文件和 cpp 文件导入我的应用程序,我想请教您的建议和帮助:

C++ Win32 控制台应用程序

int main()
{
    HMODULE lib = LoadLibrary(L"Serial.dll");

    typedef void(*void_Connect)(UINT Port, UINT Baud);
    void_Connect Connect = (void_Connect)GetProcAddress(lib, "Connect");

    Connect(1, 9600);
}   

我想在上面的 C++ Win32 控制台应用程序中添加一个事件,该事件将由 DLL 触发或托管

例如带参数的Received_Event(const char* data)Connected_Event(BOOL status)

来自 DLL 的部分

typedef void(*fpCALLBACK) (const char* aParam);

static fpCALLBACK s_pCallback = NULL;

extern "C"
{
    #define DLL __declspec(dllexport)

    DLL void ReceivedEventRegister(fpCALLBACK p_pCallback)
    {
        s_pCallback = p_pCallback;
    }

    DLL void evntReceived(const char *receivedData);
 }

DLL void evntReceived(const char *received)
{
    s_pCallback(received);
} 

我想用标准 C++ 实现类似于此 C# 版本的东西

private static ManagedCallbackDelegate MessageCallbackDelegate;

public delegate void ManagedCallbackDelegate(string aParam);

[DllImport("Serial.dll", EntryPoint = "ReceivedEventRegister", CallingConvention = CallingConvention.Cdecl)]
private static extern void ReceivedEventRegister(ManagedCallbackDelegate callback);

    static private void serialRecieved(string data)
    {
        Console.WriteLine(data);
    }

    static void Main(string[] args)
    {
        MessageCallbackDelegate = new ManagedCallbackDelegate(serialRecieved);
        ReceivedEventRegister(MessageCallbackDelegate);
    }

问题

  1. 你能给我一些为此目的编写的代码示例吗,你知道吗?
  2. 我可以Google吗?
  3. 有什么方法可以推荐吗?

谢谢!

再次感谢@dxiv的评论;帮了我很多,把事情解决了!甚至,当有人告诉你它应该工作时,你解决了 90% 的问题。

在DLL端

DLL void evntReceived(const char *received)
{
    s_pCallback(received);
} 

DLL一收到数据就一直调用上面的函数。要在您的 Win32 C++ 应用程序中实时获取该数据,您需要导入调用上述回调函数的 DLL 函数。这是哪个

DLL void ReceivedEventRegister(fpCALLBACK p_pCallback)

DLL 的一部分。

正如 DLL 函数的参数类型名称 fpCALLBACK 所暗示的,我们的函数参数必须与 DLL 中定义的一样:

typedef void(*fpCALLBACK) (const char* aParam);

在应用端

//Callback Function: A function that is passed to another function as an argument
void Event_Received(const char *recvd)
{
    std::cout << recvd << std::endl;
}

int main()
{
    HMODULE lib = LoadLibrary(L"Serial.dll");

    typedef void(*void_Connect)(UINT Port, UINT Baud);
    void_Connect Connect = (void_Connect)GetProcAddress(lib, "Connect");

// Type definition of the function pointer. 
// In our case: a Void type, which takes in a 
// void type function pointer with a parameter (const char *aParam)
// dllCALLBACK is just a type name; you can name it whatever you want.
    typedef void(*dllCALLBACK) (void(*fpCALLBACK) (const char* aParam));

// Now we will import the function from our DLL which takes     
// the same parameters as we defined above, to call  
// our callback function
dllCALLBACK ReceivedEventRegister = (dllCALLBACK)GetProcAddress(lib, "ReceivedEventRegister");

// Call the callback function with the imported function above.
// And that is all there is to it. As soon as any data received
// by our DLL - our Event_Received callback function 
// fires in our C++ Win32 Console Application
ReceivedEventRegister(Event_Received);

    Connect(1, 9600);
}   

如果你和我一样是新手,这篇文章video可能会帮助你了解回调函数。

再次感谢@dxiv,抽空阅读并回答我的问题!