GetModuleHandle 无法正常工作
GetModuleHandle does not work properly
在调试并尝试解决此问题几个小时后,我将尝试在放弃该项目之前再次解决此问题。
给定以下代码,我尝试将 DLL 注入纸牌以操纵乐谱,但它不起作用,应用程序在最终操作时崩溃。
void SetGameTime(int time)
{
MessageBox(NULL, "Func just begins here - ", "Func start", MB_OK | MB_ICONHAND);
DWORD baseAddress = (DWORD) GetModuleHandle(0);
baseAddress = *(DWORD *) (baseAddress + BASE_OFS_DEF);
DWORD temp = *(DWORD *)(baseAddress + SCORE_OFS1_DEF);
DWORD temp2 = *(DWORD *)(temp + SCORE_OFS2_DEF);
*(DWORD*) temp2 = 500; // solitaire crashes right here
}
那我做错了什么呢?我试图在使用调试器时获得错误的逻辑,由于整个函数,"eax"为 0。
调试器输出:
SetGameTime(500);
703E1284 push 10h
703E1286 push 703E3170h
703E128B push 703E317Ch
703E1290 push 0
703E1292 call dword ptr ds:[703E30ACh] // GetModuleHandle operation (?)
703E1298 push 0
703E129A call dword ptr ds:[703E3000h] // base Address operation (?)
703E12A0 mov eax,dword ptr [eax+97074h] // eax keeps zero so the wrong function has to be GetModuleHandle?
703E12A6 mov eax,dword ptr [eax+2Ch] // first offset of score
703E12A9 mov eax,dword ptr [eax+10h] // and the second one...
703E12AC mov dword ptr [eax],1F4h // unhandled exception, no access to write at pos 0x00000000
return 0;
您应该检查返回值。以下是GetModuleHandle
文档:
If the function fails, the return value is NULL.
To get extended error information, call GetLastError.
显示的程序集解释不正确。以下是它的作用:
SetGameTime(500);
703E1284 push 10h // four arguments of MessageBox call
703E1286 push 703E3170h
703E128B push 703E317Ch
703E1290 push 0
703E1292 call dword ptr ds:[703E30ACh] // MessageBox call
703E1298 push 0 // argument for GetModuleHandle()
703E129A call dword ptr ds:[703E3000h] // GetModuleHandle call
我认为你// eax keeps zero...
的评论具有误导性。
对GetModuleHandle
的调用(这是703E129A call ptr ds:[703E3000h]
)不返回 0,而是返回 0x400000。在调试器中检查 Plase,如果需要,调用 GetLastError() 看看原因,但我相信没问题。
如果你使用Visual Studio,这也是一个很好的提示。将以下内容添加到调试器的监视窗口:@err, hr
。这将确保您始终看到最后一个错误的值,以及相应的消息(如果有)。
您的常量(BASE_OFS_DEF & co)似乎无效。你从哪里得到它们?您阅读了baseAddress + BASE_OFS_DEF
的内容并将其视为有效地址,但我相信它不是。稍后,您将对SCORE_OFS2_DEF执行类似的操作。
似乎写在地址 temp + SCORE_OFS2_DEF
的值为 0,您将其分配给 temp2
,从而获得访问冲突。在调试器中监视baseAddress
、temp
和temp2
,您将看到。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程