这个函数作为SetWindowsHookEx的第二个参数 大一上写的小代码
LRESULT CALLBACK GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode >= 0)
{
CWPSTRUCT *msg = (CWPSTRUCT *)lParam;
if (msg->message == WM_DEVICECHANGE)
{
if (msg->wParam == DBT_DEVICEARRIVAL)
{
PDEV_BROADCAST_HDR pDevBroadcastHdr;
PDEV_BROADCAST_VOLUME pDevBroadcastVolume;
char strVolume;
pDevBroadcastHdr = (PDEV_BROADCAST_HDR) ((DWORD)(lParam));
if (pDevBroadcastHdr->dbch_devicetype == DBT_DEVTYP_VOLUME)
{
pDevBroadcastVolume = (PDEV_BROADCAST_VOLUME)pDevBroadcastHdr;
xJudgeDeviceChangePath(pDevBroadcastVolume->dbcv_unitmask, strVolume);
}
HWND hFind = FindWindowW(NULL, TEXT("USBMonitor"));
SendMessage(hFind, WM_FOUND, NULL, NULL);
}
}
}
return CallNextHookEx(g_hhook, nCode, wParam, lParam);
}
void xJudgeDeviceChangePath(DWORD dwData, char strPath)
{
char i;
for (i = 0; i < 26; ++i)
{
if (dwData & 0x1)
{
strPath = char(i + 'A');
break;
}
dwData = dwData >> 1;
if(dwData == 0x0000)
{
break;
}
}
}