16,473
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include "detours.h"
#include <string.h>
#include <fstream>
using namespace std;
#pragma comment(lib, "detours.lib")
#pragma comment(lib, "detoured.lib")
// must have at lease one export function
_declspec(dllexport) void exportfunc()
{
}
// target pointer for the uninstrumented MessageBox API
int (WINAPI *SysMessageBox)(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
= MessageBox;
// detour function that replaces the MessageBox API
int WINAPI HookMessageBox(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption, UINT uType)
{
ofstream ofs("record.txt", ios::app);
ofs.write(lpText, strlen(lpText));
ofs.close();
return SysMessageBox(hWnd, lpText, lpCaption, uType);
}
// DLLMain function attaches and detaches the HookMessageBox detour to the
// MessageBox target function. The MessageBox target function is referred to
// through the SysMessageBox target pointer
BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
{
if (DLL_PROCESS_ATTACH == fdwReason)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)SysMessageBox, HookMessageBox);
DetourTransactionCommit();
}
else if (DLL_PROCESS_DETACH == fdwReason)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)SysMessageBox, HookMessageBox);
DetourTransactionCommit();
}
return TRUE;
}