GetModuleHandle 无法正常工作

GetModuleHandle does not work properly

本文关键字:工作 常工作 GetModuleHandle      更新时间:2023-10-16

在调试并尝试解决此问题几个小时后,我将尝试在放弃该项目之前再次解决此问题。

给定以下代码,我尝试将 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 ,从而获得访问冲突。在调试器中监视baseAddresstemptemp2,您将看到。