Catch WM_DEVICECHANGE

How to know about arrival WM_DEVICECHANGE?

WndProcoverwritten. I grab a whole bunch of messages, but none of them have a type WM_DEVICECHANGE. RegisterDeviceNotificationmakes the linker complain that it cannot find the function! So I'm stuck in this voodoo magic. Please help.

PS: Of course, I searched googling and stackoverflowing (lol) for all of this for about 8 hours.

int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
        LPSTR lpCmdLine, int nCmdShow)
{
        LPTSTR lolclassname = "lolclass";
    WNDCLASS lolclass;
    HWND lolwindow;
    MSG lolmsg;
    UINT msgstatus;

    lolclass.style = CS_VREDRAW;
    lolclass.lpfnWndProc = &lol_wnd_proc;
    lolclass.cbClsExtra = 0;
    lolclass.cbWndExtra = 0;
    lolclass.hInstance = hInstance;
    lolclass.hIcon = NULL;
    lolclass.hCursor = NULL;
    lolclass.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);
    lolclass.lpszMenuName = NULL;
    lolclass.lpszClassName = lolclassname;
    if(!RegisterClass(&lolclass)) fail("RegisterClassEx");

    lolwindow = CreateWindow("lolclass", NULL, WS_MINIMIZE, CW_USEDEFAULT,
            CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
            HWND_MESSAGE, NULL, hInstance, NULL);

    if(lolwindow == NULL) fail("CreateWindowEx");

    /*ShowWindow(lolwindow, nCmdShow);
    UpdateWindow(lolwindow);*/

    do {
/*      if(!SetWindowPos(lolwindow, HWND_TOPMOST, 1, 1, 1, 1,
                    SWP_HIDEWINDOW))
            fail("SetWindowPos");*/
        msgstatus = GetMessage(&lolmsg, lolwindow, 0, 0);
        if(!msgstatus) break;
        if(msgstatus == - 1) fail("GetMessage");
        TranslateMessage(&lolmsg);
        DispatchMessage(&lolmsg);
        Sleep(1000);
    } while(1);

    return lolmsg.wParam;
}

lol_wnd_pro c is executed, but never when it is supposed to (when changing the device, of course, do I clear?)

+3
source share
1 answer

The problem is that you are creating a message - only window that does not receive broadcasts:

. , z-, , . .

, , . - HWND_MESSAGE CreateWindow , ShowWindow.


, Sleep(1000) . , . Sleep. , GetMessage , , , .

:

while( (bRet = GetMessage( &msg, NULL, 0, 0 )) != 0)
{ 
    if (bRet == -1)
    {
        // handle the error and possibly exit
    }
    else
    {
        TranslateMessage(&msg); 
        DispatchMessage(&msg); 
    }
} 
+4

All Articles