-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from thaJeztah/remove_go1.4
remove support for go1.3
- Loading branch information
Showing
3 changed files
with
17 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package mousetrap | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[:]) | ||
} |
This file was deleted.
Oops, something went wrong.