Initializing and Un-initializing RAPI

< BACK  NEXT >
[oR]

RAPI can be initialized using either the CeRapiInit or CeRapiInitEx functions. CeRapiInit will block (that is, not return) until a connection is made to the Windows CE device. This could result in your application being frozen for a long period of time, so it is usual to create a thread and call CeRapiInit on that thread. Note that the function will never return if a Windows CE device never connects. A call to CeRapiInit is very simple since the function takes no arguments and returns an HRESULT.

 HRESULT hr; hr = CeRapiInit(); if(FAILED(hr))   cout    "Could not initialize RAPI:"          GetLastError()   endl; 

Remember that an HRESULT is not a handle but rather a 32-bit value that contains error information. The SUCCEEDED macro should be used to test for success, and FAILED to test for failure.

CeRapiInitEx is passed a RAPIINIT structure and returns an event handle in the heRapiInit member of that structure. This event handle can be passed to WaitForSingleObject. The event will be signaled when a Windows CE device connects, and this will unblock the call to WaitForSingleObject. You can pass a timeout value in milliseconds to WaitForSingleObject to limit the amount of time to wait for a connection.

 RAPIINIT rapiInit; HRESULT hr; DWORD dwWaitResult; rapiInit.cbSize = sizeof(RAPIINIT); hr = CeRapiInitEx(&rapiInit); if(FAILED(hr)) {   cout   "Could not initialize RAPI:"          GetLastError()  endl;   return; } dwWaitResult = WaitForSingleObject(rapiInit.heRapiInit,     10000); if(dwWaitResult == WAIT_FAILED) {   cout   "Could not wait on event:"          GetLastError()   endl;   return; } if(dwWaitResult == WAIT_TIMEOUT) {   cout   "Could not connect to Windows CE Device"          endl;   return; } if(FAILED(rapiInit.hrRapiInit))   // Report RAPI error 

In the above code the cbSize of the RAPIINIT structure is initialized with the size of the structure and passed to CeRapiInitEx. The handle to the event in the member heRapiInit is passed to WaitForSingleObject with a timeout of ten seconds. If WaitForSingleObject returns WAIT_FAILED, the actual error code is determined through a call to GetLastError. The return value WAIT_TIMEOUT indicates that a device did not connect within ten seconds. You can refer to Chapter 7 for more information on waiting for events.

On a successful return from WaitForSingleObject, it is important to test the hrRapiInit member of the RAPIINIT structure to see whether the connection to the Windows CE device succeeded or failed. The next section in this chapter shows how to report RAPI errors.

The function CeRapiUninit is used to un-initialize RAPI. This function takes no arguments and returns an HRESULT.


< BACK  NEXT >


Windows CE 3. 0 Application Programming
Windows CE 3.0: Application Programming (Prentice Hall Series on Microsoft Technologies)
ISBN: 0130255920
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net