/* DllPart。Dll */
#include
#include
#include
#include
typedef (WINAPI *MsgBoxType)(HWND,LPCWSTR,LPCWSTR,UINT);
MsgBoxType oldMsg; /*API原入口地址*/
DWORD APIAddress; /*存储API入口地址的地方的地址*/
int WINAPI MyMessage(HWND hWnd ,LPCWSTR M1,LPCWSTR M2, UINT M3) {
/* 这是用来替换的函数 */
return oldMsg(hWnd,buf,M2,MB_OK);
}
const char szApp[] = "DllPart。
dll";
HHOOK hHook; /*Hook的句柄*/
HMODULE hInst; /*DLL 模块句柄,用于SetWindowsHookEx函数*/
HWND hTarget; /*目标窗口句柄*/
/*DLL 入口*/
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID lpvReserved)
{
hInst = inst;
switch (reason) {
case DLL_PROCESS_ATTACH:
/*调试信息,表示DLL已经加载*/
MessageBox(NULL,"DLL_PROCESS_ATTACH",szApp,MB_OK);
break;
case DLL_PROCESS_DETACH:
/*调试信息,表示DLL已经卸载*/
MessageBox(NULL,"DLL_PROCESS_DETACH",szApp,MB_OK);
break;
}
return true;
}
/*显示GetLastError的信息*/
void showerr(const char *m) {
char message[255];
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError()
,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),message,255, 0);
MessageBox(NULL,message,m,MB_OK);
}
//-----------------------
void UnHookApi() {
/*卸载API Hook用*/
}
void HookApi() {
/*加载API Hook同上面介绍的函数一样*/
}
//-----------------------
/*用于WH_GETMESSAGE的Hook Procedure*/
LRESULT CALLBACK GetMsgProc(int nCode,WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
MSG *msg = (MSG *)lParam;
if (msg->message == WM_CHAR) {
if (msg->wParam == 'h') HookApi();
if (msg->wParam == 'u') UnHookApi();
}
}
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
extern "C" __declspec(dllexport) SetAPIHook(HWND handle) {
DWORD ThreadId = GetWindowThreadProcessId(handle, NULL);
hTarget = handle;
MessageBox(NULL, "Enabling CallWndProc Hook", szApp, MB_OK);
hHook = SetWindowsHookEx(WH_GETMESSAGE,GetMsgProc,hInst,ThreadId);
if (hHook) {
MessageBox(NULL,"Hook OK!", szApp, MB_OK);
} else {
showerr("SetWindowsHookEx");
}
}
extern "C" __declspec(dllexport) UnHookAPIHook() {
MessageBox(NULL, "Disenabling CallWndProc Hook", szApp, MB_OK);
if (UnhookWindowsHookEx(hHook)) {
MessageBox(NULL,"UnHook OK!", szApp, MB_OK);
} else {
showerr("UnHookWindowsHookEx");
}
}
。
全部