Results 1 to 11 of 11

Thread: Reading memory from another process (ReadProcessMemory API)

Threaded View

  1. #2

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Reading memory from another process (ReadProcessMemory API)

    If you want to use the class described in the above post, just add a new Class to your project and name it NativeMemoryReader, then copy and paste the code below into the code file for that class (replacing anything that was in there).
    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class NativeMemoryReader : Implements IDisposable
    4.  
    5.  
    6. #Region "API Definitions"
    7.  
    8.     <DllImport("kernel32.dll", EntryPoint:="OpenProcess", SetLastError:=True)> _
    9.     Private Shared Function OpenProcess(ByVal dwDesiredAccess As UInteger, <MarshalAsAttribute(UnmanagedType.Bool)> ByVal bInheritHandle As Boolean, ByVal dwProcessId As UInteger) As IntPtr
    10.     End Function
    11.  
    12.     <DllImportAttribute("kernel32.dll", EntryPoint:="ReadProcessMemory", SetLastError:=True)> _
    13.     Private Shared Function ReadProcessMemory(<InAttribute()> ByVal hProcess As System.IntPtr, <InAttribute()> ByVal lpBaseAddress As System.IntPtr, <Out()> ByVal lpBuffer As Byte(), ByVal nSize As UInteger, <OutAttribute()> ByRef lpNumberOfBytesRead As UInteger) As <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)> Boolean
    14.     End Function
    15.  
    16.     <DllImport("kernel32.dll", EntryPoint:="CloseHandle", SetLastError:=True)> _
    17.     Private Shared Function CloseHandle(ByVal hObject As IntPtr) As <MarshalAsAttribute(UnmanagedType.Bool)> Boolean
    18.     End Function
    19.  
    20. #End Region
    21.  
    22.  
    23. #Region "Private Fields"
    24.  
    25.     Private _TargetProcess As Process = Nothing
    26.     Private _TargetProcessHandle As IntPtr = IntPtr.Zero
    27.     Const PROCESS_VM_READ As UInteger = 16
    28.     Const PROCESS_QUERY_INFORMATION As UInteger = 1024
    29.  
    30. #End Region
    31.  
    32.  
    33. #Region "Public Properties"
    34.  
    35.     ''' <summary>
    36.     ''' The process that memory will be read from when ReadMemory is called
    37.     ''' </summary>
    38.     Public ReadOnly Property TargetProcess() As Process
    39.         Get
    40.             Return _TargetProcess
    41.         End Get
    42.     End Property
    43.  
    44.     ''' <summary>
    45.     ''' The handle to the process that was retrieved during the constructor or the last
    46.     ''' successful call to the Open method
    47.     ''' </summary>
    48.     Public ReadOnly Property TargetProcessHandle() As IntPtr
    49.         Get
    50.             Return _TargetProcessHandle
    51.         End Get
    52.     End Property
    53.  
    54. #End Region
    55.  
    56.  
    57. #Region "Public Methods"
    58.  
    59.     ''' <summary>
    60.     ''' Reads the specified number of bytes from an address in the process's memory.
    61.     ''' All memory in the specified range must be available or the method will fail.
    62.     ''' Returns Nothing if the method fails for any reason
    63.     ''' </summary>
    64.     ''' <param name="MemoryAddress">The address in the process's virtual memory to start reading from</param>
    65.     ''' <param name="Count">The number of bytes to read</param>
    66.     Public Function ReadMemory(ByVal MemoryAddress As IntPtr, ByVal Count As Integer) As Byte()
    67.         If _TargetProcessHandle = IntPtr.Zero Then
    68.             Me.Open()
    69.         End If
    70.         Dim Bytes(Count) As Byte
    71.         Dim Result As Boolean = ReadProcessMemory(_TargetProcessHandle, MemoryAddress, Bytes, CUInt(Count), 0)
    72.         If Result Then
    73.             Return Bytes
    74.         Else
    75.             Return Nothing
    76.         End If
    77.     End Function
    78.  
    79.     ''' <summary>
    80.     ''' Gets a handle to the process specified in the TargetProcess property.
    81.     ''' A handle is automatically obtained by the constructor of this class but if the Close
    82.     ''' method has been called to close a previously obtained handle then another handle can
    83.     ''' be obtained by calling this method. If a handle has previously been obtained and Close has
    84.     ''' not been called yet then an exception will be thrown.
    85.     ''' </summary>
    86.     Public Sub Open()
    87.         If _TargetProcess Is Nothing Then
    88.             Throw New ApplicationException("Process not found")
    89.         End If
    90.         If _TargetProcessHandle = IntPtr.Zero Then
    91.             _TargetProcessHandle = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION, True, CUInt(_TargetProcess.Id))
    92.             If _TargetProcessHandle = IntPtr.Zero Then
    93.                 Throw New ApplicationException("Unable to open process for memory reading. The last error reported was: " & New System.ComponentModel.Win32Exception().Message)
    94.             End If
    95.         Else
    96.             Throw New ApplicationException("A handle to the process has already been obtained, " & _
    97.                                            "close the existing handle by calling the Close method before calling Open again")
    98.         End If
    99.     End Sub
    100.  
    101.     ''' <summary>
    102.     ''' Closes a handle that was previously obtained by the constructor or a call to the Open method
    103.     ''' </summary>
    104.     Public Sub Close()
    105.         If Not _TargetProcessHandle = IntPtr.Zero Then
    106.             Dim Result As Boolean = CloseHandle(_TargetProcessHandle)
    107.             If Not Result Then
    108.                 Throw New ApplicationException("Unable to close process handle. The last error reported was: " & _
    109.                                                New System.ComponentModel.Win32Exception().Message)
    110.             End If
    111.             _TargetProcessHandle = IntPtr.Zero
    112.         End If
    113.     End Sub
    114.  
    115. #End Region
    116.  
    117.  
    118. #Region "Constructors"
    119.  
    120.     ''' <summary>
    121.     ''' Creates a new instance of the NativeMemoryReader class and attempts to get a handle to the
    122.     ''' process that is to be read by calls to the ReadMemory method.
    123.     ''' If a handle cannot be obtained then an exception is thrown
    124.     ''' </summary>
    125.     ''' <param name="ProcessToRead">The process that memory will be read from</param>
    126.     Public Sub New(ByVal ProcessToRead As Process)
    127.         If ProcessToRead Is Nothing Then
    128.             Throw New ArgumentNullException("ProcessToRead")
    129.         End If
    130.         _TargetProcess = ProcessToRead
    131.         Me.Open()
    132.     End Sub
    133.  
    134. #End Region
    135.  
    136.  
    137. #Region "IDisposable Support"
    138.  
    139.     Private disposedValue As Boolean
    140.  
    141.     Protected Overridable Sub Dispose(ByVal disposing As Boolean)
    142.         If Not Me.disposedValue Then
    143.             If Not _TargetProcessHandle = IntPtr.Zero Then
    144.                 Try
    145.                     CloseHandle(_TargetProcessHandle)
    146.                 Catch ex As Exception
    147.                     Debug.WriteLine("Error closing handle - " & ex.Message)
    148.                 End Try
    149.             End If
    150.         End If
    151.         Me.disposedValue = True
    152.     End Sub
    153.  
    154.     Protected Overrides Sub Finalize()
    155.         Dispose(False)
    156.         MyBase.Finalize()
    157.     End Sub
    158.  
    159.     ''' <summary>
    160.     ''' Releases resources and closes any process handles that are still open
    161.     ''' </summary>
    162.     Public Sub Dispose() Implements IDisposable.Dispose
    163.         Dispose(True)
    164.         GC.SuppressFinalize(Me)
    165.     End Sub
    166.  
    167. #End Region
    168.  
    169. End Class
    Last edited by chris128; Jun 10th, 2010 at 04:21 AM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width