Encapsulate native DLL callback functions in Windows Mobile and Windows WinCE (Windows Embedded CE)

Source: Internet
Author: User
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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.