MFCDll.h
#ifdef __cplusplus
#define DLLEXPORT extern "C" __declspec (dllexport)
#else
#define DLLEXPORT __declspec (dllexport)
#endif
DLLEXPORT int CALLBACK InstallHOOK();
DLLEXPORT int CALLBACK UninstallHOOK();
DLLEXPORT int CALLBACK GetPos();
MFCDll.cpp
// MFCDll.cpp : Defines the initialization routines for theDLL.
//
#include "stdafx.h"
#include <afxdllx.h>
#include "MFCDll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//声明一个全局变量
HINSTANCE g_hInst;
const int KeyPressMask=0x80000000; //键盘掩码常量
char g_PrvChar;
//声明共享区
#pragma data_seg("shared")
static HHOOK g_hHook=NULL;
static int m_abc=3;
static CPoint m_point=NULL;
#pragma data_seg()
#pragma comment(linker, "/SECTION:shared,rws")
//声明回调函数
LRESULT CALLBACK MouseProc(int iCode,WPARAM wParam,LPARAMlParam);
static AFX_EXTENSION_MODULE MFCDllDLL = { NULL, NULL };
extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOIDlpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("MFCDLL.DLLInitializing!n");
g_hInst=hInstance;
// Extension DLL one-timeinitialization
if(!AfxInitExtensionModule(MFCDllDLL, hInstance))
return 0;
// Insert this DLL into theresource chain
// NOTE: If this Extension DLLis being implicitly linked to by
// an MFCRegular DLL (such as an ActiveX Control)
// instead ofan MFC application, then you will want to
// removethis line from DllMain and put it in a separate
// functionexported from this Extension DLL. The RegularDLL
// that usesthis Extension DLL should then explicitly call that
// functionto initialize this Extension DLL.Otherwise,
// theCDynLinkLibrary object will not be attached to the
// RegularDLL's resource chain, and serious problems will
//result.
newCDynLinkLibrary(MFCDllDLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("MFCDLL.DLLTerminating!n");
// Terminate the library beforedestructors are called
AfxTermExtensionModule(MFCDllDLL);
}
return 1; //ok
}
//声明导出函数,装载钩子
DLLEXPORT int CALLBACK InstallHOOK()
{
g_hHook=SetWindowsHookEx(WH_MOUSE,MouseProc,g_hInst,0);
if(g_hHook)
{
return TRUE;
}
else
{
return FALSE;
}
}
//卸载钩子
DLLEXPORT int CALLBACK UninstallHOOK()
{
if(UnhookWindowsHookEx(g_hHook)==0)
{
returnFALSE;
}
else
{
returnTRUE;
}
}
//定义回调函数
LRESULT CALLBACK MouseProc(int iCode,WPARAM wParam,LPARAMlParam)
{
//AfxMessageBox("鼠标正在移动");
//获取鼠标的位置,并且写入文件中
m_point.x=10;
m_point.y=10;
//获取当前的鼠标位置
GetCursorPos(&m_point);
FILE* file;
file=fopen("c:\bbaa.log","w+");
fprintf(file,"%d,%dn",m_point.x,m_point.y);
//获取IE浏览器的所在位置
HWNDhwnd=::FindWindow("IEFrame",NULL);
CRectm_rect;
::GetWindowRect(hwnd,m_rect);
CString m_temp;
m_temp.Format("IE位置 x=%d x1=%d y=%d y1=%d 鼠标位置[%d,%d]",m_rect.left,m_rect.right,m_rect.top,m_rect.bottom,m_point.x,m_point.y);
fprintf(file,m_temp);
fclose(file);
returnCallNextHookEx(g_hHook,iCode,wParam,lParam);
}
//获取当前的坐标位置
DLLEXPORT int CALLBACK GetPos()
{
return m_point.x;
}
MFCDll.def
; MFCDll.def : Declares the module parameters for the DLL.
LIBRARY"MFCDll"
DESCRIPTION 'MFCDll Windows Dynamic LinkLibrary'
EXPORTS
InstallHOOK
UninstallHOOK
GetPos
主应用代码
typedef int (CALLBACK*InstHOOK)();//写到CPP文件头部分
void CMyHookDlg::OnStartHook()
{
AfxMessageBox("正在装载全局钩子...");
// TODO: Add your control notification handlercode here
staticHINSTANCE hinstDLL;
staticInstHOOK insthook;
hinstDLL =LoadLibrary((LPCTSTR)"D:\VCProject\MFCDll\Release\MFCDll.dll");
AfxMessageBox("装载DLL完成...");
insthook=(InstHOOK)GetProcAddress(hinstDLL,"InstallHOOK");
insthook();
}
止此钩子代码完毕.