Skip to content

Instantly share code, notes, and snippets.

@ambroff
Last active August 29, 2015 14:19
Show Gist options
  • Save ambroff/365199714d5b4026288f to your computer and use it in GitHub Desktop.
Save ambroff/365199714d5b4026288f to your computer and use it in GitHub Desktop.
func getImageNameFromHandle(processHandle syscall.Handle) (string, error) {
h, err := syscall.LoadLibrary("Psapi.dll")
if err != nil {
return "", err
}
defer syscall.FreeLibrary(h)
addr, err := syscall.GetProcAddress(h, "GetModuleFileNameExW")
if err != nil {
return "", err
}
buf := make([]uint16, 300)
var syscallErr error
r1, _, e1 := syscall.Syscall(addr, 3, uintptr(0), uintptr(unsafe.Pointer(&buf[0])), uintptr(len(buf)))
n := uint32(r1)
if n == 0 {
if e1 != 0 {
syscallErr = error(e1)
} else {
syscallErr = syscall.EINVAL
}
}
if syscallErr != nil {
return "", syscallErr
}
return string(utf16.Decode(buf[0:n])), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment