Skip to content

Instantly share code, notes, and snippets.

@mogaika
Created August 23, 2017 02:43
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 mogaika/4b48f65e5eb9abd9f420e2c9e5436180 to your computer and use it in GitHub Desktop.
Save mogaika/4b48f65e5eb9abd9f420e2c9e5436180 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
func getPidByName(processName string) uint32 {
hSnapshot, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0)
if err != nil {
panic(err)
}
defer windows.CloseHandle(hSnapshot)
var entry windows.ProcessEntry32
entry.Size = uint32(unsafe.Sizeof(entry))
if err := windows.Process32First(hSnapshot, &entry); err != nil {
panic(err)
}
for {
if syscall.UTF16ToString(entry.ExeFile[:]) == processName {
return entry.ProcessID
}
if err := windows.Process32Next(hSnapshot, &entry); err != nil {
break
}
}
return 0
}
func main() {
log.Println(getPidByName("explorer.exe"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment