Created
January 1, 2017 04:05
-
-
Save obonyojimmy/f80a41b4adb18fe5389e98b64b27f21d to your computer and use it in GitHub Desktop.
Go lang check if active window the window yo are looking for
This file contains hidden or 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
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