package main
import (
"fmt"
"log"
"syscall"
"unsafe"
)
var (
user32 = syscall.MustLoadDLL("user32.dll")
procEnumWindows = user32.MustFindProc("EnumWindows")
procGetWindowTextW = user32.MustFindProc("GetWindowTextW")
)
func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
len = int32(r0)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func FindWindow(title string) (syscall.Handle, error) {
var hwnd syscall.Handle
cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr {
b := make([]uint16, 200)
_, err := GetWindowText(h, &b[0], int32(len(b)))
if err != nil {
// ignore the error
return 1 // continue enumeration
}
fmt.Printf("%s
", syscall.UTF16ToString(b))
if syscall.UTF16ToString(b) == title {
// note the window
hwnd = h
return 0 // stop enumeration
}
return 1 // continue enumeration
})
EnumWindows(cb, 0)
if hwnd == 0 {
return 0, fmt.Errorf("No window with title '%s' found", title)
}
return hwnd, nil
}
func main() {
//const title = "OpenTTD"
//const title = "FileZilla FTP Client"
const title = "OpenTTD"
h, err := FindWindow(title)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found '%s' window: handle=0x%x
", title, h)
}
Go/Golang Trying to get window information via syscall. (As in EnumWindows, etc.) - Stack Overflow - Google Chrome
任务管理器
OpenTTD 1.3.0
openttd.exe 属性
filezilla.exe 属性
OfficePowerManagerWindow
game_manager [Y:devgame_manager] - C:Gosrcosexecexec.go - GoLand
CandidateWindow Mode Indicator Mode Indicator 电池指示器 Network Flyout Mode Indicator Mode Indicator go_win_api [Y:devgo_win_api] - ...m.go - GoLand OpenTTD 1.3.0 Found 'OpenTTD 1.3.0' window: handle=0x610d6 CandidateWindow Mode Indicator Mode Indicator 电池指示器 Network Flyout Mode Indicator Mode Indicator go_win_api [Y:devgo_win_api] - ...m.go - GoLand OpenTTD 1.3.0 Get Retrun Value Before setForegroundWindow Invoked: %!s(uintptr=1)
package main
import (
"fmt"
"log"
"syscall"
"unsafe"
)
var (
user32 = syscall.MustLoadDLL("user32.dll")
procEnumWindows = user32.MustFindProc("EnumWindows")
procGetWindowTextW = user32.MustFindProc("GetWindowTextW")
user32_b, _ = syscall.LoadLibrary("user32.dll")
setForegroundWindow, _ = syscall.GetProcAddress(user32_b, "SetForegroundWindow")
)
func EnumWindows(enumFunc uintptr, lparam uintptr) (err error) {
r1, _, e1 := syscall.Syscall(procEnumWindows.Addr(), 2, uintptr(enumFunc), uintptr(lparam), 0)
if r1 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func GetWindowText(hwnd syscall.Handle, str *uint16, maxCount int32) (len int32, err error) {
r0, _, e1 := syscall.Syscall(procGetWindowTextW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(str)), uintptr(maxCount))
len = int32(r0)
if len == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
}
return
}
func FindWindow(title string) (syscall.Handle, error) {
var hwnd syscall.Handle
cb := syscall.NewCallback(func(h syscall.Handle, p uintptr) uintptr {
b := make([]uint16, 200)
_, err := GetWindowText(h, &b[0], int32(len(b)))
if err != nil {
// ignore the error
return 1 // continue enumeration
}
fmt.Printf("%s
", syscall.UTF16ToString(b))
if syscall.UTF16ToString(b) == title {
// note the window
hwnd = h
return 0 // stop enumeration
}
return 1 // continue enumeration
})
EnumWindows(cb, 0)
if hwnd == 0 {
return 0, fmt.Errorf("No window with title '%s' found", title)
}
return hwnd, nil
}
func IntPtr(n int) uintptr {
return uintptr(n)
}
func abort(funcname string, err syscall.Errno) {
panic(funcname + " failed: " + err.Error())
}
func main() {
//const title = "OpenTTD"
//const title = "FileZilla FTP Client"
const title = "OpenTTD 1.3.0"
h, err := FindWindow(title)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Found '%s' window: handle=0x%x
", title, h)
h, err = FindWindow(title)
if err != nil {
log.Fatal(err)
}
ret, _, callErr := syscall.Syscall(setForegroundWindow, 1, uintptr(h), IntPtr(0), IntPtr(0))
if callErr != 0 {
abort("Call setForegroundWindow", callErr)
}
fmt.Printf("Get Retrun Value Before setForegroundWindow Invoked: %s
", ret)
}