#include <Pnp.h>
//定义DEVDETAIL联合体,目的为了DEVDETAIL结构体的szName变量的长度
typedef union {
DEVDETAIL d;
char pad[sizeof(DEVDETAIL)+MAX_DEVCLASS_NAMELEN];
} MYDEV;
//检测SD卡插拔状态的线程
UINT CRequestDeviceNotifications_TestDlg::SDCheckThread(LPVOID lpParam)
{
GUID guid = {0}; // or any known and relevant device interface GUID
HANDLE hQueue, hn;
MSGQUEUEOPTIONS msgopts;
msgopts.dwSize = sizeof(MSGQUEUEOPTIONS);
msgopts.dwFlags = 0;
msgopts.dwMaxMessages = 0;
msgopts.cbMaxMessage = sizeof(MYDEV);
msgopts.bReadAccess = TRUE;
//创建消息队列
hQueue = CreateMsgQueue(NULL, &msgopts);
if (hQueue == 0)
{
return 0;
}
//注册消息队列到设备管理器
hn = RequestDeviceNotifications(&guid, hQueue, TRUE);
MYDEV devDetail;
DWORD dwFlags;
DWORD dwSize;
for(;;)
{
//等待消息队列的返回
DWORD rel = ::WaitForSingleObject(hQueue, INFINITE);
if(rel == WAIT_OBJECT_0)
{
//读消息队列的数据,这里是读出很多消息的,因为有比较多的设备
while(::ReadMsgQueue(hQueue, &devDetail, sizeof(MYDEV), &dwSize, 0, &dwFlags) == TRUE)
{
//判断是否SD卡目录,这里可以增加其他判断,这样就可以变成检测其他设备
if(TEXT("//SD Memory") == devDetail.d.szName)
{
wprintf(L"ReadMsgQueue %s/r/n",devDetail.d.szName);
}
}
}
}
return 0;
}