Question

Hi All I need a C++ code to get Portable Device Notification when a user connects some Portable Device to system . my part of code for getting mass storage device notification is as follow , please go through it and suggest me how to do it for portable devices .

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;
}
}

the above code is working perfect for mass storage devices but now i want to extend this for portable devices so please go through my code and suggest me what to do for portable devices. thanks in advance ..

Was it helpful?

Solution

I think you need to take a look @ this two links

This is very basic tutorial to get you started with WMI http://blogs.technet.com/b/heyscriptingguy/archive/2006/03/20/how-can-i-get-a-list-of-installed-device-drivers.aspx

This is the real code you will need

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top