1.新建/普通DLL
2.新建.cpp和.h文件
3..cpp文件内容:
// MouseHook.cpp : Defines the entry point for the DLL application.
//
#include "MouseHook.h"
/********************************************************************/
/* 文件名: MyMouseHook.cpp */
/* */
/* 功能: 标准 DLL ---- 利用鼠标全局钩子实现右键双击粘贴功能 */
/* */
/* 作者: 李博 (booolee) 时间: 2004.9.18 */
/* */
/* BLOG: http://blog.csdn.net/booolee */
/********************************************************************/
// 导出函数列表
// StartHook
// StopHook
// About
#define _WIN32_WINNT 0x0500 // 设置系统版本, 确保可以使用底层鼠标钩子
#include "windows.h"
// 全局变量
HINSTANCE g_hInstance = NULL; // 模块实例句柄
HHOOK g_hHook = NULL; // 钩子句柄
// DLL 入口函数
bool APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
// 保存模块实例句柄
g_hInstance = (HINSTANCE)hModule;
// 在进程结束或线程结束时卸载钩子
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_PROCESS_DETACH:
case DLL_THREAD_DETACH:
if (g_hHook != NULL) UnhookWindowsHookEx(g_hHook);
break;
}
return true;
}
// 底层鼠标钩子函数
LRESULT CALLBACK MouseHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *) lParam;
if (nCode>=0)
{
if (wParam==WM_RBUTTONDBLCLK)
{
HWND glhTargetWnd=pMouseHook->hwnd;
if(glhTargetWnd !=NULL)
{
char * buffer = NULL;
if (OpenClipboard(NULL))
{
HANDLE hData = GetClipboardData(CF_TEXT);
char * buffer = (char*)GlobalLock(hData);
SetWindowText(glhTargetWnd,buffer);
GlobalUnlock(hData);
CloseClipboard();
return true;
}
}
}
}
// 传给系统中的下一个钩子
return CallNextHookEx(g_hHook, nCode, wParam, lParam);
}
/********************************************************************/
/* 安装鼠标钩子 */
/* 参数: (无) */
/* 返回值: true 成功, false 失败 */
/********************************************************************/
bool __stdcall StartHook()
{
// 如果已经安装鼠标钩子则返回 false
if (g_hHook != NULL) return false;
HWND hdl = ::FindWindow(NULL, "完 美 世 界 外 挂");
DWORD dwProcessId = 0;
DWORD dwThreadId = GetWindowThreadProcessId(hdl, &dwProcessId); //获得被监控进程的窗口线程ID
if (dwProcessId != 0) //找到了,如果=0就成了全局钩子,这样就所有的进程中都有Drive.dll了,因为钩子函数体在Drive.dll中,所以会加载Drive.dll到所有进程
{
// 安装底层鼠标钩子
g_hHook = SetWindowsHookEx(WH_MOUSE, MouseHookProc, g_hInstance, dwThreadId);
if (g_hHook == NULL) return false;
MessageBox(NULL,"启动成功!","BOBO鼠标双击右键粘贴器",MB_OK);
return true;
}
else
{
return false;
}
}
/********************************************************************/
/* 卸载鼠标钩子 */
/* */
/* 参数: (无) */
/* */
/* 返回值: true 成功, false 失败 */
/********************************************************************/
bool __stdcall StopHook()
{
// 卸载钩子
if (UnhookWindowsHookEx(g_hHook) == 0) return false;
g_hHook = NULL;
MessageBox(NULL,"卸载成功!","BOBO鼠标双击右键粘贴器",MB_OK);
return true;
}
bool __stdcall About()
{
MessageBox(NULL,"作者:李博\n功能:双击鼠标右键进行粘贴操作\n日期:2004.9.18\n邮件:booolee@yahoo.com.cn","关于鼠标钩子",MB_OK);
return true;
}
4..h文件内容:
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the MOUSEHOOK_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// MOUSEHOOK_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#ifdef MOUSEHOOK_EXPORTS
#define MOUSEHOOK_API __declspec(dllexport)
#else
#define MOUSEHOOK_API __declspec(dllimport)
#endif
/********************************************************************/
/* 文件名: MyMouseHook.h */
/* */
/* 功能: 标准 DLL 导出函数头文件, 在使用该DLL的程序中包含此文件 */
/* */
/* 作者: 李博 (booolee) 时间: 2004.9.18 */
/* */
/* BLOG: http://blog.csdn.net/booolee */
/********************************************************************/
MOUSEHOOK_API bool __stdcall StartHook();
MOUSEHOOK_API bool __stdcall StopMaskKey();
MOUSEHOOK_API bool __stdcall About();