WinCE下訊息佇列用法MsgQueue

2021-05-05 17:39:36 字數 3024 閱讀 4745

動和ap之間互相通訊有很多中方法,比如自定義event然後ap通過等待event的方式來和driver同步。但是很多標準的事件,比如電源狀況的改變,sd卡插入等等通用的訊息在ce的內部是早有預留的。微軟透過訊息佇列的形式來告訴需要獲知相關訊息的ap來做處理,我們僅僅需要事先去了解系統有哪些訊息佇列,然後在ap中寫**去捕捉就好了,本文就以獲知電池電量改變為例,講講訊息佇列的用法。

在wince的專案開發過程中經常要編寫ap來獲取電池的電量和電源的**等資訊,由於wince底層的電池驅動一般以查詢的方式得到電池的狀態然後更新到乙個結構體中,ap可以呼叫getsystempowerstatu***2來得到這個結構體的數值,為了實時的更新電池的資訊ap必須頻繁的呼叫函式去得到資料更新。

其實wince的電源管理中已經整合了一種notify機制,會在電池資訊發生變化時發出提醒。

requestpowernotifications函式可以被ap用來請求接收這種提醒服務。

ap在呼叫這個api之前必須建立乙個訊息佇列,可以用createmsgqueue來實現。

接受提醒的方式是使用waitforsingleobject來實現,該函式會一直等待直到收到電源管理發來的提醒,然後ap可以去讀取訊息佇列中的資料來判定具體電源系統發生了哪些變化,然後做相關的事情比如更新ui的顯示等。

#include

#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)

//在這裡處理一些電池資訊相關資料改變的事情

// messagebox(null,l"battery info change",null,null);

nkdbgprintfw(l"[fred]battery info change batterylifepercent=%d/r/n",ppbpi->bbatterylifepercent);

if(pb->message==pbt_powerstatuschange)

//在這裡處理一些電源輸入狀態改變 (ac/battery)的事情

//messagebox(null,l"power input change",null,null);

nkdbgprintfw(l"[fred]power input change acin=%d/r/n",ppbpi->baclinestatus);

void init_powernotify()

nkdbgprintfw(l"[fred]init_powernotify++/r/n");

msgqueueoptions  options  =  ;

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

nkdbgprintfw(l"[fred]createmsgqueue failed/r/n");

retailmsg(1,  (text("[fred]createmessagequeue  error:%d/n"),  dwerr));  

return;  

handle hnotifications  =  requestpowernotifications(hmsgq, power_notify_all);  //  flags  

if  (!hnotifications)  {  

dwerr  =  getlasterror();  

retailmsg(1,  (text("[fred]requestpowernotifications  error:%d/n"),  dwerr));  

stoppowernotifications(hmsgq);

return;

createthread(null, 0, powerchangelisten, null, 0, null);

nkdbgprintfw(l"[fred]init_powernotify--/r/n");

複製**ap可以把上面的**全部複製到自己的原始碼中,然後在初始化的時候呼叫一次init_powernotify,之後就可以等待訊息的發生(中文注釋部分)

Linux 程序間通訊(1)訊息佇列 msg

2.訊息佇列介面 msgget 和 ftok 3.訊息佇列介面 msgctl 4.訊息佇列介面 msgsnd 5.訊息佇列介面 msgrcv 6.訊息佇列兩個命令 ipcs 參考 其實,就是程序間通訊。linux中的訊息佇列是程序間通訊的一種方式 通過建立乙個訊息佇列可以完成乙個或者多個程序的資訊交...

程序間通訊 訊息佇列(msg 實現自由通訊

msg 訊息佇列 是將訊息按佇列的方式組織成的鍊錶 include utili.h include include include include include include include include ser.c include utili.h define send type 100 d...

練習 LINUX程序間通訊之訊息佇列MSG

繼續堅持,或許不能深刻理解,但至少要保證有印象。訊息佇列 也叫做報文佇列 能夠克服早期unix通訊機制的一些缺點。作為早期unix通訊機制之一的訊號能夠傳送的資訊量有限,後來雖然posix 1003.1b在訊號的實時性方面作了拓廣,使得訊號在傳遞資訊量方面有了相當程度的改進,但是訊號這種通訊方式更像...