Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

RegisterDeviceNotification Question

Options
  • 10-01-2005 12:34pm
    #1
    Closed Accounts Posts: 1


    Hey, im trying to write some C++ code that will monitor when a certain usb device is plugged in/ plugged out. I've been on the MSDN website and good idea of what i need to do. However, I'm having trouble with RegisterDeviceNotification. The RegisterDeviceNotification is always returning NULL. I would very much appreciate if someone could point out where ive gone wrong with this code:


    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <dbt.h>
    #include <setupapi.h>

    int main()
    {
    bool result;
    HWND hWnd;
    char szMsg[80];
    GUID InterFaceClassGuid;
    void **hDevNotify=NULL;

    //CLASSGUID FOR USB DEVICE
    InterFaceClassGuid.Data1 = 0x8D948EE9;
    InterFaceClassGuid.Data2 = 0xD0AA;
    InterFaceClassGuid.Data3 = 0x4A12;
    InterFaceClassGuid.Data4[0] = 0x9D;
    InterFaceClassGuid.Data4[1] = 0x9A;
    InterFaceClassGuid.Data4[2] = 0x9C;
    InterFaceClassGuid.Data4[3] = 0xC5;
    InterFaceClassGuid.Data4[4] = 0xDE;
    InterFaceClassGuid.Data4[5] = 0x36;
    InterFaceClassGuid.Data4[6] = 0x19;
    InterFaceClassGuid.Data4[7] = 0x9B;

    DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;

    ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
    NotificationFilter.dbcc_devicetype= DBT_DEVTYP_DEVICEINTERFACE;
    NotificationFilter.dbcc_classguid = InterFaceClassGuid;
    hDevNotify=(void**)RegisterDeviceNotification(hWnd,&NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE);

    if (!hDevNotify)
    {
    printf("RegisterDeviceNotification failure: %d\n", GetLastError() );
    MessageBox(hWnd, szMsg, "ResisterDeviceNotification", MB_OK);

    result= false;
    }
    result= true;


    return 0;
    }

    Thanks


Comments

  • Registered Users Posts: 79 ✭✭tendofan


    Maybe you've left it out of the code you posted, but hWnd doesn't appear to be set to a valid window handle.

    Tendofan.


  • Registered Users Posts: 1,391 ✭✭✭fatherdougalmag


    What does GetLastError say? For a start, you'll need to initialise all of the 'size' members of the structures you're using. Although you're using the MSDN example code, it's something to be wary of. Also have a look here for an example of RegisterDeviceNotification.


Advertisement