Make this Readprocessmemory code a lot more efficient
<p><strong>Introduction</strong></p>

<p>Currently I am using someone's class to read bytes in memory (via pointers). The code I have so far works perfectly, I don't really want to change the way the class is setup if possible (because it works), but hopefully some minor tweaks can be made to the class and my code to make it highly efficient.</p>

<p><strong>What I achieve at the moment:</strong></p>

<ul>
<li><p>Start off with a pointer address in memory, reads a byte at that address, adds the data to an array, adds 1 to the original address so we can now read the next address (eg original address is 24004, once the byte is stored, increment 1 and next address to read becomes 24005).</p></li>
<li><p>Reads the byte at the next address (24005), adds this to the same array, adds 1 to that address (next to read becomes 24006).</p></li>
<li><p>And so forth for a fixed number of iterations (approximately 10,000).  </p></li>
</ul>

<p><strong>The problem:</strong> </p>

<p>Making 10,000 calls to readprocessmemory one after the other causes a system delay of 20 seconds whilst it goes about its business.</p>

<p><strong>What I hope can be achieved:</strong></p>

<p>Perform Readprocessmemory once only, specifying 10,000 bytes of data to be read (instead of just a byte at a time for 10,000 iterations), save this to the array in the same format as I had before with individual bytes (I am aware therefore that instead of array {0} (1) (2).. etc.. I will now just have array {0}, so I would imagine I would need an efficient means of splitting this very large number out into 10,000 numbers (in another array)) The data stored at each address are integers. So for 5 addresses: array {12345} becomes {1}{2}{3}{4}{5}. Where 1 2 3 4 or 5 could just as well be 1 200 40 43 or 20, for example.</p>

<p>So ideally, if I could wave my newbie wand, it would look something like this (class below as well as what I have so far):</p>

<p><em><strong>Iteration code:</em></strong></p>

<pre><code> private void label1_Click(object sender, EventArgs e)
    {
        int[] valuesSeperated[200];
        List&lt;byte&gt; PreArray = new List&lt;byte&gt;();
        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 =&gt; (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.
    }
</code></pre>

<p><em><strong>Class:</em></strong></p>

<pre><code>[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;
}
</code></pre>
