简介
目前我正在使用某人的类来读取内存中的字节(通过指针)。到目前为止我的代码完美无缺,我真的不想改变类的设置方式(如果可行)(因为它可以工作),但希望可以对类和我的代码进行一些小的调整以使其高效
我目前取得的成就:
从内存中的指针地址开始,在该地址读取一个字节,将数据添加到数组,将原始地址加1,这样我们现在可以读取下一个地址(例如原始地址是24004,存储字节后,增量1和下一个要读取的地址变为24005。
读取下一个地址(24005)的字节,将其添加到同一个数组,将该地址加1(读取次数变为24006)。
等等,进行固定次数的迭代(约10,000次)。
问题:
一次接一次地对readprocessmemory进行10,000次调用会导致系统延迟20秒,同时还会影响其业务。
我希望能够实现的目标:
仅执行一次Readprocessmemory,指定要读取的10,000字节数据(而不是10,000次迭代一次只有一个字节),以与之前单个字节相同的格式将其保存到数组中(我知道因此,而不是数组{0}(1)(2)..等..我现在只有数组{0},所以我想我需要一个有效的方法将这个非常大的数字分成10,000个数字(在另一个数组中))存储在每个地址的数据是整数。因此对于5个地址:数组{12345}变为{1} {2} {3} {4} {5}。例如,1 2 3 4或5也可以是1 200 40 43或20。
理想情况下,如果我可以挥动我的新手魔杖,它会看起来像这样(下面的课程以及我到目前为止的课程):
迭代代码:
private void label1_Click(object sender, EventArgs e)
{
int[] valuesSeperated[200];
List<byte> PreArray = new List<byte>();
Process[] test = Process.GetProcessesByName("MyProcess"); //Get process handle
int baseAddress = test[0].MainModule.BaseAddress.ToInt32(); //Get base address
byte ReadX = MyClass.ReadPointerByte("MyProcess", BaseAddress, new int[] { 0xc, 0x0, 0x2 }); //call memory reading function (including memory offsets)
PreArray.Add(ReadX);
byte[] PreArrayToInt = PreArray.ToArray();
int[] MYConvertedBytes = PreArray ToInt.Select(x => (int)x).ToArray();
foreach (int i in MYConvertedBytes)
{
valuesSeperated // (don't really know what to do here, if the read was successful I would have a long number at [0], so now need to separate these as if I had read each one in memory one at a time.
}
//new array with 10,000 values created.
}
的类别: 的
[DllImport("kernel32", EntryPoint = "ReadProcessMemory")]
private static extern byte ReadProcessMemoryByte(int Handle, int Address, ref byte Value, int Size, ref int BytesRead);
public static byte ReadPointerByte(string EXENAME, int Pointer, int[] Offset)
{
byte Value = 0;
checked
{
try
{
Process[] Proc = Process.GetProcessesByName(EXENAME);
if (Proc.Length != 0)
{
int Bytes = 0;
int Handle = OpenProcess(PROCESS_ALL_ACCESS, 0, Proc[0].Id);
if (Handle != 0)
{
foreach (int i in Offset)
{
ReadProcessMemoryInteger((int)Handle, Pointer, ref Pointer, 4, ref Bytes);
Pointer += i;
}
ReadProcessMemoryByte((int)Handle, Pointer, ref Value, 2, ref Bytes);
CloseHandle(Handle);
}
}
}
catch
{ }
}
return Value;
}
答案 0 :(得分:3)
一些建议:
不要在“托管包装”(ReadPointerByte或其他......)中调用OpenProcess / CloseHandle,您可以使用Process.Handle属性。
为了避免与页面权限相关的可能错误,您可能需要用VirtualProtectEx包装对ReadProcessMemory的调用(一个用于'unprotect',允许读取,另一个用于'恢复'以前的保护)。
一些代码:
// from http://www.pinvoke.net/default.aspx/kernel32.readprocessmemory
[DllImport("kernel32", EntryPoint = "ReadProcessMemory")]
private static extern bool ReadProcessMemory(IntPtr Handle, IntPtr Address,
[Out] byte[] Arr, int Size, out int BytesRead);
public static byte[] ReadBytes(IntPtr hnd, IntPtr Pointer, int Length)
{
byte[] Arr = new byte[Length];
int Bytes = 0;
if(!ReadProcessMemory(hnd, Pointer, Arr, Length, out Bytes)){
// Throw exception ...
}
// Check if Bytes == Length ...
return Arr;
}
private void button1_Click(object sender, EventArgs e)
{
//Get process handle
Process[] test = Process.GetProcessesByName("notepad++");
//Get base address
IntPtr baseAddress = test[0].MainModule.BaseAddress;
int bytesToRead = 16;
int[] valuesSeparated = new int[bytesToRead / 4];
byte[] ret = ReadBytes(test[0].Handle, baseAddress, bytesToRead);
// Interpret ret as you like ...
// Convert ret to int[]
valuesSeparated = ....
}
[迭代过程]
这里有两个选项:
无论目标 - 进程 - 内存 - 读取技术如何,您的算法都是这样的(我真的很难将您的文本翻译成实现......):
输入:进程的基本地址,偏移量
输出: byte [] Ret
<强>步骤:强>
1: LastPtr = BaseAddress
2: IdxOffset = 0
3: Offset = Offsets[IdxOffset]
4: LastValue = Memory[LastPtr + Offset]
5: Ret.Add(LastValue)
6: IdxOffset++
7: LastPtr = LastValue // Is this it?????
8: If IdxOffset < Offsets.Length then return else goto 3