Path: blob/main/vendor/github.com/inconshreveable/mousetrap/trap_windows.go
2875 views
package mousetrap12import (3"syscall"4"unsafe"5)67func getProcessEntry(pid int) (*syscall.ProcessEntry32, error) {8snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)9if err != nil {10return nil, err11}12defer syscall.CloseHandle(snapshot)13var procEntry syscall.ProcessEntry3214procEntry.Size = uint32(unsafe.Sizeof(procEntry))15if err = syscall.Process32First(snapshot, &procEntry); err != nil {16return nil, err17}18for {19if procEntry.ProcessID == uint32(pid) {20return &procEntry, nil21}22err = syscall.Process32Next(snapshot, &procEntry)23if err != nil {24return nil, err25}26}27}2829// StartedByExplorer returns true if the program was invoked by the user double-clicking30// on the executable from explorer.exe31//32// It is conservative and returns false if any of the internal calls fail.33// It does not guarantee that the program was run from a terminal. It only can tell you34// whether it was launched from explorer.exe35func StartedByExplorer() bool {36pe, err := getProcessEntry(syscall.Getppid())37if err != nil {38return false39}40return "explorer.exe" == syscall.UTF16ToString(pe.ExeFile[:])41}424344