elite*gold: 0
Join Date: May 2005
Posts: 562
Received Thanks: 0
|
#include-once
#region _Mem()
;================================================= =====================
; AutoIt Version: 3.1.127 (beta)
; Language: English
; Platform: All Windows
; Author: Nomad
; Requirements:
; These functions will only work with beta. If you place this
; script in your include folder, you can use these functions
; just as any other function, as long as you put
; "#include <Memory.au3>" in your script (or w/e you name it).
;================================================= =====================
; Credits:
; wOuter:
; These functions are based on his original _Mem() functions.
; They are easier to use and comprehend, IMO. These functions
; are in no way a direct copy of his functions. His functions
; only provided a foundation from which these evolved. The
; biggest changes were made made to _MemRead() and _MemWrite().
; You also no longer need to use _MemCreate() before using
; _MemWrite().
;================================================= =====================
; Additional:
; I've never used _MemRev(), _MemAlloc(), _MemFree(), or _MemText()
; So I did not attempt to revise them in any way and they are not
; included in this script.
;
; Please remember to credit accordingly for any use of these UDF's.
;================================================= =====================
;================================================= =====================
; Function Name: _MemOpen()
;
; Description: Opens the process specified by the Process ID and
; returns the necessary Dll information for reading
; from it's memory.
;
; Parameters:
; $Pid = The Process ID of the program you want to open.
;
; $InheritHandle = [optional] If this value is TRUE, processes
; created by this process will inherit the
; handle. Otherwise, the processes do not
; inherit this handle. Most users will not
; need to alter this parameter.
;
; Returns:
; On Success: Returns an array containing the .dll and an open handle
; to the specified process.
; On Failure: Returns 0 and sets error to 1
;================================================= =====================
Func _MemOpen($Pid, $InheritHandle = 0x1F0FFF)
Local $Dll[2] = [DllOpen('kernel32.dll')]
Local $OpenProcess = DllCall($Dll[0], 'int', 'OpenProcess', 'int', $InheritHandle, 'int', 0, 'int', $Pid)
If @Error Then
DllClose($Dll[0])
SetError(1)
Return 0
EndIf
$Dll[1] = $OpenProcess[0]
Return $Dll
EndFunc
;================================================= =====================
; Function Name: _MemRead()
;
; Description: Reads the value located in the memory address
; specified by the $Address parameter. You must open
; the process first with _MemOpen()
;
; Parameters:
; $Address = The memory address you want to read from.
; It must be in hex format (0x00000000).
;
; $Dll = The necessary Dll information which is an array returned
; from _MemOpen().
;
; $Type = [optional] The "Type" of value you intend to read.
; This is set to 'dword'(32bit(4byte) signed integer)
; by default. See the help file for DllStructCreate
; for all types. For example, if you want to read a
; word that is 15 characters in length, you would use
; 'char[15]' since a 'char' is 8 bits (1 byte) in size.
;
; Additional:
; Values returned are in Decimal format, unless specified as a
; 'char' type, then they are returned in ASCII format.
;
; Returns:
; On Success: Returns the value located at the specified address
; On Failure: Returns 0 and sets error to 1
;================================================= =====================
Func _MemRead($Address, $Dll, $Type = 'dword')
If Not IsArray($Dll) Then
SetError(1)
Return 0
EndIf
Local $LpBuffer = DllStructCreate($Type)
DllCall($Dll[0], 'int', 'ReadProcessMemory', 'int', $Dll[1], 'int', $Address, 'ptr', DllStructGetPtr($LpBuffer), 'int', DllStructGetSize($LpBuffer), 'int', '')
If Not @Error Then
$Value = DllStructGetData($LpBuffer, 1)
Return $Value
Else
SetError(1)
Return 0
EndIf
EndFunc
;================================================= =====================
; Function Name: _MemWrite()
;
; Description: Writes data to the specified memory address
;
; Parameters:
; $Address = The memory address which you want to write to.
; Must be in hex format (0x00000000).
;
; $Dll = The necessary Dll information which is an array returned
; from _MemOpen().
;
; $Value = The information you want to write.
;
; $Type = [optional] The "Type" of value you intend to write.
; This is set to 'dword'(32bit(4byte) signed integer)
; by default. See the help file for DllStructCreate
; for all types. If you want to write a 'char' type,
; you will use 'char[15]' if it's 15 characters in
; length.
;
; Returns:
; On Success: Returns 1
; On Failure: Returns 0 and sets error to 1
;================================================= =====================
Func _MemWrite($Address, $Dll, $Value, $Type = 'dword')
If Not IsArray($Dll) Then
SetError(1)
Return 0
EndIf
Local $LpBuffer = DllStructCreate($Type)
DllStructSetData($LpBuffer, 1, $Value, 1)
DllCall($Dll[0], 'int', 'WriteProcessMemory', 'int', $Dll[1], 'int', $Address, 'ptr', DllStructGetPtr($LpBuffer), 'int', DllStructGetSize($LpBuffer), 'int', '')
If Not @Error Then
Return 1
Else
SetError(1)
Return 0
EndIf
EndFunc
;================================================= =====================
; Function Name: _MemClose()
;
; Description: Closes the process which was opened using _MemOpen().
;
; Parameters:
; $Dll = The necessary Dll information which is an array returned
; from _MemOpen().
;
; Returns:
; On Success: Returns 1
; On Failure: Returns 0 and sets error to 1
;================================================= =====================
Func _MemClose($Dll)
If Not IsArray($Dll) Then
SetError(1)
Return 0
EndIf
DllCall($Dll[0], 'int', 'CloseHandle', 'int', $Dll[1])
If Not @Error Then
DllClose($Dll[0])
Return 1
Else
DllClose($Dll[0])
SetError(1)
Return 0
EndIf
EndFunc
#endregion
|