同志们帮我看看,我就这么点米了,大家行行好,程序附上了(150分)

  • 主题发起人 主题发起人 Fermi
  • 开始时间 开始时间
F

Fermi

Unregistered / Unconfirmed
GUEST, unregistred user!
这个程序的目的是先运行一个游戏(伯德之门2),然后用gamemaster找到所需要的变量的地址,<br>将其能够显示在这个程序中。我找到这个游戏的handle是没有问题的,和gm提示的一样,可是为什么<br>显示出来的内存信息都是零呢?不光是游戏,甚至换成其他程序也是这样。<br>同志们帮帮忙,小弟比较穷,就这么点米了<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>h:thandle;<br>lpBuffer:pByte; <br>&nbsp; nSize: DWORD; <br>&nbsp; lpNumberOfBytesRead: DWORD; <br>&nbsp; i:integer; <br>&nbsp; s:string; <br><br>begin<br>h:=findwindow('ChitinClass','Baldur''s Gate II - Shadows of Amn - Throne of Bhaal');<br>if h&lt;&gt;0 then<br>begin<br>unit2.Form1.caption:=inttostr(h);<br>nSize:=4;<br>&nbsp; lpBuffer:=AllocMem(nSize);<br>&nbsp; for i:=$02B928AE to $02B928FE do //此处的地址用的是gamemaster找到的偏移地址<br>&nbsp; begin<br>&nbsp; &nbsp; ReadProcessMemory(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;h,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Pointer(i),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpBuffer,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nSize,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpNumberOfBytesRead<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;); <br>&nbsp; &nbsp; s:=s+intTohex(lpBuffer^,2)+' '; <br><br>&nbsp; &nbsp; if (i mod 16) =0 then <br>&nbsp; &nbsp; begin <br>&nbsp; &nbsp; &nbsp; Memo1.Lines.Add(s); <br>&nbsp; &nbsp; &nbsp; s:=''; <br>&nbsp; &nbsp; end; <br><br>&nbsp; end; <br>&nbsp; FreeMem(lpBuffer,nSize); <br><br>end;<br><br>end;
 
参考Win32 API对ReadProcessMemory函数的定义:<br>BOOL ReadProcessMemory(<br>&nbsp; &nbsp; HANDLE hProcess, // handle of the process whose memory is read &nbsp;<br>&nbsp; &nbsp; LPCVOID lpBaseAddress, // address to start reading<br>&nbsp; &nbsp; LPVOID lpBuffer, // address of buffer to place read data<br>&nbsp; &nbsp; DWORD nSize, // number of bytes to read<br>&nbsp; &nbsp; LPDWORD lpNumberOfBytesRead // address of number of bytes read<br>&nbsp; &nbsp;);<br>第一个参数应该是进程句柄,而你用FindWindow得到的是窗口句柄,<br>另外你应该判断一下ReadProcessMemory的返回值以确定是否发生错误。<br><br>取得一个窗口对应的进程ID可以调用GetWindowThreadProcessId函数,<br>得到进程ID后再调用OpenProcess函数取得对应的线程句柄,然后把得到的<br>进程句柄传给ReadProcessMemory。<br><br>示意代码:<br>WinHandle:=FindWindow('ChitinClass','Baldur''s Gate II - Shadows of Amn - Throne of Bhaal');<br>if WinHandle&lt;&gt;0 then<br>begin<br>&nbsp; ProcessID:=GetWindowThreadProcessId(WinHandle,nil);<br>&nbsp; ProcessHandle:=OpenProcess(PROCESS_VM_READ,false,ProcessID);<br>&nbsp; if ProcessHandle&lt;&gt;0 then<br>&nbsp; &nbsp; for i:=$02B928AE to $02B928FE do<br>&nbsp; &nbsp; &nbsp; if ReadProcessMemory(ProcessHandle,Pointer(i),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lpBuffer,nSize,lpNumberOfBytesRead)&lt;&gt;0 then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; // 做你想做的!<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else RaiseLastWin32Error;<br>end;
 
agree.<br>form的handle和process的handle不一样.
 
多人接受答案了。
 
skyweb,你给的解不对,GetWindowThreadProcessId所得到的是一个窗体对应的线程的id,而不是<br>进程的id,所以结果不正确。可是我已经给了分了,能不能再帮我看看啊,谢谢啦
 
不好意思,一个小错误:)<br>function GetWindowThreadProcessId(hWnd: HWND; lpdwProcessId: Pointer): DWORD; stdcall;<br>返回值是线程ID,参数lpdwProcessID返回的才是进程ID:)<br><br>下面是一个编译通过,可以正确运行的例子:<br><br>Form1.Edit1 : 窗口类名,用于FindWindow<br>Form1.Edit2 : 窗口名,用于FindWindow<br>Form1.Edit3 : 进程内存基址,用于ReadProcessMemory<br>Form1.Edit4 : 需要读出的字节数<br>Form1.Button1 : 读内存按钮<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; WinHandle,ProcessID,ProcessHandle: THandle;<br>&nbsp; BaseAddress: Pointer;<br>&nbsp; BytesToRead,<br>&nbsp; BytesReaded: DWord;<br>&nbsp; Buffer: PChar;<br>&nbsp; i: integer;<br>&nbsp; s: string;<br>begin<br>&nbsp; WinHandle:=FindWindow(PChar(Edit1.Text),PChar(Edit2.Text));<br>&nbsp; if WinHandle&lt;&gt;0 then<br>&nbsp; begin<br>&nbsp; &nbsp; GetWindowThreadProcessId(WinHandle,@ProcessID);<br>&nbsp; &nbsp; ProcessHandle:=OpenProcess(PROCESS_VM_READ,false,ProcessID);<br>&nbsp; &nbsp; if ProcessHandle&lt;&gt;0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; BaseAddress:=Pointer(StrToInt(Edit3.Text));<br>&nbsp; &nbsp; &nbsp; BytesToRead:=StrToInt(Edit4.Text);<br>&nbsp; &nbsp; &nbsp; Buffer:=AllocMem(BytesToRead);<br>&nbsp; &nbsp; &nbsp; if ReadProcessMemory(ProcessHandle,BaseAddress,<br>&nbsp; &nbsp; &nbsp; &nbsp; Buffer,BytesToRead,BytesReaded) then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; s:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; for i:=0 to BytesReaded-1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i&gt;0) and (i mod 16 = 0) then s:=s+#13#10;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s:=s+' '+IntToHex(Byte(Buffer),2);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; Memo1.Lines.Text:=s;<br>&nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; else RaiseLastWin32Error;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br><br><br>
 
后退
顶部