• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
gretty

Accessing a Devices Interface/Class GUID always causes an error

5 posts in this topic

Hello

 

Using C++ and native Win32 functions, I am attempting to get the class/interface GUID of a connected device such as a TV or projector. I am using the function RegisterDeviceNotification() to give me the GUID when a TV is connected.

My Problem: When the TV is connected I can successfully be notified but when I go to access the GUID of the device(by inspecting the lParam structure) I get an access violation or error.

I cant figure out whats going wrong? Whenever I go to access or use the GUID my program fails, note my program doesn't crash but the output window writes something like First-chance exception at 0x001c1a9d in myProgram.exe... and I cant figure out whats going wrong?

   

 

HDEVNOTIFY *hDeviceNotify;
    GUID interfaceClassGuid = { 0x25dbce51, 0x6c8f, 0x4a72, 0x8a,0x6d,0xb5,0x4c,0x2b,0x4f,0xc8,0x35 };
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        int wmId, wmEvent;
        PAINTSTRUCT ps;
        HDC hdc;
    
        switch (message)
        {
            case WM_CREATE:
            {                  
                 openConsoleWindow();
    
                 DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
                 ZeroMemory( &NotificationFilter, sizeof(NotificationFilter) );
                 NotificationFilter.dbcc_size       = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
                 NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
                 NotificationFilter.dbcc_classguid  = interfaceClassGuid;
             
                 *hDeviceNotify = RegisterDeviceNotification( hWnd, &NotificationFilter, DEVICE_NOTIFY_WINDOW_HANDLE );
             
                 if (*hDeviceNotify == NULL)
                     printf("hDeviceNotify = NULL \n");
            }    
            break;
            case WM_DEVICECHANGE:
            {
                PDEV_BROADCAST_DEVICEINTERFACE b = (PDEV_BROADCAST_DEVICEINTERFACE) lParam;
                    
                // The following printf NEVER prints out.
                // When accessing the below GUID I get an error:
                // "First-chance exception at 0x001c1a9d in myProgram.exe: 0xC0000005: Access violation writing location 0x00000000."
                printf("GUID: Data1: %x, Data2: %x, Data3: %x, Data4: %s \n", b->dbcc_classguid.Data1,
                    b->dbcc_classguid.Data2, b->dbcc_classguid.Data3, (char*)b->dbcc_classguid.Data4);
                        
                // The following function ALWAYS fails.
                // GetLastError() gives the error "The parameter is incorrect"
                HDEVINFO hDevInfo = SetupDiGetClassDevs(&b->dbcc_classguid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
                if (hDevInfo == INVALID_HANDLE_VALUE) {
                     //printf("hDevInfo == INVALID_HANDLE_VALUE \n");
                   outputLastError(_T("hDevInfo == INVALID_HANDLE_VALUE \n"));
                }
    
            }
            break;
            ...

 

0

Share this post


Link to post
Share on other sites

Have you checked wparam? If it's something like DBT_DEVICEREMOVECOMPLETE, you're probably casting lParam to the wrong type in the first place.

 

In general, it's my understanding that you should always first cast lparam to a DEV_BROADCAST_HDR* and only cast to something further once you have confirmed the value of its dbch_devicetype.

1

Share this post


Link to post
Share on other sites

Thanks for the reply

 

For my TV, the wParam value is always DBT_DEVNODES_CHANGED, even when I connect and remove the device(instead of having DBT_DEVICEARRIVAL/DBT_DEVICEREMOVE sent).

 

The following code takes your advice into account but it reproduces the same errors(exception and same GetLastError() value):

 

 

        case WM_DEVICECHANGE:
        {
            if (wParam != DBT_DEVNODES_CHANGED) {
                printf("1: \n");
                break;
            }

            PDEV_BROADCAST_HDR h = (PDEV_BROADCAST_HDR) lParam;
            if (h->dbch_devicetype != DBT_DEVTYP_DEVICEINTERFACE) {
                printf("2\n");
                break;
            }

            PDEV_BROADCAST_DEVICEINTERFACE b = (PDEV_BROADCAST_DEVICEINTERFACE) h;
                
            // When accessing the below GUID I get an error:
            // "First-chance exception at 0x001c1a9d in TEST GUID Error.exe: 0xC0000005: Access violation writing location 0x00000000."
            printf("GUID: Data1: %x, Data2: %x, Data3: %x, Data4: %s \n", b->dbcc_classguid.Data1,
                b->dbcc_classguid.Data2, b->dbcc_classguid.Data3, b->dbcc_classguid.Data4);
                    
            // The following function ALWAYS fails.
            // GetLastError() gives the error "The parameter is incorrect"
            HDEVINFO hDevInfo = SetupDiGetClassDevs(&b->dbcc_classguid, NULL, NULL, DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
            if (hDevInfo == INVALID_HANDLE_VALUE) {
                 //printf("hDevInfo == INVALID_HANDLE_VALUE \n");
               outputLastError(_T("hDevInfo == INVALID_HANDLE_VALUE \n"));
            }

        }
        break;
0

Share this post


Link to post
Share on other sites

MSDN says that lParam is 0 when wParam is DBT_DEVNODES_CHANGED (which IMHO, you should really assert on anyway). So your code behaves as expected -- undefined behaviour.

 

It appears you have copied the GUID passed to RegisterDeviceNotification from the MSDN example code. I don't know much about Windows' device handling but I have a strong suspicion that this GUID is not the one you should be using.

 

Have you tried adding DEVICE_NOTIFY_ALL_INTERFACE_CLASSES to the flags in your call to RegisterDeviceNotification()? In this case the GUID is ignored and you get messages for all devices. This may give you a better indication of what's really happening and help you detect your TV.

 

Just out of curiosity, are you attempting to detect your telly to automatically enable some kind of out-of-band debug console for a game? That would be cool :)

0

Share this post


Link to post
Share on other sites

I'll try adding DEVICE_NOTIFY_ALL_INTERFACE_CLASSES and see what messages I receive.

 

 

Just out of curiosity, are you attempting to detect your telly to automatically enable some kind of out-of-band debug console for a game? That would be cool smile.png

 

Nah, the ultimate goal is so when I connect my laptop to the TV it will open my folder that contains all my TV shows and start playing the video file I was playing when I last disconnected from the TV(maybe even at the same position/time in the file). I'm moving into a new unit and I'm too cheap to pay for Foxtel(cable TV) :P so I want this application to let me play tv shows off my laptop :)

 

The code above is the first step:

 - Detect a device arrival - get the class/interface GUID of the device

 - Determine if the connect device is a TV (I have no idea how I'll do this but I suspect using a SetupDi..() function which is why I need the GUID)

 - If the device is a TV:

    - Open the tv show folder

    - Start playing the video file I was playing when I last disconnected from the TV

0

Share this post


Link to post
Share on other sites

Thanks Edd :)

 

Adding that flag worked. Now I receive DBT_DEVICEARRIVAL message upon connection and the GUID is inside the structure also.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0