Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

RegisterDeviceNotification Question

  • 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, Registered Users 2 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, Registered Users 2 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