介绍
目前我正在使用某人的类来读取内存中的字节(通过指针).到目前为止我的代码完美无缺,我真的不想改变类的设置方式(如果可行)(因为它可以工作),但希望可以对类和我的代码进行一些小的调整以使其高效.
我目前取得的成就:
从内存中的指针地址开始,在该地址读取一个字节,将数据添加到数组,将原始地址加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.
}
Run Code Online (Sandbox Code Playgroud)
类:
[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;
}
Run Code Online (Sandbox Code Playgroud)
一些建议:
不要在“托管包装器”(ReadPointerByte 或其他...)内调用 OpenProcess / CloseHandle,您可以使用Process.Handle属性。
为了避免与页面权限相关的可能错误,您可能需要使用VirtualProtectEx包装对 ReadProcessMemory 的调用(一个用于“取消保护”,允许读取,另一个用于“恢复”先前的保护)。
一些代码:
// 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 = ....
}
Run Code Online (Sandbox Code Playgroud)
[迭代过程]
您在这里有两个选择:
无论目标进程内存读取技术如何,您的算法都是这样的(我真的很难将您的文本翻译成实现......):
输入:进程的基址、偏移量
输出: 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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2513 次 |
| 最近记录: |