title: Calling kernel32's ReadProcessMemory in Go
body: <p>I'm trying to manipulate processes on Windows using Go language,
  and I'm starting off by reading other process' memory by using <code>ReadProcessMemory</code>.</p>
  
  <p>However, for most of the addresses I get <code>Error: Only part of a ReadProcessMemory or WriteProcessMemory request was completed.</code> error. Maybe my list of arguments is wrong, but I can't find out why.</p>
  
  <p>Can anyone point out what I am doing wrong here?</p>
  
  <pre><code>package main
  
  import (
    "fmt"
  )
  
  import (
    windows "golang.org/x/sys/windows"
  )
  
  func main() {
    handle, _ := windows.OpenProcess(0x0010, false, 6100) // 0x0010 PROCESS_VM_READ, PID 6100
    procReadProcessMemory := windows.MustLoadDLL("kernel32.dll").MustFindProc("ReadProcessMemory")
  
    var data uint   = 0
    var length uint = 0
  
    for i := 0; i &lt; 0xffffffff; i += 2 {
      fmt.Printf("0x%x\n", i)
  
      // BOOL ReadProcessMemory(HANDLE hProcess, LPCVOID lpBaseAddress, LPVOID lpBuffer, DWORD nSize, LPDWORD lpNumberOfBytesRead)
      ret, _, e := procReadProcessMemory.Call(uintptr(handle), uintptr(i), uintptr(data), 2, uintptr(length)) // read 2 bytes
      if (ret == 0) {
          fmt.Println("  Error:", e)          
      } else {
          fmt.Println("  Length:", length)
          fmt.Println("  Data:", data)                        
      }
    }
  
    windows.CloseHandle(handle)
  }
  </code></pre>