Global $TItle="我的 GUI",$str='我是头 要找的数据 我是尾'
GUICreate("读取内存",400,200,default,default,default)
$Button1 = GUICtrlCreateButton("读 取", 312, 150, 75, 40)
$Button2 = GUICtrlCreateLabel($str, 5, 5, 370, 30)
$Button3 = GUICtrlCreateLabel("2", 5, 35, 370, 30)
GUISetState(@SW_SHOW)
While 1
Switch GUIGetMsg()
Case -3
ExitLoop
Case $Button1
_test()
EndSwitch
WEnd
Func _test()
$Handle=_MemoryOpen(@AutoItPID)
If Not IsArray($Handle) Then
Return MsgBox(16, '', '读取进程出错')
EndIf
Local $Addr, $sHex, $iSecq, $iLen, $i = 0
Local $sMatch1 = StringToBinary('我是头')
$sMatch1 = StringTrimLeft($sMatch1, 2) ;删除开头的 "0x"
Local $sMatch2 = StringToBinary('我是尾')
$sMatch2 = StringTrimLeft($sMatch2, 2) ;删除开头的 "0x"
;CE D2 CA C7 CD B7 20 D2 AA D5 D2 B5 C4 CA FD BE DD 20 CE D2 CA C7 CE B2
Local $sMatch = $sMatch1 & '\w{10,40}' & $sMatch2
ConsoleWrite( $sMatch & @CRLF)
$Addr = 0x40000 ;程序 通常的 起始地址
While 1 ;地址顺序,搜索内存数据
If $Addr > 0x10000000 Then ExitLoop ; 设定的搜索最大地址.
$sHex=_MemoryRead($Addr,$Handle,'byte[5000]') ; 5000 比 4500 大,保证读取时,不会截断搜索的数据
If StringRegExp($sHex, $sMatch) Then
$iSecq = StringInStr($sHex, $sMatch1)
If $iSecq > 0 Then
$Addr = $Addr+Round($iSecq/2)-2 ;读内存中数据, 1 byte = 2个数字组成的数据
ConsoleWrite('地址已找到: ' & Hex($Addr, 8) & @CRLF)
$iLen = BinaryLen(StringToBinary($str))
$str = _MemoryRead($Addr, $Handle, 'char[' & $iLen & ']')
$sHex = _MemoryRead($Addr, $Handle, 'byte[' & $iLen & ']')
MsgBox(0, '数据找到' , '直接读取文字 : ' & $str & @CRLF & '读取后转化的文字: ' & BinaryToString($sHex))
Return
EndIf
EndIf
$Addr += 4500
$i+=1
GUICtrlSetData($Button3, $i & @TAB & Hex($Addr, 8))
WEnd
EndFunc
Func _MemoryOpen($iv_Pid, $iv_DesiredAccess = 0x1F0FFF, $if_InheritHandle = 1)
If Not ProcessExists($iv_Pid) Then
SetError(1)
Return 0
EndIf
Local $ah_Handle[2] = [DllOpen('kernel32.dll')]
If @error Then
SetError(2)
Return 0
EndIf
Local $av_OpenProcess = DllCall($ah_Handle[0], 'int', 'OpenProcess', 'int', $iv_DesiredAccess, 'int', $if_InheritHandle, 'int', $iv_Pid)
If @error Then
DllClose($ah_Handle[0])
SetError(3)
Return 0
EndIf
$ah_Handle[1] = $av_OpenProcess[0]
Return $ah_Handle
EndFunc ;==>_MemoryOpen
Func _MemoryRead($iv_Address, $ah_Handle, $sv_Type = 'dword')
If Not IsArray($ah_Handle) Then
SetError(1)
Return 0
EndIf
Local $v_Buffer = DllStructCreate($sv_Type)
If @error Then
SetError(@error + 1)
Return 0
EndIf
DllCall("kernel32.dll", 'int', 'ReadProcessMemory', 'int', $ah_Handle[1], 'int', $iv_Address, 'ptr', DllStructGetPtr($v_Buffer), 'int', DllStructGetSize($v_Buffer), 'int', '')
If Not @error Then
Local $v_Value = DllStructGetData($v_Buffer, 1)
Return $v_Value
Else
SetError(6)
Return 0
EndIf
EndFunc ;==>_MemoryRead