Rapid detection of PocketPC how it has been connected to a PC
If in the process of testing PocketPC (hereinafter referred PPC) it has been connected to the PC. PocketPC SDK provides a set RAPI function, through which we can CeRapiInit or CeRapiInitEx to detect.
First, CeRapiInit, which is defined as:
HRESULT CeRapiInit (void);
This function call up some relatively simple, need only test their return value can be. But when the PPC and is not connected to a PC, have been waiting for this function will not return, that is to say to the current deadlock of the thread. Wait until the PPC and PC connectivity successful return to a time before S_OK value.
Suppose a process must be known PPC and PC link the success of the implementation of certain operating circumstances, if already connect, can be easily handled and CeRapiInit return S_OK; If there is no connection, then CeRapiInit have been waiting for, and procedures that users will die.
Fortunately, we have another function CeRapiInitEx, the function prototype is:
HRESULT CeRapiInitEx (pRapiInit RAPIINIT *);
CeRapiInit function and the function is, the difference is that it will return immediately, but the return value is not representative of the PPC and PC connectivity. We need to write additional code to detect whether a successful connection. Please note that this function requires a parameter RAPIINIT * pRapiInit, RAPIINIT defined as follows:
Typedef struct _RAPIINIT (
DWORD cbSize; / / RAPIINIT the size of the structure
HANDLE heRapiInit; / / Event of a Handle
HRESULT hrRapiInit; / / connection is a successful return
RAPIINIT);
MSDN official MsgWaitForMultipleObjects function is used to monitor the structure heRapiInit variables in the experiment that can be used WaitForSingleObject completion of the operation, and call up more convenient WaitForSingleObject:)
Sample code is as follows:
/ / Definition of variables RAPIINIT
RAPIINIT ri;
/ / Size of the variables given cbSize parameters, the Windows SDK programming is commonly used in the operation
/ / I do not know, please check the relevant information
Ri.cbSize = sizeof (RAPIINIT);
/ / Call CeRapiInitEx function, the function to return immediately
/ / Note that the return value is not representative of PPC and the connection status of PC
HRESULT hInitResult = CeRapiInitEx (& ri);
/ / Here to WaitForSingleObject to track heRapiInit this Event
DWORD dwWaitResult = WaitForSingleObject (ri.heRapiInit, 2000);
/ / Test for the successful connection
If (S_OK hInitResult == == S_OK ri.hrRapiInit & & & & dwWaitResult! = WAIT_TIMEOUT)
(
/ / Connect success
…
)
Else
(
/ / Connect unsuccessful
…
)
Well, here code has been finished, we should pay attention to:
This detection process is not standardized, but there will not be much problem, the key lies in the final WaitForSingleObject the value of a parameter how to set up, this is an experience that is value. According to my experience, if the PPC has been connected to the PC, then the process will be detected in less than one second to complete, if not connected, the longest waiting time will not be more than two seconds.
Well, written for the first time development, the limited level, we hope that the correction.








0 Comments to “Rapid detection of PocketPC how it has been connected to a PC”
No Comments. Send your comment.
Leave a Reply
You must be logged in to post a comment.