绕道 3.0 钩子崩溃消息框 A.
Detours 3.0 Hook Crashes MessageBoxA
我正在尝试将 MessageBoxA 函数与 MS Detours 3.0 挂钩,但当我尝试时,我的程序崩溃了。我不确定是什么导致程序崩溃。当我运行测试程序并按 shift 时,会出现消息框,但是当我注入 dll 并按 shift 时,我的程序崩溃了。
测试程序
#include <Windows.h>
int main()
{
for(;;)
{
if(GetAsyncKeyState(VK_SHIFT))
{
MessageBoxA(0,"NOT HOOKED","HOOK STATUS",0);
}
}
}
钩子 DLL
#include <Windows.h>
#include <detours.h>
#pragma comment(lib,"detours.lib")
BOOL (WINAPI* oMessageBoxA)(HWND,LPCTSTR,LPCTSTR,UINT);
BOOL WINAPI hMessageBoxA( HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption,UINT uType)
{
return oMessageBoxA(hWnd,"HOOKED",lpCaption,uType);
}
void patch()
{
HMODULE user32 = GetModuleHandle("user32.dll");
if(user32 != NULL)
{
DWORD MessageBoxAddress = (DWORD)GetProcAddress(user32,"MessageBoxA");
oMessageBoxA = (BOOL (WINAPI*)(HWND, LPCTSTR, LPCTSTR, UINT))MessageBoxAddress;
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)oMessageBoxA, hMessageBoxA);
DetourTransactionCommit();
}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved)
{
if(fdwReason==DLL_PROCESS_ATTACH)
{
patch();
}
}
您错误地声明了MessageBoxA()
的签名,并且您对DWORD MessageBoxAddress
的使用在 64 位 DLL 中不起作用。
请尝试以下 DLL 代码:
#include <Windows.h>
#include <detours.h>
#pragma comment(lib,"detours.lib")
typedef int (WINAPI* LPFN_MBA)(HWND, LPCSTR, LPCSTR, UINT);
LPFN_MBA oMessageBoxA = NULL;
int WINAPI hMessageBoxA( HWND hWnd, LPCSTR lpText, LPCSTR lpCaption,UINT uType)
{
return oMessageBoxA(hWnd,"HOOKED",lpCaption,uType);
}
void patch()
{
HMODULE user32 = GetModuleHandle(TEXT("user32.dll"));
if (user32 != NULL)
{
oMessageBoxA = (LPFN_MBA) GetProcAddress(user32, "MessageBoxA");
if (oMessageBoxA != NULL)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach((PVOID*)&oMessageBoxA, hMessageBoxA);
DetourTransactionCommit();
}
}
}
void unpatch()
{
if (oMessageBoxA != NULL)
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach((PVOID*)&oMessageBoxA, hMessageBoxA);
DetourTransactionCommit();
}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(hinstDLL);
patch();
}
else if (fdwReason == DLL_PROCESS_DETACH)
{
unpatch();
}
}
有关更多详细信息,请阅读以下内容:
带有 MS 绕道的 API 挂钩
相关文章:
- 当回溯以零开始时,如何调试崩溃
- boost::进程间消息队列引发错误
- 内联映射初始化的动态atexit析构函数崩溃
- 执行函数时导致崩溃的变量
- 在线编译器中的分段C++没有打印消息
- 当通过tcp接收编码图像的消息时,zmq在第二个循环中崩溃
- Qt应用程序在每个消息框上崩溃
- 绕道 3.0 钩子崩溃消息框 A.
- Borland C++在没有消息的情况下崩溃
- boost::asio 中的未经请求的消息使应用程序崩溃,没有 SSL 它可以正常工作,为什么
- 向TEdit发送消息后崩溃
- 我想在应用程序因内存耗尽而崩溃时显示消息框
- 如果安装在单独的类上,Qt消息处理程序将崩溃
- C++程序在执行时崩溃且没有错误消息 con = 驱动程序>连接(主机、用户、通行证);
- 程序崩溃并显示消息"terminate called recursively"而不引发任何异常
- 分配消息崩溃在google protobuf
- 如果没有QDebug消息,Qt应用程序就会崩溃
- 具有多重继承的消息映射MFC:如何避免警告C4407和运行时崩溃
- 插入C++会触发程序消息,然后崩溃
- JNI_CreateJavaVM使c++应用程序崩溃,没有错误消息