btjily 发表于 2011-4-5 10:15:33

VB2005中ReadProcessMenory问题

模块代码:
Module Module1
--------------------------------------------------------------------------------------------------------------------------------------
    Public Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    Public Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer
    Public Declare Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
    Public Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer
    Public Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Integer) As Integer
    Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As Integer, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByRef lpNumberOfBytesWritten As Integer) As Integer
    Public Const PROCESS_ALL_ACCESS As Integer = &H1F0FFF

End Module
-------------------------------------------------------------------------------------------------------------------------------------
窗体代码:
Public Class Form1
    Dim hwd, pid, hProcess,base1, base2, hp, hpmax As Integer

    Private Sub Form1_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
      CloseHandle(hProcess)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      hwd = FindWindow("ZElementClient Window", "Element Client")
      GetWindowThreadProcessId(hwd, pid)
      hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid)
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
      hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, pid)
      If hProcess Then
                        Call ReadProcessMemory(hProcess, &H8F77D4, base1, 4, 0)
            Call ReadProcessMemory(hProcess, base1 + &H28S, base2, 4, 0)
            Call ReadProcessMemory(hProcess, base2 + &H254S, hp, 4, 0)
            Call ReadProcessMemory(hProcess, base2 + &H26CS, hpmax, 4, 0)

            Label6.Text = hp & "-" & hpmax
            CloseHandle(hProcess)
      End If
    End Sub
End Class

蓝色海洋 发表于 2011-4-5 10:15:44

参考一下:

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpBaseAddress As Long, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Long) As Long
Public Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
.
.
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
   
ReadProcessMemory hProcess, ByVal Baddr, h, 4, 0&
BaseAddr1 = h + &H28
ReadProcessMemory hProcess, ByVal BaseAddr1, h, 4, 0&
BaseAddr1 = h
   

NameAddr = BaseAddr1 + &H3A0    ' --------------------------------------- 主角NAME
   
HPAddr = BaseAddr1 + &H254      ' --------------------------------------- 主角HP
MaxHPAddr = BaseAddr1 + &H26C
MPAddr = BaseAddr1 + &H258      ' --------------------------------------- 主角MP
MaxMPAddr = BaseAddr1 + &H270
.
.
ReadProcessMemory hProcess, ByVal HPAddr, HP, 4, 0&
ReadProcessMemory hProcess, ByVal MaxHPAddr, MaxHP, 4, 0&
.
.
CloseHandle hProcess

蓝色海洋 发表于 2011-4-5 10:15:55

最好不要象:
Call ReadProcessMemory(hProcess, base1 + &H28S, base2, 4, 0)
这样在里面使用运算,有时候会有些好奇怪的问题。

另外开头时候的声明很重要,尤其是类型,还有就是写不写ByVal、ByRef的问题。所有这些都会有影响的。
页: [1]
查看完整版本: VB2005中ReadProcessMenory问题