Skip to content

Instantly share code, notes, and snippets.

@obonyojimmy
Created January 1, 2017 04:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save obonyojimmy/f80a41b4adb18fe5389e98b64b27f21d to your computer and use it in GitHub Desktop.
Save obonyojimmy/f80a41b4adb18fe5389e98b64b27f21d to your computer and use it in GitHub Desktop.
Go lang check if active window the window yo are looking for
package main
import (
"fmt"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
var (
mod = windows.NewLazyDLL("user32.dll")
procGetClassNameW = mod.NewProc("GetClassNameW")
)
type (
HANDLE uintptr
HWND HANDLE
)
func GetClassName(hwnd HWND) (name string, err error) {
n := make([]uint16, 256)
p := &n[0]
r0, _, e1 := syscall.Syscall(procGetClassNameW.Addr(), 3, uintptr(hwnd), uintptr(unsafe.Pointer(p)), uintptr(len(n)))
if r0 == 0 {
if e1 != 0 {
err = error(e1)
} else {
err = syscall.EINVAL
}
return
}
name = syscall.UTF16ToString(n)
return
}
func getWindow(funcName string) uintptr {
proc := mod.NewProc(funcName)
hwnd, _, _ := proc.Call()
return hwnd
}
func chkinArray(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func main() {
classtolook := []string{
"CabinetWClass",
}
for {
if hwnd := getWindow("GetForegroundWindow") ; hwnd != 0 {
cn , _ := GetClassName(HWND(hwnd))
if chkinArray(cn ,classtolook) {
fmt.Println("class :", cn, "# hwnd:", hwnd)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment