Created
December 14, 2014 16:30
-
-
Save castaneai/ed8cc2aaedf9d1eafd68 to your computer and use it in GitHub Desktop.
WinAPI OpenProcess in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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