Skip to content

Instantly share code, notes, and snippets.

@Lomanic
Forked from castaneai/open_process.go
Created November 12, 2017 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lomanic/bd13c0ec372a814ff40a148cbc6af81c to your computer and use it in GitHub Desktop.
Save Lomanic/bd13c0ec372a814ff40a148cbc6af81c to your computer and use it in GitHub Desktop.
WinAPI OpenProcess in Golang
package main
import (
"fmt"
"syscall"
"unsafe"
)
type Process uintptr
const PROCESS_ALL_ACCESS = 0x1F0FFF
func main() {
// open process
pid := 0
fmt.Print("Input PID: ")
fmt.Scanf("%d", &pid)
handle := OpenProcessHandle(pid)
fmt.Printf("handle: %d", handle)
}
func OpenProcessHandle(processId int) Process {
kernel32 := syscall.MustLoadDLL("kernel32.dll")
proc := kernel32.MustFindProc("OpenProcess")
handle, _, _ := proc.Call(ptr(PROCESS_ALL_ACCESS), ptr(true), ptr(processId))
return Process(handle)
}
func ptr(val interface{}) uintptr {
switch val.(type) {
case string:
return uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(val.(string))))
case int:
return uintptr(val.(int))
default:
return uintptr(0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment