Hook(钩子),截获系统消息,具体不太会用.

解决方案 »

  1.   

    全局钩子hook,
    用vb没法实现,因为vb无法写出来标准动态库
    用VC(Not MFC)做一个标准的动态库
    使用setWindowsHook将其投到系统进程中
    调用hook里的函数时需要返回一个自定义消息,
    以供自己开发的应用程序使用
    在hook程序里对消息进行处理就可以了,当截获到
    需要的消息时,就向自己的程序发自定义消息即可
      

  2.   

    // rtahook.cpp : Defines the entry point for the DLL application.
    //#include "stdafx.h"
    #include "rtahook.h"
    #pragma data_seg("Shared")
    UINT WM_MM_FILECLOSE=RegisterWindowMessage("WM_MM_FILECLOSE");
    HWND hWndMain = NULL;
    HHOOK hHook = NULL;
    HWND ghw=NULL;
    DWORD pId=NULL;
    DWORD tId=NULL;
    char  WndText[256];
    #pragma data_seg()
    #pragma comment (linker,"/SECTION:Shared,rws")
    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;
        }
        return TRUE;
    }
    // This is an example of an exported variable
    RTAHOOK_API int nRtahook=0;
    LRESULT CALLBACK CallWndProc(  int nCode,  
     WPARAM wParam,
     LPARAM lParam)
    { CWPSTRUCT *lp;
    lp=(CWPSTRUCT*) lParam;
    if (lp->message==WM_DESTROY)
    {
    if (lp->hwnd==ghw)
    {
    long Ret=UnhookWindowsHookEx(hHook);
    if (Ret==0)
    {
    ::MessageBox(0,"取消Hook没有成功",NULL,NULL);
    return 1;
    }
    else
    {
    if (hWndMain==0)
    {
    MessageBox(0,"主窗体不见了",NULL,NULL);
    }
    else
    {
    ::SendNotifyMessage(hWndMain,WM_MM_FILECLOSE,0,0);
    if (WM_MM_FILECLOSE==0)
    MessageBox(0,"自定义消息为空",NULL,NULL);
    }
    }
    }
    }
    return(CallNextHookEx(hHook,nCode,wParam,lParam));
    }
    // This is an example of an exported function.
    #pragma comment (linker,"/export:HOOK=_HOOK@8")
    extern "C" RTAHOOK_API int  _stdcall HOOK (LPCTSTR strWndText,LPARAM lParam)
    {
    lParam=WM_MM_FILECLOSE;
    if (lParam=0)
    MessageBox(0,"自定义消息没有生成",NULL,NULL);
    hWndMain=FindWindow(NULL,strWndText);
    if (hWndMain==0) 
    MessageBox(0,"主窗体没有发现",NULL,NULL);
    HINSTANCE hInst = GetModuleHandle("SSHook.dll");
    POINT pt;
    pt.x=100;
    pt.y=100;
    ghw=WindowFromPoint(pt);
    tId=GetWindowThreadProcessId(ghw,&pId);
    hHook=SetWindowsHookEx(WH_CALLWNDPROC,CallWndProc,hInst,tId);
    if (hHook==0)
    ::MessageBox(0,"添加没有成功",NULL,NULL);
        //lpPrevWndProc =(WNDPROC) SetWindowLong(ghw, GWL_WNDPROC,  (long) WindowProc);
    ::CloseHandle(hInst);
    return WM_MM_FILECLOSE;
    }// This is the constructor of a class that has been exported.
    // see rtahook.h for the class definition
    CRtahook::CRtahook()

    return; 
    }