diff --git a/v3/pkg/w32/user32.go b/v3/pkg/w32/user32.go index 029f38143..0afd7d37d 100644 --- a/v3/pkg/w32/user32.go +++ b/v3/pkg/w32/user32.go @@ -137,6 +137,7 @@ var ( procEnumDisplayDevices = moduser32.NewProc("EnumDisplayDevicesW") procEnumDisplaySettings = moduser32.NewProc("EnumDisplaySettingsW") procEnumDisplaySettingsEx = moduser32.NewProc("EnumDisplaySettingsExW") + procEnumWindows = moduser32.NewProc("EnumWindows") procChangeDisplaySettingsEx = moduser32.NewProc("ChangeDisplaySettingsExW") procSendInput = moduser32.NewProc("SendInput") procSetWindowsHookEx = moduser32.NewProc("SetWindowsHookExW") @@ -656,6 +657,14 @@ func ReleaseCapture() bool { return ret != 0 } +func EnumWindows(enumFunc uintptr, lparam uintptr) bool { + ret, _, _ := procEnumWindows.Call( + enumFunc, + lparam) + + return ret != 0 +} + func GetWindowThreadProcessId(hwnd HWND) (HANDLE, int) { var processId int ret, _, _ := procGetWindowThreadProcessId.Call( diff --git a/v3/plugins/single_instance/plugin_windows.go b/v3/plugins/single_instance/plugin_windows.go new file mode 100644 index 000000000..398af721a --- /dev/null +++ b/v3/plugins/single_instance/plugin_windows.go @@ -0,0 +1,35 @@ +//go:build windows + +package single_instance + +import ( + "github.com/wailsapp/wails/v3/pkg/w32" + "syscall" +) + +type enumWindowsCallback func(hwnd syscall.Handle, lParam uintptr) uintptr + +func enumWindowsProc(hwnd syscall.Handle, lParam uintptr) uintptr { + _, processID := w32.GetWindowThreadProcessId(uintptr(hwnd)) + targetProcessID := uint32(lParam) + if uint32(processID) == targetProcessID { + // Bring the window forward + w32.SetForegroundWindow(w32.HWND(hwnd)) + println("Found window: ", hwnd) + } + + // Continue enumeration + return 1 +} + +func (p *Plugin) activeInstance(pid int) error { + + // Get the window associated with the process ID. + targetProcessID := uint32(pid) // Replace with the desired process ID + + w32.EnumWindows( + syscall.NewCallback(enumWindowsCallback(enumWindowsProc)), + uintptr(targetProcessID), + ) + return nil +}