Code:
''' <summary> Function used to read memory. </summary>
''' <param name="P">The process to read memory from.</param>
''' <param name="memAdr">The memory address that will be read.</param>
Public Function ReadByte(ByVal P As Process, ByVal memAdr As UInt32) As Byte
Return ReadBytes(P, memAdr, 1)(0) 'Since the array only holds 1 byte, we just return it.
End Function
''' <summary> Function used to read memory. </summary>
''' <param name="P">The process to read memory from.</param>
''' <param name="memAdr">The memory address that will be read.</param>
''' <param name="startIndex">The index of the bit to read from(0 to 7).</param>
Public Function ReadBit(ByVal P As Process, ByVal memAdr As Int32, ByVal startIndex As Integer) As Boolean
If (startIndex <= 7 And startIndex >= 0) Then '1 Byte == 8 Bits. ALWAYS REMEMBER THIS!
Return ReadByte(P, memAdr) = (ReadByte(P, memAdr) Or (2 ^ startIndex)) ' 2^0 = 1(00000001), 2^1 = 2(00000010), 2^2 = 4(00000100) (...);
End If
Return 2
End Function
''' <summary> Function used to read memory. </summary>
''' <param name="P">The process to read memory from.</param>
''' <param name="memAdr">The memory address that will be read.</param>
''' <param name="length">The amount of bytes to read from the specified address.</param>
Public Function ReadBytes(ByVal P As Process, ByVal memAdr As UInt32, ByVal length As UInteger) As Byte()
Dim buffer As Byte() = New Byte(length - 1) {} 'Creates our buffer.
If Not ReadProcessMemory(P.Handle, New IntPtr(CLng(memAdr)), buffer, length, 0) Then buffer = Nothing 'Reads the memory in that address, and writes it to the buffer. If there was an error while reading memory, then it'll return null.
Return buffer 'Returns the buffer.
End Function
Public Function WriteByte(ByVal P As Process, ByVal memAdr As UInt32, ByVal value As Byte) As Boolean
Return WriteBytes(P, memAdr, BitConverter.GetBytes(value), 1)
End Function
''' <summary> Function used to write memory. </summary>
''' <param name="P">The process to read memory from.</param>
''' <param name="memAdr">The memory address that will be edited.</param>
''' <param name="bitvalue">The boolean value that will be written to the speficied bit.</param>
''' <param name="startIndex">The index of the bit to read from(0 to 7).</param>
Public Function WriteBit(ByVal P As Process, ByVal memAdr As UInt32, ByVal startIndex As UInteger, bitValue As Boolean) As Boolean
'!! Read Comments on ReadBit aswell !!
If (startIndex <= 7) Then
If bitValue = True Then
Return WriteByte(P, memAdr, (ReadByte(P, memAdr) Or (2 ^ startIndex))) ' 0 or 1 = 1 || 1 or 1 = 1 , so it forces the bit into being 1, regardless of what value it previously had.
Else : If ReadBit(P, memAdr, startIndex) = True Then Return WriteByte(P, memAdr, (ReadByte(P, memAdr) Xor (2 ^ startIndex))) ' 1 xor 1 = 0 || 0 xor 1 = 1 , so we need to add a check so it remains 0.
End If
End If
Return False
End Function
''' <summary> Function used to write memory. </summary>
''' <param name="P">The process to read memory from.</param>
''' <param name="memAdr">The memory address that will be edited.</param>
''' <param name="length">The amount of bytes to read from the specified address.</param>
''' <param name="bytes">The amount of bytes to read from the specified address.</param>
Public Function WriteBytes(ByVal P As Process, ByVal memAdr As Int32, ByVal bytes As Byte(), ByVal length As UInteger) As Boolean
Return WriteProcessMemory(P.Handle, New IntPtr(memAdr), bytes, length, 0) 'Returns True if sucessful writing the memory, False if not.
End Function
#Region "Declared Functions"
''' <summary> Writes data to an area of memory in a specified process. The entire area to be written to must be accessible or the operation fails.</summary>
''' <param name="hProcess">A handle to the process memory to be modified. The handle must have PROCESS_VM_WRITE and PROCESS_VM_OPERATION access to the process.</param>
''' <param name="lpBaseAddress">A pointer to the base address in the specified process to which data is written. Before data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for write access, and if it is not accessible, the function fails.</param>
''' <param name="lpBuffer">A pointer to the buffer that contains data to be written in the address space of the specified process.</param>
''' <param name="nSize">The number of bytes to be written to the specified process.</param>
''' <param name="lpNumberOfBytesWritten">A pointer to a variable that receives the number of bytes transferred into the specified process. This parameter is optional. If lpNumberOfBytesWritten is NULL, the parameter is ignored.</param>
'''<remarks>WriteProcessMemory copies the data from the specified buffer in the current process to the address range of the specified process. Any process that has a handle with PROCESS_VM_WRITE and PROCESS_VM_OPERATION access to the process to be written to can call the function. Typically but not always, the process with address space that is being written to is being debugged.
'''The entire area to be written to must be accessible, and if it is not accessible, the function fails.</remarks>
Public Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer() As Byte, ByVal nSize As UInteger, ByVal lpNumberOfBytesWritten As UIntPtr) As Boolean
''' <summary> Reads data from an area of memory in a specified process. The entire area to be read must be accessible or the operation fails.</summary>
''' <param name="hProcess">A handle to the process with memory that is being read. The handle must have PROCESS_VM_READ access to the process.</param>
''' <param name="lpBaseAddress">A pointer to the base address in the specified process from which to read. Before any data transfer occurs, the system verifies that all data in the base address and memory of the specified size is accessible for read access, and if it is not accessible the function fails.</param>
''' <param name="lpBuffer">A pointer to a buffer that receives the contents from the address space of the specified process.</param>
''' <param name="nSize">The number of bytes to be read from the specified process.</param>
''' <param name="lpNumberOfBytesRead">A pointer to a variable that receives the number of bytes transferred into the specified buffer. If lpNumberOfBytesRead is NULL, the parameter is ignored.</param>
'''<remarks>WriteProcessMemory copies the data from the specified buffer in the current process to the address range of the specified process. Any process that has a handle with PROCESS_VM_WRITE and PROCESS_VM_OPERATION access to the process to be written to can call the function. Typically but not always, the process with address space that is being written to is being debugged.
'''The entire area to be written to must be accessible, and if it is not accessible, the function fails.</remarks>
Public Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As IntPtr, ByVal lpBuffer() As Byte, ByVal nSize As UInteger, ByRef lpNumberOfBytesRead As UIntPtr) As Boolean
#End Region