Go4Expert (http://www.go4expert.com/)
-   MFC (http://www.go4expert.com/forums/mfc/)
-   -   How to disable a windows Message ? (http://www.go4expert.com/forums/disable-windows-message-t12265/)

Tranquil 20Jul2008 12:08

How to disable a windows Message ?
Hello all,

I want to create a USB Mass Storage access control system where one can disable a particular USB Media with username and password as input. The idea i'm trying to workout is Using Global Hooks with hook parameter WH_GETMESSAGE i need to trap the WM_DEVICECHANGE message and disable it so that the device cannot be enabled. But the things are not working properly as i expected. i'm new to vc++ so can u pls help me on this...

The code i've tried in my Hook.cpp Dll is as follows... On executing this code with my MFC App when i insert my USB Drive the syatem tary icon for safe removal of usb drive is disabled and also the dock or undock sound in winxp was disabled. But still the drive can be accessed from My Computer . Also the WM_DEVICECHANGE can be monitored on spy++.


*                                setHook 
__declspec(dllexport) BOOL setHook(HWND hWnd) 
    if(hWndServer != NULL) 
    return FALSE; 
    hook = SetWindowsHookEx(WH_GETMESSAGE, 
    if(hook != NULL) 
    hWndhWndServer = hWnd; 
    return TRUE; 
    return FALSE; // failed to set hook 
*                                clearHook 
__declspec(dllexport) BOOL clearHook(HWND hWnd) 
    if(hWnd != hWndServer || hWnd == NULL) 
    return FALSE; 
    BOOL unhooked = UnhookWindowsHookEx(hook); 
    hWndServer = NULL; 
    return unhooked; 
*                                  msghook 
static LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam) 
    if(nCode < 0) 
    CallNextHookEx(hook, nCode, wParam, lParam); 
    return 0; 
      if(((MSG*)lParam)->message == WM_DEVICECHANGE )
                ((MSG*)lParam)->message = WM_NULL;
                ((MSG*)lParam)->lParam = 0;
                ((MSG*)lParam)->wParam = 0;
                PostMessage(HWND_BROADCAST, WM_NULL, 0, 0);
    return CallNextHookEx(hook, nCode, wParam, lParam); 

Thanks in Advance...
Varun C.H.

xpi0t0s 10Aug2008 13:47

Re: How to disable a windows Message ?
First write the code in accordance with the manual

For example you don't handle the case where nCode<0 correctly.

Why do you need to PostMessage(HWND_BROADCAST, WM_NULL, 0, 0); in addition to modifying the existing message?

All times are GMT +5.5. The time now is 01:56.