利用系統鉤子鎖住滑鼠和鍵盤

來源:互聯網
上載者:User
要截獲系統中所有應用程式的訊息,必須將鉤子函數寫在動態串連庫中,否則鉤子只能截獲該應用程式的訊息。其實,這個道理很簡單,我們看下面這個安裝鉤子的API: HHOOK SetWindowsHookEx(
  int
idHook ,        // type of hook to install
  HOOKPROC lpfn ,     // address of hook procedure
  HINSTANCE hMod ,    // handle to application instance
  DWORD dwThreadId   // identity of thread to install hook for
);
注意第三個參數( HINSTANCE hMod)和第四個參數( DWORD dwThreadId),這兩個參數指明了一個dll執行個體控制代碼和需要截獲訊息的線程ID。如果hMod為NULL,則必須指定dwThreadId;如果dwThreadId為0則說明鉤子對所有線程有效,但此時你必須指定hMod,這是因為一個dll就是一個獨立的線程,它獨立於其他應用程式,因此可以截獲其他線程的訊息。下面這個函數實現了鉤子安裝: /////////////////////////////////////////////////////////////////////////////
// The KeyBoard Hook Install Function Implement
DllExport void WINAPI InstallHook()
{
 g_KeyBoardHook = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardHookProc,
       theApp.m_hInstance,0);
 g_MouseHook    = SetWindowsHookEx(WH_MOUSE,(HOOKPROC)MouseHookProc,
       theApp.m_hInstance,0);
}
其中,theApp是dll類的全域對象。下面這個函數實現了鉤子的卸載: /////////////////////////////////////////////////////////////////////////////
// The KeyBoard Hook UnInstall Function Implement
DllExport void WINAPI UnInstallHook()
{
 UnhookWindowsHookEx(g_KeyBoardHook);
 UnhookWindowsHookEx(g_MouseHook);
}
下面兩個函數分別是鍵盤和滑鼠的鉤子函數: /////////////////////////////////////////////////////////////////////////////
// The KeyBoard & Mouse Hook Function Implement
DllExport LRESULT CALLBACK KeyboardHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
 if(nCode == HC_ACTION)
 {
  switch(wParam)
  {
   case VK_SPACE:
    if(((GetKeyState(VK_LCONTROL) & 0x8000) != 0) && ((GetKeyState(VK_RSHIFT) & 0x8000) != 0))
    {
     bEatKeystroke = FALSE;
     bEatMousestroke = FALSE;
    }
    break;    
  }
 }
     return(bEatKeystroke ? 1 : CallNextHookEx(g_KeyBoardHook,nCode,wParam,lParam));
}
DllExport LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
     return(bEatMousestroke ? 1 : CallNextHookEx(g_MouseHook,nCode,wParam,lParam));
}
很簡單,這裡只是判斷是否按下了左Ctrl+右Shift+空格,如果是就解鎖。最後,在應用程式中,引入該dll的lib,然後在應用程式的恰當位置調用上面函數安裝hook,在適當的位置Uninstall即可。下面是一個能夠在一個時間段鎖住鍵盤和滑鼠完整的應用程式: //TimerLock.h
// head file
// declare application class and window class
//        LionMountainMan
//           2005-8-25
class CMyApp : public CWinApp
{
protected:
 CTime m_time;
public:
 virtual BOOL InitInstance();
 virtual int   Run();
};
/////////////////////////////////////////////////////////////////////////////////////
// TimerLock.cpp
// implement file
// implement the timer lock keyboard and mouse function.
//            LionMountainMan
//               2005-8-25
#include <afxwin.h>
#include "TimerLock.h"
#include "HookDll.h"
CMyApp myApp; /////////////////////////////////////////////////////////////////////////////////////
//CMyApp member functions
BOOL CMyApp::InitInstance()
{
 m_time = CTime::GetCurrentTime();
 return TRUE;
}
int CMyApp::Run()
{
 BOOL  bLock = FALSE;
 CTime tmAlarm1(m_time.GetYear(),m_time.GetMonth(), /
       m_time.GetDay() ,10,30,0);
 CTime tmCancelAlarm1(m_time.GetYear(),m_time.GetMonth(), /
       m_time.GetDay() ,10,40,0);
 CTime tmAlarm2(m_time.GetYear(),m_time.GetMonth(), /
       m_time.GetDay() ,15,30,0);
 CTime tmCancelAlarm2(m_time.GetYear(),m_time.GetMonth(), /
       m_time.GetDay() ,15,40,0);
 CTime tmAlarm3(m_time.GetYear(),m_time.GetMonth(), /
       m_time.GetDay(),12,30,0);
 CTime tmCancelAlarm3(m_time.GetYear(),m_time.GetMonth(),
          m_time.GetDay(),13,30,0);
 CTime tmAlarm4(m_time.GetYear(),m_time.GetMonth(), /
       m_time.GetDay(),17,30,0);
 CTime tmCancelAlarm4(m_time.GetYear(),m_time.GetMonth(), /
       m_time.GetDay(),18,0,0);   
  
 while(1)
 {
  m_time = CTime::GetCurrentTime();
  if((tmAlarm1 <= m_time && tmCancelAlarm1 >= m_time) || /
     (tmAlarm2 <= m_time && tmCancelAlarm2 >= m_time) || /
     (tmAlarm3 <= m_time && tmCancelAlarm3 >= m_time) || /
     (tmAlarm4 <= m_time && tmCancelAlarm4 >= m_time))
  {
   if(!bLock)
   {
    InstallHook();
    MessageBox(NULL,"時間到,停止一切操作!","溫馨提示",MB_ICONSTOP|MB_TOPMOST);
    bLock = TRUE;
   }
  }
  else if(bLock)
  {
   UnInstallHook();
   bLock = FALSE;
  }
  Sleep(1000);
 }
 return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.