title: C# reading another process memory
body: <p>I am having trouble reading a simple int from a game. I found a static address with Cheat Engine, if I manually insert it into the list, it works every time. But when I try to read it with C# it always reads 0 bytes. If anyone could help, that would be great!</p>
  
  <p>Thanks!</p>
  
  <pre><code> class Program
  {
      private static readonly int PROCESS_WM_READ = 0x0010;
  
      [DllImport("kernel32.dll")]
      public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
  
      [DllImport("kernel32.dll")]
      public static extern bool ReadProcessMemory(int hProcess, int lpBaseAddress, byte[] lpBuffer, int dwSize, ref int lpNumberOfBytesRead);
  
      const string procName = "hl";
      static Process process = null;
      static IntPtr processHandle;
  
      public static byte[] Read(int handle, int address, int size, ref int bytes)
      {
          byte[] buffer = new byte[size];
          ReadProcessMemory(handle, address, buffer, size, ref bytes);
          return buffer;
      }
  
      static void Main(string[] args)
      {
          int bytesRead = 0;
          byte[] value = new byte[20];
          int address = 0x019EF0D7;
  
          Console.WriteLine("Starting");
          process = Process.GetProcessesByName(procName)[0];
          processHandle = OpenProcess(PROCESS_WM_READ, false, process.Id);
  
          while (true)
          {
              //ReadProcessMemory((int)processHandle, jumpAddresses, jumpValues, jumpValues.Length, ref bytesRead);
              value = Read((int)processHandle, address, 20, ref bytesRead);
              Console.WriteLine(int.Parse(bytesRead.ToString()) + " " + bytesRead);
              Thread.Sleep(100);
          }
  
      }
  }
  </code></pre>