一、我写以下DLL时,为什么没有调用DllMain()
二、为什么钩往的只是当前窗口的,没有系统的。
谢谢
源码如下:#include "stdafx.h"
#include "hook.h"HWND m_hWnd=0;
HHOOK m_hHook;
HINSTANCE m_hInst;BOOL WINAPI DllMain(HINSTANCE hModule,DWORD  ul_reason_for_call,LPVOID lpReserved){
MessageBox((HWND)hModule,"enter","aaa",MB_OK);
//在这里本该有显示
m_hInst=hModule;
    return TRUE;
}DLL BOOL InstallHook(HWND hWnd){
//这句因为没初始化m_hInst所以不能调用成功。
m_hHook=::SetWindowsHookEx(WH_MOUSE,(HOOKPROC)Mouse,m_hInst,NULL);//这里改为m_hHook=::SetWindowsHookEx(WH_MOUSE,(HOOKPROC)Mouse,AfxGetInstanceHandle(),NULL);
//就可以钩本窗口的信息。
if(!m_hHook)int n=GetLastError();
m_hWnd=hWnd;
return(m_hHook!=NULL);
}DLL BOOL UnstallHook(){
if(m_hHook&&(::UnhookWindowsHookEx(m_hHook)!=0))
   m_hHook=NULL;
return(m_hHook==NULL);
}DLL LRESULT CALLBACK Mouse(int nCode,      // hook code
                              WPARAM wParam,  // message identifier
                              LPARAM lParam   // mouse coordinates
                          )
{
MOUSEHOOKSTRUCT *pmousestruct;
pmousestruct=reinterpret_cast<MOUSEHOOKSTRUCT *>(lParam);
::SendMessage(m_hWnd,WM_DRAW,wParam,lParam);

return 1;//::CallNextHookEx(nCode,wParam,lParam);
}

解决方案 »

  1.   

    钩子不是这样写的。
    #include <windows.h>
    #include <windowsx.h>
    #include <tchar.h>HINSTANCE g_hinstDll = NULL;#pragma data_seg(".drectve")
        static char szLinkDirectiveShared[] = "-section:Shared,rws";
    #pragma data_seg()
    #pragma data_seg("Shared")HHOOK g_hhook      = NULL;
    HWND  g_hwndPost   = NULL;
    UINT  g_uMsgNotify = WM_USER;#pragma data_seg()static LRESULT WINAPI KeyboardHook_HookProc (
       int nCode,
       WPARAM wParam, 
       LPARAM lParam) 
    {   LRESULT lResult = CallNextHookEx(g_hhook, nCode, wParam, lParam);   if (nCode == HC_ACTION) 
       {
          PostMessage(g_hwndPost, g_uMsgNotify, wParam, lParam);
       }
       return(lResult);
    }BOOL WINAPI SetKeyboardHook (HWND hWndPost, UINT Msg) 
    {
       HHOOK hhook;   if (g_hhook != NULL) return(FALSE);   g_hwndPost   = hWndPost;
       g_uMsgNotify = Msg;
       Sleep(0);   hhook = SetWindowsHookEx(WH_KEYBOARD, KeyboardHook_HookProc, g_hinstDll, 0);
       InterlockedExchange((PLONG) &g_hhook, (LONG) hhook);
       return(g_hhook != NULL);
    }BOOL WINAPI ReleaseKeyboardHook() 
    {
       BOOL fOK = TRUE;   if (g_hhook != NULL) 
       {
          fOK = UnhookWindowsHookEx(g_hhook);
          g_hhook = NULL;
       }
       return(fOK);
    }BOOL WINAPI DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved) 
    {
       switch (fdwReason) 
       {
          case DLL_PROCESS_ATTACH:
             g_hinstDll = hinstDll;
             break;
       }
       return(TRUE);
    }