简体   繁体   中英

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

Have a good day!

For portability reasons, i have created a C++ DLL with almost novice C++ knowledge just by searching thousands of pages, hundreds of compiling error corrections and couple of stackoverflow questions.

It is far beyond being stable but it is working most of the time:)

Just for curiosity and modularity reasons without importing the header and the cpp file to my application, i would like to ask your advice and help:

C++ Win32 Console Application

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

I want to add an event into above C++ Win32 Console application, which will be triggered or hosted by the DLL

For example, a Received_Event(const char* data) or Connected_Event(BOOL status) with parameters.

Part from the 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);
} 

I want to achive something similar to this C# version, with Standard 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);
    }
  • With C# code above, any data received by the DLL , the serialRecieved function is called in realtime. I want to achive this with Standard C++
  • I want to be informed or be aware of with the DLL process in realtime, in my Win32 C++ Console Application. ( Without blocking the Win32 Console application )
  • No MFC. if it is possible , no Component Object Model (It is possible with C# without need to convert the DLL into a COM). if it is possible , I want to do it with Standard C++ .
  • I am compiling things with Visual Studio 2017 Community
  • Please go easy with me. It is a hobby for me and i just do it in my spare time.

Questions

  1. Can you reference me some code examples written for this purpose, you are aware of?
  2. Is there any specific name for this kind of communication that i can Google?
  3. Is there any approach, you can suggest?

Thank you!

Once again, thanks to @dxiv 's comment; helped me a lot, to sort things out, Even, when someone tells you that it should work. you resolve 90% of the problem.

At the DLL side

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

The DLL is calling the above function all the time as soon as it receives any data. To get that data realtime in your Win32 C++ Application, you need to import the DLL function which is calling the above callback function. Which is this

DLL void ReceivedEventRegister(fpCALLBACK p_pCallback)

part of the DLL.

As the DLL function's parameter type name fpCALLBACK suggests, our function parameter has to be as it is defind in the DLL:

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

At the Application side

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

If you are a novice like me this video may help you to understand about Callback functions.

Thank you @dxiv again, taking time to read and answer my question!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM