How to disable a windows Message ?

Discussion in 'MFC' started by Tranquil, Jul 20, 2008.

  1. Tranquil

    Tranquil New Member

    Joined:
    Jul 11, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    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++.

    Code:
    /
    ****************************************************************************  
    *                                 setHook  
    ****************************************************************************/  
     
    __declspec(dllexport) BOOL setHook(HWND hWnd)  
        {  
         if(hWndServer != NULL)  
        return FALSE;   
         hook = SetWindowsHookEx(WH_GETMESSAGE,  
                    (HOOKPROC)msghook,  
                    hInst,  
                    0);  
         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);  
         if(unhooked)  
        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.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    First write the code in accordance with the manual
    http://msdn.microsoft.com/en-us/library/ms644981(VS.85).aspx

    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?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice