Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am new to c++ and not able to find any help for my requirement.
I have a function in dll which is continuously checking for one specific hardware connectivity in separate thread. I want to notify client (who is consuming the dll) if hardware status is changed (removed/connected). I need and simple example which I can replicate in my scenario.

Please see if somebody can help.

Thanks.

What I have tried:

Somebody suggested to implement callback but I am not able to find simple example which I can understand and implement here.
Posted
Updated 8-Mar-16 21:21pm
v2

Using a callback function is the common way.

Searching the net for "c++ callback dll example" gives you enough results. askyb.com » Visual C++ Callback Function Example[^] is a short and simple explanation.

But there is one point to observe when using callback functions with DLLs (which is not mentioned in the above link):
A DLL may be used by multiple applications at the same time including multiple instances of the same (your) program. The DLL must be able to handle this by providing a register function to add a new callback function, an unregister function to remove it, and loop through all registered ones and call them upon the event.

So you need to

  • Understand C/C++ callback functions
  • Define the callback function (return value and parameter types) using a typedef
  • In the DLL add a list variable to hold the callback function pointers
  • In the DLL implement the register and unregister functions
  • In the DLL call the registered functions upon the event
  • In the app implement the static callback function
  • In the app call the register and unregister functions (e.g. at program start and exit)


Because callback functions are static, they can be class members but have no access to other (non-static) class members. But you usually want to pass the event from the callback function to a class of your application. A solution to this problem is passing a pointer to this class (or even a void pointer which is more flexible) as parameter. This requires that this pointer is also passed to the register function and stored in the list.

Short example for the public functions of the DLL in the header file:
C++
# ifdef CALLBACKPROC_EXPORTS
#  define CALLBACKPROC_API __declspec(dllexport)
# else
#  define CALLBACKPROC_API __declspec(dllimport)
# endif
typedef void (CALLBACK * callBackFnType)(void *p, int event);

extern "C"
{
	CALLBACKPROC_API void RegisterCallback(callBackFnType func, void *p);
	CALLBACKPROC_API void UnregisterCallback(callBackFnType func);
}

// Struct to store registered function and parameter in a list
typedef struct {
    callBackFnType func;
    void *p;
} CallBackItem;
 
Share this answer
 
v2
Comments
Arthur V. Ratz 20-Mar-16 11:03am    
If you're not just using WinAPI, but also would use MFC, you can derive a class from CCmdTarget and the use message map macros to handle messages to your class.
This great article about callbacks give you a deep insight look about this complicated issue.

It is a very powerful tools so it isnt easy to understand.
 
Share this answer
 
Comments
Arthur V. Ratz 21-Mar-16 2:12am    
I also agree with you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900