kbdll.cpp
来自「电脑编程技巧和源码。很不错的。」· C++ 代码 · 共 170 行
CPP
170 行
// Kbdll.cpp : Defines the initialization routines for the DLL.
//
#include "stdafx.h"
#include <afxdllx.h>
#include "Kbdll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
static AFX_EXTENSION_MODULE KbdllDLL = { NULL, NULL };
#define DLLEXPORT _declspec (dllexport)
/*extern "C" DLLEXPORT LRESULT WINAPI KeyboardHookProc(
int nCode,WPARAM wParam,LPARAM lParam);*/
extern "C" DLLEXPORT LRESULT WINAPI
GetMsgHookProc(int nCode,WPARAM wParam,LPARAM lParam);
//在win32 dll中,每个进程获得其实例的全局变量拷贝。挂钩程序要访问
//全局变量,而且这些变量应装入dll的所有进程之间共享。通过指定
//#progma data_seg指令和指定在def文件中的SECTION部分中的共享段可以
//作到着一点
#pragma data_seg(".sdata")
// HHOOK glhHook=NULL;//hook handle
HHOOK glhHook2=NULL;//hook handle
HINSTANCE glhInstance=NULL;//instance of dll
BOOL changed=FALSE;
#pragma data_seg()
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("KBDLL.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!KbdllDLL.bInitialized)
{
AfxInitExtensionModule(KbdllDLL, hInstance);
// if (!AfxInitExtensionModule(KbdllDLL, hInstance))
// return 0;
// Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
// an MFC Regular DLL (such as an ActiveX Control)
// instead of an MFC application, then you will want to
// remove this line from DllMain and put it in a separate
// function exported from this Extension DLL. The Regular DLL
// that uses this Extension DLL should then explicitly call that
// function to initialize this Extension DLL. Otherwise,
// the CDynLinkLibrary object will not be attached to the
// Regular DLL's resource chain, and serious problems will
// result.
new CDynLinkLibrary(KbdllDLL);
glhInstance=hInstance;
}
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("KBDLL.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(KbdllDLL);
}
return 1; // ok
}
CKbHook::CKbHook()
{
}
CKbHook::~CKbHook()
{
Stop();//uninstall mouse hook
}
HHOOK CKbHook::Start()
{
glhHook2=SetWindowsHookEx(WH_GETMESSAGE,GetMsgHookProc,glhInstance,0);
// glhHook=SetWindowsHookEx(WH_KEYBOARD,KeyboardHookProc,glhInstance,0);
return glhHook2;
}
BOOL CKbHook::Stop()
{
BOOL bResult=FALSE;
//uninstall mouse hook if currently active
/* if(glhHook)
{
bResult=UnhookWindowsHookEx(glhHook);
if(bResult)
{
//initalize variables
glhHook=NULL;
}
}*/
if(glhHook2)
{
bResult=UnhookWindowsHookEx(glhHook2);
if(bResult)
{
//initalize variables
glhHook2=NULL;
}
}
return bResult;
}
/*extern "C" DLLEXPORT LRESULT WINAPI
KeyboardHookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
BOOL bNextProc=FALSE;
LRESULT lResult=0;
HWND hWndTarget=NULL;
if(!hWndTarget)
hWndTarget=::GetActiveWindow();
bNextProc=TRUE;
if(bNextProc)
lResult=CallNextHookEx(glhHook,nCode,wParam,lParam);
return(lResult);
}*/
extern "C" DLLEXPORT LRESULT WINAPI
GetMsgHookProc(int nCode,WPARAM wParam,LPARAM lParam)
{
BOOL bStopMessage=FALSE;
MSG * msg=(MSG * ) lParam;
if(nCode<0)
return CallNextHookEx(glhHook2,nCode,wParam,lParam);
else
{
HWND hWndTarget=::GetActiveWindow();
switch(msg->message)
{
case WM_CHAR:
if(((char)(msg->wParam)=='-')&&(changed==FALSE))
{
msg->wParam=(WPARAM)'_';
changed=TRUE;
}
else
{
if(((char)(msg->wParam)=='_')&&(changed==FALSE))
{
msg->wParam=(WPARAM)'-';
changed=TRUE;
}
}
break;
default:
break;
}
}
changed=FALSE;
return CallNextHookEx(glhHook2,nCode,wParam,lParam);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?