Background
I have written some native DLL-encapsulatedArticle, The link is as follows:
How to encapsulate native DLL provided to. NET Compact framework in Windows Mobile and WinCE (Windows Embedded CE) for calling
Encapsulation of native DLL in Windows Mobile and Windows WinCE (Windows Embedded CE)
How to use native C ++ to dynamically load DLL in Windows Mobile
Reason
Sometimes the encapsulated DLL needs to provide asynchronous notifications to the client and use the callback function. If it is not a pure c dll, asynchronous notifications will be sent to the client in oberver mode. However, this method cannot be used when native DLL of pure C is encapsulated. For more information about the oberver mode, see my practical design mode-Observer Mode
Solution
The following uses an LCD touch screen temperature and the encapsulation of the touch callback function as an example to describe how to encapsulate the callback function.
Definition
# Ifdef LCD _exports
# Define LCD _api Extern "C" _ Declspec ( Dllexport )
# Else
# Define LCD _api Extern "C" _ Declspec ( Dllimport )
# Endif
/**
* Function signature for callback event handlers.
*/
Typedef void ( _ Stdcall * Lcdtemperaturehandler )( Double Degree );
Typedef void ( _ Stdcall * Lcdtouchsensorhandler )();
LCD _api Void Lcdtemperatureregisterhandler (lcdtemperaturehandler handler );
LCD _api Void Lcdtouchsensorregisterhandler (lcdtouchsensorhandler handler );
LCD _api Void Lcdtemperatureunregisterhandler ();
LCD _api Void Lcdtouchsensorunregisterhandler ();
Define the function pointers of the temperature and touch callback functions: lcdtemperaturehandler and lcdtouchsensorhandler. When the lcdtemperaturehandler calls back, it also transmits the temperature value (degree ). Lcdtemperatureregisterhandler and lcdtouchsensorregisterhandler are the registration functions. The parameters are: lcdtemperaturehandler and lcdtouchsensorhandler are the original global functions or static member functions.
Implementation
Lcdtemperaturehandler glcdtemperaturehandler = NULL;
Lcdtouchsensorhandler glcdtouchsensorhandler = NULL;
VoidLcdtemperatureregisterhandler (lcdtemperaturehandler handler)
{
Glcdtemperaturehandler = handler;
}
VoidLcdtouchsensorregisterhandler (lcdtouchsensorhandler handler)
{
Glcdtouchsensorhandler = handler;
}
VoidLcdtemperatureunregisterhandler ()
{
Glcdtemperaturehandler = NULL;
}
VoidLcdtouchsensorunregisterhandler ()
{
Glcdtouchsensorhandler = NULL;
}
A global variable or static member variable is required to save the function pointer.
VoidLcdtouched ()
{
If(Glcdtouchsensorhandler! = NULL)
{
Glcdtouchsensorhandler ();
}
}
VoidLcdtemperaturechanged ()
{
DoubleTemperature = 30.2;
If(Glcdtemperaturehandler! = NULL)
{
Glcdtemperaturehandler (temperature );
}
}
Call these function pointers to call the registered functions.
Client
When using the client, you must define the same functions as lcdtemperaturehandler and lcdtouchsensorhandler.
VoidOnlcdtouched ()
{
Printf ("Onlcdouched \ n");
}
VoidOntemperaturechanged (DoubleDegree)
{
Printf ("Ontemperaturechanged degree = % d \ n", Degree );
}
Pass these functions as parameters to the registration function.
Lcdtouchsensorregisterhandler (onlcdtouched );
Lcdtemperatureregisterhandler (ontemperaturechanged );