Window hook is not working in windows 7 but in windows xp
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Window hook is not working in windows 7 but in windows xp

Hybrid View

  1. #1
    Join Date
    Nov 2011
    Posts
    3

    Window hook is not working in windows 7 but in windows xp

    We are migrating our applications from windows xp to windows 7. One of the application is window and mouse hook. same code is working fine in windows xp but it is not working in windows 7 target machine(windows 7 installed with our own software platform).

    *One important thing is its working fine in my personal laptop which is having windows 7 os*

    i dont know what is happening...

    i have pasted the code(only window hook) of dll file which is having hook function to be called.

    void ProcWMMoving (HWND hWnd , LPRECT pRect)

    {

    RECT rcWnd;

    int Width = 0;

    ::GetWindowRect(hWnd, &rcWnd);

    Width = rcWnd.right - rcWnd.left;
    if( pRect->left <= 200 )
    {
    pRect->left = 200 + 1;
    pRect->right = 200 + Width + 1;
    }
    if( pRect->right >= 800)
    {
    pRect->right = 800 - 1;
    pRect->left = (800 - Width) - 1;

    }
    }

    LRESULT CALLBACK ScreenPosWndProc(HWND hWnd,
    UINT uMsg,
    WPARAM wParam,
    LPARAM lParam)

    {


    BOOL bResult = TRUE;

    const WNDPROC wpOldProc = (WNDPROC) ::GetProp(hWnd, WND_PROC_PROP);

    if (wpOldProc == NULL)
    {
    DebugMsg(_T("ScreenPosWndProc(): ERROR - wpOldProc is NULL!!!"));
    bResult = FALSE;
    }
    DebugMsg(_T("entering into ScreenPosWndProc"));
    LRESULT lResult = 0;

    if (bResult)
    {
    TCHAR strClassName[WINDOW_POS_CLASS_NAME_MAX_LEN];

    switch (uMsg)
    {
    case WM_MOVING:
    DebugMsg(_T("processing Wnd moving msg!"));
    ProcWMMoving (hWnd,
    (LPRECT) lParam);
    break;
    case WM_CREATE:
    GetClassName (hWnd, strClassName, WINDOW_POS_CLASS_NAME_MAX_LEN - 1);
    if (_tcscmp ( strClassName, WINDOW_POS_SYS_TREE_VIEW_32_CLASS_NAME) == 0)
    {
    DebugMsg (_T("WM_CREATE: Class Matched = %s, hwnd=0x%x"),
    strClassName, hWnd);

    g_hSysTreeView32ParentWnd = GetParent (hWnd);
    if (g_hSysTreeView32ParentWnd != NULL)
    {
    g_hSysTreeView32ParentWnd = GetParent (g_hSysTreeView32ParentWnd);
    if (g_hSysTreeView32ParentWnd != NULL)
    {
    // Init. needed to detect repositioning of Help window with Tree View
    g_nRequestToRepSysTreeView32Parent = TRUE;
    }
    }
    }
    else if (_tcscmp ( strClassName, WINDOW_POS_MS_WINHELP_CLASS_NAME) == 0)
    {
    DebugMsg (_T("WM_CREATE: Class Matched = %s, hwnd=0x%x"),
    strClassName, hWnd);

    g_hWinHelpWnd = hWnd;

    // Init. needed to detect repositioning of Help window with MS_WINHELP class
    g_nRequestToRepMsWinHelpWindow = TRUE;
    }

    break;

    case WM_DESTROY:
    if (g_nRequestToRepSysTreeView32Parent)
    {
    GetClassName (hWnd, strClassName, WINDOW_POS_CLASS_NAME_MAX_LEN - 1);
    if (_tcscmp ( strClassName, WINDOW_POS_SYS_TREE_VIEW_32_CLASS_NAME) == 0)
    {
    DebugMsg (_T("WM_DESTROY: Class Matched = %s, hwnd=0x%x"),
    strClassName, hWnd);

    // Init. needed to detect repositioning of Help window with Tree View
    g_nRequestToRepSysTreeView32Parent = FALSE;
    }
    }
    else if (g_nRequestToRepMsWinHelpWindow)
    {
    GetClassName (hWnd, strClassName, WINDOW_POS_CLASS_NAME_MAX_LEN - 1);
    if (_tcscmp ( strClassName, WINDOW_POS_MS_WINHELP_CLASS_NAME) == 0)
    {
    DebugMsg (_T("WM_DESTROY: Class Matched = %s, hwnd=0x%x"),
    strClassName, hWnd);

    // Init. needed to detect repositioning of Help window with Tree View
    g_nRequestToRepMsWinHelpWindow = FALSE;
    }
    }

    break;

    case WM_NCDESTROY:

    // Restore the original window procedure.
    ::RemoveProp(hWnd, WND_PROC_PROP);
    ::SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG) wpOldProc);

    m_mapHwnd2WndProc.erase(hWnd);

    DebugMsg(_T("WM_DESTROY: restored WNDPROC @ 0x%08x for 0x%08x"),
    wpOldProc, hWnd);
    break;

    default:
    break;
    }

    lResult = ::CallWindowProc(wpOldProc, hWnd, uMsg, wParam, lParam);
    }
    else
    {
    lResult = :efWindowProc(hWnd, uMsg, wParam, lParam);
    }

    //////
    // Call the window procedure.
    //////

    return lResult;
    }




    static LRESULT CALLBACK Monitor(int nCode, WPARAM wParam, LPARAM lParam)
    {

    DebugMsg(_T("entering into monitor()!"));
    switch(nCode)
    {
    case HCBT_CREATEWND:
    {
    DebugMsg(_T("create"));
    const HWND hWnd = (HWND) wParam;
    const WNDPROC wpOrigProc = (WNDPROC)::SetWindowLong(hWnd,GWL_WNDPROC,(LONG) ScreenPosWndProc);
    ::SetProp(hWnd, WND_PROC_PROP, wpOrigProc);
    m_mapHwnd2WndProc[hWnd] = wpOrigProc;
    }
    break;
    }
    return CallNextHookEx(g_hhkCallWndProc, nCode, wParam, lParam);
    }



    __declspec(dllexport) BOOL setMyHook(HINSTANCE hInst)
    {
    g_hhkCallWndProc = SetWindowsHookEx(WH_CBT,Monitor,hInst,0);
    return TRUE;
    } // setMyHook

    __declspec(dllexport) void clearMyHook()
    {
    UnhookWindowsHookEx(g_hhkCallWndProc);

    } // clearMyHook


    **i am calling setMyHook and clearMyHook from client exe with the following code**



    typedef BOOL(CALLBACK* SETHOOKPROC)(HINSTANCE hinst);
    typedef void(CALLBACK* UNSETHOOKPROC)();



    int APIENTRY _tWinMain(HINSTANCE /*hInstance*/,
    HINSTANCE /*hPrevInstance*/,
    LPTSTR /*lpCmdLine*/,
    int /*nCmdShow*/)
    {
    HINSTANCE hinstDLL = 0;

    //HOOKPROC fpMouseProc = 0 ;
    UNSETHOOKPROC fpCBTProc = 0 ;

    SETHOOKPROC fpSetCBTHook = 0;

    hinstDLL = LoadLibrary(_T("HookAppWin.dll"));

    ASSERT(hinstDLL);

    const int i =0;
    bool bSucess = false;
    switch(i)
    {
    case 0:
    {

    fpSetCBTHook = (SETHOOKPROC)GetProcAddress (hinstDLL, "setMyHook");

    ASSERT( fpSetCBTHook );
    bSucess = fpSetCBTHook(hinstDLL);
    }
    break;
    }
    if( bSucess )
    {
    // Create the auto-reset event that is signalled to indicate shutdown of Mousetrap process
    HANDLE lMouseTrapStopHandle = CreateEvent(NULL, FALSE, FALSE, MOUSE_TRAP_STOP_EVENT_NAME);

    if (lMouseTrapStopHandle)
    {
    // Wait for a process to request shutdown of this process
    ::WaitForSingleObject (lMouseTrapStopHandle, INFINITE);

    ::CloseHandle (lMouseTrapStopHandle);
    }
    }

    fpCBTProc = (UNSETHOOKPROC)GetProcAddress(hinstDLL, "clearMyHook");
    ASSERT(fpCBTProc);

    fpCBTProc();

    if (NULL != hinstDLL)
    FreeLibrary (hinstDLL);

    return 0;
    }

    please help me to resolve this...

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,276

    Re: Window hook is not working in windows 7 but in windows xp

    Well, I see you have already got an answer here.

    i can only add one more limitation (from MSDN):
    SetWindowsHookEx can be used to inject a DLL into another process. A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. If an application requires the use of hooks in other processes, it is required that a 32-bit application call SetWindowsHookEx to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call SetWindowsHookEx to inject a 64-bit DLL into 64-bit processes. The 32-bit and 64-bit DLLs must have different names.
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2011
    Posts
    3

    Re: Window hook is not working in windows 7 but in windows xp

    Thanks VictorN,

    actually i have tried that also... bit its not working.. i run it on admin account only.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center