How to quickly detect if PocketPC is connected to your PC

Source: Internet
Author: User

If you detect PocketPC (PPC) in your program, you are already connected to your PC. The PocketPC SDK provides a set of RAPI functions that we can detect through Cerapiinit or Cerapiinitex.

First say cerapiinit, it is defined as:

HRESULT cerapiinit (void);

This function is simpler to call, and only needs to be checked for its return value. But when the PPC is not connected to the PC, the function waits without returning, which means that the current thread is deadlocked. You will not return a S_OK value until the PPC and PC are connected successfully.

Suppose a program must perform some operation if it is known that the PPC is connected to the PC, and if it is connected, then the Cerapiinit returns to S_OK; if there is no connection, then Cerapiinit waits and the user thinks the program is dead.

Fortunately we have another function Cerapiinitex, the prototype of this function is:

HRESULT Cerapiinitex (Rapiinit *prapiinit);

The function is equivalent to Cerapiinit, except that it returns immediately, but the return value does not represent the connection between the PPC and the PC. We also need to write extra code to detect if the connection is successful. Note that the function needs to have a parameter Rapiinit *prapiinit, where rapiinit is defined as follows:

typedef struct _RAPIINIT {
DWORD cbSize;    // RAPIINIT结构的大小
HANDLE heRapiInit;  // 一个Event的Handle
HRESULT hrRapiInit; // 返回连接是否建立成功
} RAPIINIT;

The official MSDN practice is to use the msgwaitformultipleobjects function to monitor the herapiinit variables in the structure, and the experiment proves that the operation can also be done with WaitForSingleObject. And WaitForSingleObject is more convenient to call:)

The sample code is as follows:

// 定义RAPIINIT结构变量
RAPIINIT ri;
// 将该变量的大小赋予cbSize参数,这个是Windows SDK编程中常用的操作
// 不知者请查阅相关资料
ri.cbSize = sizeof(RAPIINIT);
// 调用CeRapiInitEx函数,该函数立即返回
// 注意,其返回值并不代表PPC与PC的连接状态
HRESULT hInitResult=CeRapiInitEx(&ri);
// 这里用WaitForSingleObject来跟踪heRapiInit这个Event
DWORD dwWaitResult=WaitForSingleObject(ri.heRapiInit,2000);
// 检测是否连接成功
if(hInitResult==S_OK && ri.hrRapiInit==S_OK && dwWaitResult!=WAIT_TIMEOUT)
{
   // 连接成功
   ...
}
else
{
   // 连接不成功
   ...
}

Okay, here's the code, and note:

The detection process is not standard, but there is no big problem, the key is to waitforsingleobject the value of the last parameter set, which means that this is an empirical value. According to my experience, if the PPC is connected to the PC, the detection process will be completed in less than 1 seconds, and if there is no connection, the maximum wait time will not exceed 2 seconds.

Well, the first time to write development, the level is limited, I hope you correct me.

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.