http urls monitor.

trap_windows_1.4.go 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // +build windows
  2. // +build go1.4
  3. package mousetrap
  4. import (
  5. "os"
  6. "syscall"
  7. "unsafe"
  8. )
  9. func getProcessEntry(pid int) (*syscall.ProcessEntry32, error) {
  10. snapshot, err := syscall.CreateToolhelp32Snapshot(syscall.TH32CS_SNAPPROCESS, 0)
  11. if err != nil {
  12. return nil, err
  13. }
  14. defer syscall.CloseHandle(snapshot)
  15. var procEntry syscall.ProcessEntry32
  16. procEntry.Size = uint32(unsafe.Sizeof(procEntry))
  17. if err = syscall.Process32First(snapshot, &procEntry); err != nil {
  18. return nil, err
  19. }
  20. for {
  21. if procEntry.ProcessID == uint32(pid) {
  22. return &procEntry, nil
  23. }
  24. err = syscall.Process32Next(snapshot, &procEntry)
  25. if err != nil {
  26. return nil, err
  27. }
  28. }
  29. }
  30. // StartedByExplorer returns true if the program was invoked by the user double-clicking
  31. // on the executable from explorer.exe
  32. //
  33. // It is conservative and returns false if any of the internal calls fail.
  34. // It does not guarantee that the program was run from a terminal. It only can tell you
  35. // whether it was launched from explorer.exe
  36. func StartedByExplorer() bool {
  37. pe, err := getProcessEntry(os.Getppid())
  38. if err != nil {
  39. return false
  40. }
  41. return "explorer.exe" == syscall.UTF16ToString(pe.ExeFile[:])
  42. }