把你安装这个HOOK的代码给也贴出来.

解决方案 »

  1.   

    extern "C" __declspec(dllexport) BOOL  Installhook(HWND hWnd,DWORD Id_Thread)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    ASSERT(hWnd!=NULL);
    g_hWnd=hWnd;
    g_hHook=::SetWindowsHookEx(WM_COMMAND,(HOOKPROC)KeyboardProc,NULL,Id_Thread);
    return (g_hHook==NULL)?FALSE:TRUE;
    }
    我也弄了一个hook用于得到系统键盘的消息,这是我在DLL中的函数,你把这个函数加上,在你的exe中调用一下.贴出来的汇编代码只有是快到返回值的代码,没有上下文,没有办法参考.
      

  2.   


    SendNotifyMessage(hWnd, WM_COMMAND, MSG_PAUSEREST+1, 0);  // 向其他线程创建的窗口发送消息不能使用SendMessage

    // CloseHandle(hWnd); //hWnd不属于CloseHandle操作的对象类型
      

  3.   

    这是头文件:
    #pragma once
    #ifndef _WIN32_WINNT
    #define _WIN32_WINNT 0x0500
    #endif
    #include <windows.h>#define STR_ClassName TEXT("The Software ClassName")
    #define DLLEXPORT __declspec(dllexport)extern "C" DLLEXPORT BOOL EnableKeyboardHook(BOOL bEnable);
      

  4.   

    这是DLL的所有代码。★用BoundsChecker检查,竟然发现了 40 多处 内 存 泄 露!
    请高手指点啊,我是新手,诚心感谢!!#include "Hook.h"
    #pragma comment(linker,"/OPT:nowin98")
    #define MSG_PAUSEREST WM_USER + 801
    ////////////////////////////////////////////////////////////////////////////////////////
    // The section is SHARED among all instances of this DLL.
    // A low-level keyboard hook is always a system-wide hook.
    #pragma data_seg (".SoftWareData")
    HINSTANCE g_hInstance = NULL;
    HHOOK g_hhkKeyboard = NULL; // hook handle
    UINT g_iCount = 0;
    #pragma data_seg ()
    #pragma comment(linker, "/SECTION:.SoftWareData, RWS") // tell linker: make it shared
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // The section is SHARED among all instances of this DLL.
    // A low-level keyboard hook is always a system-wide hook.
    BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
    {
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
    break;
    }
    g_hInstance = (HINSTANCE)hModule;
    return TRUE;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Low-level keyboard hook:
    // Trap task-switching keys by returning without passing along.
    LRESULT CALLBACK KeyHookProc(int nCode, WPARAM wParam, LPARAM lParam)
    {
    KBDLLHOOKSTRUCT *pkh = (KBDLLHOOKSTRUCT *)lParam;
    if (nCode == HC_ACTION)
    {
    g_iCount++;
    if (g_iCount == 2)
    {
    if (pkh->vkCode == VK_ESCAPE)
    {
    HWND hWnd = FindWindow(STR_ClassName, STR_ClassName);
    if (hWnd != NULL)
    {
    SendNotifyMessage(hWnd, WM_COMMAND, MSG_PAUSEREST, 0);
    hWnd = NULL;
    }
    }
    else
    MessageBeep(0);
    g_iCount = 0;
    }
    return TRUE;
    }
    return CallNextHookEx(g_hhkKeyboard, nCode, wParam, lParam);
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Disable task keys: install low-level kbd hook.
    // Return whether currently disabled or not.
    extern "C" DLLEXPORT BOOL EnableKeyboardHook(BOOL bEnable)
    {
    if (bEnable)
    {
    if (g_hhkKeyboard)
    UnhookWindowsHookEx(g_hhkKeyboard);
    g_hhkKeyboard = SetWindowsHookEx(WH_KEYBOARD_LL, (HOOKPROC)KeyHookProc, g_hInstance, 0);
    if (!g_hhkKeyboard) return FALSE;
    }
    else
    {
    // 启用任务键
    UnhookWindowsHookEx(g_hhkKeyboard);
    g_hhkKeyboard = NULL;
    }
    return TRUE;
    }
    ////////////////////////////////////////////////////////////////////////////////////////
      

  5.   

    请高手指点,为何上面这么简单的DLL代码,竟然会存在那么严重的内存泄露问题?还是BoundsChecker 7.2 的误报?
      

  6.   

    DLL代码没问题。如果报内存泄露,应该是误报吧。