Imports System.Diagnostics
Imports System.Runtime.InteropServices
Shared Function WriteProcessMemory(hProcess As IntPtr, lpBaseAddress As IntPtr, lpBuffer As IntPtr, nSize As UInteger, ByRef nNumberOfBytesWritten As UInteger) As Boolean
Shared Function ReadProcessMemory(hProcess As IntPtr, lpBaseAddress As IntPtr, lpBuffer As IntPtr, nSize As UInteger, ByRef lpNumberOfBytesRead As UInteger) As Boolean
Public Shared Sub Main(args As String())
Dim myAge As Integer = 23
Dim pAgeAddress, pResultAge As IntPtr
pAgeAddress = Marshal.AllocHGlobal(4)
pResultAge = Marshal.AllocHGlobal(4)
Marshal.StructureToPtr(myAge, pAgeAddress, False)
Dim reads, written As UInteger
If Not ReadProcessMemory(Process.GetCurrentProcess().Handle, pAgeAddress, pResultAge, 4, reads) Then
Console.WriteLine("메모리 읽기 실패!")
Console.WriteLine("메모리 읽기 성공!")
Console.WriteLine("myAge 변수의 값: {0}", myAge)
Console.WriteLine("pResultAge 변수가 가리키는 주소의 값: {0}", Marshal.ReadInt32(pResultAge))
Marshal.WriteInt32(pResultAge, 777)
If Not WriteProcessMemory(Process.GetCurrentProcess().Handle, pAgeAddress, pResultAge, 4, written) Then
Console.WriteLine("메모리 쓰기 실패!")
Console.WriteLine("메모리 쓰기 성공!")
Console.WriteLine("pAgeAddress 변수가 가리키는 주소의 값: {0}", Marshal.ReadInt32(pAgeAddress))
Console.WriteLine("pResultAge 변수가 가리키는 주소의 값: {0}", Marshal.ReadInt32(pResultAge))