Skip to content

Commit

Permalink
Merge pull request #7 from thaJeztah/remove_go1.4
Browse files Browse the repository at this point in the history
remove support for go1.3
  • Loading branch information
inconshreveable committed Nov 27, 2022
2 parents 3a66f25 + 15567a6 commit 4e8053e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 118 deletions.
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) {
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.

0 comments on commit 4e8053e

Please sign in to comment.