During the development of the wince project, you often need to write an AP to obtain information such as the battery power and power source, because the underlying battery driver of Wince generally obtains the battery state by querying and then updates it to a struct, the AP can call getsystempowerstatusex2 to obtain the value of this struct, to update battery information in real time, the AP must frequently call functions to obtain data updates.
In fact, the power management of Wince has integrated a sort y mechanism, which will send a reminder when the battery information changes.
The requestpowerconfigurications function can be used by the AP to request to receive such reminder services.
Before calling this API, the AP must create a message queue, which can be implemented using createmsgqueue.
You can use waitforsingleobject to receive notifications. This function waits until it receives a notification from the power management, then, the AP can read the data in the message queue to determine the changes to the specific power supply system, and then perform related tasks such as updating the UI display.
Reference source code:
//###################################### ###########################
# Include <pm. h>
# Define queue_entries 3
# Define max_namelen 200
# Define queue_size (queue_entries * (sizeof (power_broadcast) + max_namelen ))
Handle hmsgq;
DWORD winapi powerchangelisten (void * temp_p)
{
Uchar Buf [queue_size];
Unsigned long nread = 0, flags = 0, Res = 0;
While (1)
{
DWORD dwres = waitforsingleobject (hmsgq, infinite );
If (dwres = wait_object_0)
{
Memset (& Buf, 0, queue_size );
If (readmsgqueue (hmsgq, & Buf, queue_size, & nread, infinite, & flags ))
{
Ppower_broadcast Pb = (ppower_broadcast) & Buf;
Ppower_broadcast_power_info ppbpi = (ppower_broadcast_power_info) Pb-> systempowerstate;
If (Pb-> message = pbt_powerinfochange)
{
// Handle some changes in battery information.
// MessageBox (null, l "battery info change", null, null );
Nkdbuplintfw (L "[Fred] Battery info change batterylifepercent = % d/R/N", ppbpi-> bbatterylifepercent );
}
If (Pb-> message = pbt_powerstatuschange)
{
// Handle some changes in the power input status (AC/battery) here
// MessageBox (null, l "power input change", null, null );
Nkdbuplintfw (L "[Fred] power input change ACIN = % d/R/N", ppbpi-> baclinestatus );
}
}
}
}
}
Void init_powernotify ()
{
Nkdbuplintfw (L "[Fred] init_power1_y ++/R/N ");
Msgqueueoptions Options = {0 };
DWORD dwerr;
Options. dwsize = sizeof (msgqueueoptions );
Options. dwflags = 0;
Options. dwmaxmessages = queue_entries;
Options. cbmaxmessage = sizeof (power_broadcast) + max_namelen;
Options. breadaccess = true;
Hmsgq = createmsgqueue (null, & options );
If (! Hmsgq)
{
Dwerr = getlasterror ();
Nkdbuplintfw (L "[Fred] createmsgqueue failed/R/N ");
Retailmsg (1, (text ("[Fred] createmessagequeue error: % d/N"), dwerr ));
Return;
}
Handle hnotifications = requestpowernotifications (hmsgq, power_policy_all); // flags
If (! Hnotifications ){
Dwerr = getlasterror ();
Retailmsg (1, (text ("[Fred] requestpowerconfigurications error: % d/N"), dwerr ));
Stoppowerconfigurications (hmsgq );
Return;
}
Createthread (null, 0, powerchangelisten, null, 0, null );
Nkdbuplintfw (L "[Fred] init_powernotify --/R/N ");
}
//###################################### ######################################## #################
The AP can copy all the above Code to its source code, and then call init_powernotify once during initialization. Then, the AP can wait for the message to occur (Chinese comments)