|
| 1 | +// +build windows |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "fmt" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + "syscall" |
| 10 | + "unsafe" |
| 11 | + |
| 12 | + "golang.org/x/sys/windows" |
| 13 | +) |
| 14 | + |
| 15 | +// WindowsProcess is an implementation of Process for Windows. |
| 16 | +type WindowsProcess struct { |
| 17 | + pid int |
| 18 | + ppid int |
| 19 | + exe string |
| 20 | +} |
| 21 | + |
| 22 | +// getImagePid returns the |
| 23 | +func getImagePid(imageName string) ([]int, error) { |
| 24 | + processes, err := processes() |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + var pids []int |
| 29 | + for i := 0; i < len(processes); i++ { |
| 30 | + if strings.ToLower(processes[i].exe) == imageName { |
| 31 | + pids = append(pids, processes[i].pid) |
| 32 | + } |
| 33 | + } |
| 34 | + return pids, nil |
| 35 | +} |
| 36 | + |
| 37 | +// getWindowTitle returns the title of a window linked to a process name |
| 38 | +func getWindowTitle(imageName string, windowTitleRegex string) (string, error) { |
| 39 | + processPid, err := getImagePid(imageName) |
| 40 | + if err != nil { |
| 41 | + return "", nil |
| 42 | + } |
| 43 | + // returns the first window of the first pid |
| 44 | + _, windowTitle, err := GetWindowTitle(processPid[0], windowTitleRegex) |
| 45 | + if err != nil { |
| 46 | + return "", nil |
| 47 | + } |
| 48 | + return windowTitle, nil |
| 49 | +} |
| 50 | + |
| 51 | +func newWindowsProcess(e *windows.ProcessEntry32) *WindowsProcess { |
| 52 | + // Find when the string ends for decoding |
| 53 | + end := 0 |
| 54 | + for { |
| 55 | + if e.ExeFile[end] == 0 { |
| 56 | + break |
| 57 | + } |
| 58 | + end++ |
| 59 | + } |
| 60 | + |
| 61 | + return &WindowsProcess{ |
| 62 | + pid: int(e.ProcessID), |
| 63 | + ppid: int(e.ParentProcessID), |
| 64 | + exe: syscall.UTF16ToString(e.ExeFile[:end]), |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Processes returns a snapshot of all the processes |
| 69 | +// Taken and adapted from https://github.com/mitchellh/go-ps |
| 70 | +func processes() ([]WindowsProcess, error) { |
| 71 | + // get process table snapshot |
| 72 | + handle, err := windows.CreateToolhelp32Snapshot(windows.TH32CS_SNAPPROCESS, 0) |
| 73 | + if err != nil { |
| 74 | + return nil, syscall.GetLastError() |
| 75 | + } |
| 76 | + defer windows.CloseHandle(handle) |
| 77 | + |
| 78 | + // get process infor by looping through the snapshot |
| 79 | + var entry windows.ProcessEntry32 |
| 80 | + entry.Size = uint32(unsafe.Sizeof(entry)) |
| 81 | + err = windows.Process32First(handle, &entry) |
| 82 | + if err != nil { |
| 83 | + return nil, fmt.Errorf("error retrieving process info") |
| 84 | + } |
| 85 | + |
| 86 | + results := make([]WindowsProcess, 0, 50) |
| 87 | + for { |
| 88 | + results = append(results, *newWindowsProcess(&entry)) |
| 89 | + err := windows.Process32Next(handle, &entry) |
| 90 | + if err != nil { |
| 91 | + if err == syscall.ERROR_NO_MORE_FILES { |
| 92 | + break |
| 93 | + } |
| 94 | + return nil, fmt.Errorf("Fail to syscall Process32Next: %v", err) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return results, nil |
| 99 | +} |
| 100 | + |
| 101 | +// win32 specific code |
| 102 | + |
| 103 | +// win32 dll load and function definitions |
| 104 | +var ( |
| 105 | + user32 = syscall.NewLazyDLL("user32.dll") |
| 106 | + procEnumWindows = user32.NewProc("EnumWindows") |
| 107 | + procGetWindowTextW = user32.NewProc("GetWindowTextW") |
| 108 | + procGetWindowThreadProcessID = user32.NewProc("GetWindowThreadProcessId") |
| 109 | +) |
| 110 | + |
| 111 | +// EnumWindows call EnumWindows from user32 and returns all active windows |
| 112 | +func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) { |
| 113 | + r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0) |
| 114 | + if r1 == 0 { |
| 115 | + if e1 != 0 { |
| 116 | + err = error(e1) |
| 117 | + } else { |
| 118 | + err = syscall.EINVAL |
| 119 | + } |
| 120 | + } |
| 121 | + return |
| 122 | +} |
| 123 | + |
| 124 | +// GetWindowText returns the title and text of a window from a window handle |
| 125 | +func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) { |
| 126 | + r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount)) |
| 127 | + len = int32(r0) |
| 128 | + if len == 0 { |
| 129 | + if e1 != 0 { |
| 130 | + err = error(e1) |
| 131 | + } else { |
| 132 | + err = syscall.EINVAL |
| 133 | + } |
| 134 | + } |
| 135 | + return |
| 136 | +} |
| 137 | + |
| 138 | +// GetWindowTitle searchs for a window attached to the pid |
| 139 | +func GetWindowTitle(pid int, windowTitleRegex string) (syscall.Handle, string, error) { |
| 140 | + var hwnd syscall.Handle |
| 141 | + var title string |
| 142 | + compiledRegex, err := regexp.Compile(windowTitleRegex) |
| 143 | + if err != nil { |
| 144 | + return 0, "", fmt.Errorf("Error while compiling the regex '%s'", windowTitleRegex) |
| 145 | + } |
| 146 | + // callback fro EnumWindows |
| 147 | + cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr { |
| 148 | + var prcsID int = 0 |
| 149 | + // get pid |
| 150 | + _, _, _ = procGetWindowThreadProcessID.Call(uintptr(h), uintptr(unsafe.Pointer(&prcsID))) |
| 151 | + // check if pid matches spotify pid |
| 152 | + if prcsID == pid { |
| 153 | + b := make([]uint16, 200) |
| 154 | + _, err := GetWindowText(h, &b[0], int32(len(b))) |
| 155 | + if err != nil { |
| 156 | + // ignore the error |
| 157 | + return 1 // continue enumeration |
| 158 | + } |
| 159 | + title = syscall.UTF16ToString(b) |
| 160 | + if compiledRegex.MatchString(title) { |
| 161 | + hwnd = h |
| 162 | + return 0 |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return 1 // continue enumeration |
| 167 | + }) |
| 168 | + // Enumerates all top-level windows on the screen |
| 169 | + EnumWindows(cb, 0) |
| 170 | + if hwnd == 0 { |
| 171 | + return 0, "", fmt.Errorf("No window with title '%b' found", pid) |
| 172 | + } |
| 173 | + return hwnd, title, nil |
| 174 | +} |
0 commit comments