Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove support for go1.3 #7

Merged
merged 3 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions trap_others.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !windows
// +build !windows

package mousetrap
Expand Down
88 changes: 16 additions & 72 deletions trap_windows.go
Original file line number Diff line number Diff line change
@@ -1,98 +1,42 @@
// +build windows
// +build !go1.4

package mousetrap

import (
"fmt"
"os"
"syscall"
"unsafe"
)

const (
// defined by the Win32 API
th32cs_snapprocess uintptr = 0x2
)

var (
kernel = syscall.MustLoadDLL("kernel32.dll")
CreateToolhelp32Snapshot = kernel.MustFindProc("CreateToolhelp32Snapshot")
Process32First = kernel.MustFindProc("Process32FirstW")
Process32Next = kernel.MustFindProc("Process32NextW")
)

// ProcessEntry32 structure defined by the Win32 API
type processEntry32 struct {
dwSize uint32
cntUsage uint32
th32ProcessID uint32
th32DefaultHeapID int
th32ModuleID uint32
cntThreads uint32
th32ParentProcessID uint32
pcPriClassBase int32
dwFlags uint32
szExeFile [syscall.MAX_PATH]uint16
}

func getProcessEntry(pid int) (pe *processEntry32, err error) {
snapshot, _, e1 := CreateToolhelp32Snapshot.Call(th32cs_snapprocess, uintptr(0))
if snapshot == uintptr(syscall.InvalidHandle) {
err = fmt.Errorf("CreateToolhelp32Snapshot: %v", e1)
return
func getProcessEntry(pid int) (*syscall.ProcessEntry32, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

FWIW, I'm considering a pull request in golang.org/x/sys to see if the maintainers there are open to exporting the equivalent function there. I'll let you know if they do (and open a PR here)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened golang/sys#143 (https://go-review.googlesource.com/c/sys/+/459039) to see if the maintainers are open to exporting it 👍

snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)
if err != nil {
return nil, err
}
defer syscall.CloseHandle(syscall.Handle(snapshot))

var processEntry processEntry32
processEntry.dwSize = uint32(unsafe.Sizeof(processEntry))
ok, _, e1 := Process32First.Call(snapshot, uintptr(unsafe.Pointer(&processEntry)))
if ok == 0 {
err = fmt.Errorf("Process32First: %v", e1)
return
defer syscall.CloseHandle(snapshot)
var procEntry syscall.ProcessEntry32
procEntry.Size = uint32(unsafe.Sizeof(procEntry))
if err = syscall.Process32First(snapshot, &procEntry); err != nil {
return nil, err
}

for {
if processEntry.th32ProcessID == uint32(pid) {
pe = &processEntry
return
if procEntry.ProcessID == uint32(pid) {
return &procEntry, nil
}

ok, _, e1 = Process32Next.Call(snapshot, uintptr(unsafe.Pointer(&processEntry)))
if ok == 0 {
err = fmt.Errorf("Process32Next: %v", e1)
return
err = syscall.Process32Next(snapshot, &procEntry)
if err != nil {
return nil, err
}
}
}

func getppid() (pid int, err error) {
pe, err := getProcessEntry(os.Getpid())
if err != nil {
return
}

pid = int(pe.th32ParentProcessID)
return
}

// StartedByExplorer returns true if the program was invoked by the user double-clicking
// on the executable from explorer.exe
//
// It is conservative and returns false if any of the internal calls fail.
// It does not guarantee that the program was run from a terminal. It only can tell you
// whether it was launched from explorer.exe
func StartedByExplorer() bool {
ppid, err := getppid()
pe, err := getProcessEntry(syscall.Getppid())
if err != nil {
return false
}

pe, err := getProcessEntry(ppid)
if err != nil {
return false
}

name := syscall.UTF16ToString(pe.szExeFile[:])
return name == "explorer.exe"
return "explorer.exe" == syscall.UTF16ToString(pe.ExeFile[:])
}
46 changes: 0 additions & 46 deletions trap_windows_1.4.go

This file was deleted.