Skip to content

Commit

Permalink
fix: detect process
Browse files Browse the repository at this point in the history
  • Loading branch information
blurfx committed Apr 19, 2024
1 parent 8efe02d commit 6607e58
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

const sleepTime = 100 * time.Millisecond
const executable = "kakaotalk.exe"

var mutex = &sync.Mutex{}
var handles = make([]windows.HWND, 0)
Expand All @@ -26,19 +27,17 @@ func uint8ToStr(arr []uint8) string {
}

func watch() {
const executeable = "kakaotalk.exe"
var (
pe32 winapi.ProcessEntry32
szExeFile string
)

snapshot := winapi.CreateToolhelp32Snapshot(winapi.Th32csSnapprocess, 0)
pe32.DwSize = uint32(unsafe.Sizeof(pe32))

lastFoundAt := time.Now().Unix() - 2
var snapshot windows.HWND
var enumWindow = syscall.NewCallback(func(handle windows.HWND, processId uintptr) uintptr {
winapi.GetWindowThreadProcessId(handle, &pe32.Th32ProcessID)

if processId == uintptr(pe32.Th32ProcessID) {
lastFoundAt = time.Now().Unix()
handles = append(handles, handle)
}
return 1
Expand All @@ -47,12 +46,16 @@ func watch() {
for {
mutex.Lock()
handles = handles[:0]
if lastFoundAt < time.Now().Unix()-1 {
snapshot = winapi.CreateToolhelp32Snapshot(winapi.Th32csSnapprocess, 0)
lastFoundAt = time.Now().Unix()
}

if winapi.Process32First(uintptr(snapshot), &pe32) {
for {
szExeFile = uint8ToStr(pe32.SzExeFile[:])

if strings.ToLower(szExeFile) == executeable {
if strings.ToLower(szExeFile) == executable {
winapi.EnumWindows(enumWindow, uintptr(pe32.Th32ProcessID))
break
}
Expand Down

0 comments on commit 6607e58

Please sign in to comment.