0

大家好,当用户将某些便携式设备连接到系统时,我需要一个 C++ 代码来获取便携式设备通知。我获取大容量存储设备通知的部分代码如下,请仔细阅读并建议我如何为便携式设备执行此操作。

LRESULT CALLBACK DLLWindowProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HDEVNOTIFY hDeviceNotify;
__int32 devType;
char c;
char buf[5]={0};
PawCallBack *callbackHandle;
char Buffer[10];
switch (message)
{
case WM_CREATE:
    RegisterHotKey(hwnd, 1, MOD_ALT  , VK_SNAPSHOT);
    RegisterHotKey(hwnd, 2, 0 , VK_SNAPSHOT);
    if ( ! DoRegisterDeviceInterfaceToHwnd(WceusbshGUID, hwnd, &hDeviceNotify) )
    {
        // Terminate on failure.
        ExitProcess(1);
    }
    break;
case WM_HOTKEY:
    LPSTR temppath;
    CaptureScreen(GetDesktopWindow());
    UnicodeToAnsi(temp_filename, &temppath);
    callbackHandle      = new PawCallBack;
    strcpy(callbackHandle->ImageSavePath,temppath);
    callbackHandle->servicecode     = 111;
    if(!myPipe.SendMsg(_T("PAWAGENT"), callbackHandle, sizeof(PawCallBack), Buffer, 10,0))
    {
        //MessageBoxA(NULL,"Cannot Send to pipe","Drive Info",MB_OK);
    }
    delete callbackHandle;

    break;

case WM_DEVICECHANGE:
    {
        PDEV_BROADCAST_HDR pHdr;
        PDEV_BROADCAST_VOLUME vol;

        // Output some messages to the window.
        switch (wParam)
        {
        case DBT_DEVICEARRIVAL:
            //MessageBoxA(NULL,"Device Arrived", "Info", MB_OK);
            pHdr = (PDEV_BROADCAST_HDR)lParam;
            if(pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME)
            {

                vol = (PDEV_BROADCAST_VOLUME)pHdr;
                c = DriveMaskToLetter(vol->dbcv_unitmask);
                memset(buf,0,5);
                sprintf(buf,"%c:", c);
                //MessageBoxA(NULL,&c, "Device Arrived", MB_OK);
                callbackHandle      = new PawCallBack;
                strcpy(callbackHandle->DriveName, buf);
                callbackHandle->servicecode     = 109;
                if(!myPipe.SendMsg(_T("PAWAGENT"), callbackHandle, sizeof(PawCallBack), Buffer, 10, 0))
                {
                    //MessageBoxA(NULL,"Cannot Send to pipe","Drive Info",MB_OK);
                }
                delete callbackHandle;
            }
            break;

        case DBT_DEVICEREMOVECOMPLETE:
            pHdr = (PDEV_BROADCAST_HDR)lParam;
            if(pHdr->dbch_devicetype == DBT_DEVTYP_VOLUME)
            {
                vol = (PDEV_BROADCAST_VOLUME)pHdr;
                c = DriveMaskToLetter(vol->dbcv_unitmask);
                memset(buf,0,5);
                sprintf(buf,"%c:",c);
                callbackHandle      = new PawCallBack;
                strcpy(callbackHandle->DriveName,buf);
                callbackHandle->servicecode     = 110;

                if(!myPipe.SendMsg(_T("PAWAGENT"), callbackHandle, sizeof(PawCallBack), Buffer, 10, 0))
                {
                    //MessageBoxA(NULL,"Cannot Send to pipe","Drive Info",MB_OK);
                }
                delete callbackHandle;
            }
            break;

        }
        break;
case WM_DESTROY:
    PostQuitMessage (0);
    break;
case WM_CLOSE:
    if ( ! UnregisterDeviceNotification(hDeviceNotify) )
    {
        MessageBoxA(NULL, "UnregisterDeviceNotification Failed", "Info",MB_OK);
    }
    UnregisterHotKey(hwnd, 1);
    UnregisterHotKey(hwnd, 2);
    break;
default:
    return DefWindowProc (hwnd, message, wParam, lParam);
    }
    return 0;
}
}

上面的代码非常适合大容量存储设备,但现在我想将其扩展到便携式设备,所以请查看我的代码并建议我为便携式设备做什么。提前致谢 ..

4

1 回答 1

0

我想你需要看看@这两个链接

这是让您开始使用 WMI http://blogs.technet.com/b/heyscriptingguy/archive/2006/03/20/how-can-i-get-a-list-of-installed-device的非常基本的教程-drivers.aspx

这是您需要的真实代码

http://msdn.microsoft.com/en-us/library/windows/desktop/aa390425(v=vs.85).aspx

于 2012-08-27T10:29:03.540 回答