From e98cdbb8b673a6f3307c903c14cd8b8e72badc60 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Fri, 26 Feb 2021 20:42:00 -0800 Subject: [PATCH] util/winutil: add little Windows utility package Code from Alex Brainman, split out of another change. I changed it to a comma-ok return and tweaked the docs a bit. --- util/winutil/winutil.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 util/winutil/winutil.go diff --git a/util/winutil/winutil.go b/util/winutil/winutil.go new file mode 100644 index 000000000..cf6abb450 --- /dev/null +++ b/util/winutil/winutil.go @@ -0,0 +1,21 @@ +// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// +build windows + +// Package winuntil contains misc Windows/win32 helper functions. +package winutil + +// GetDesktopPID searches the PID of the process that's running the +// currently active desktop and whether it was found. +// Usually the PID will be for explorer.exe. +func GetDesktopPID() (pid uint32, ok bool) { + hwnd := windows.GetShellWindow() + if hwnd == 0 { + return 0, false + } + var pid uint32 + windows.GetWindowThreadProcessId(hwnd, &pid) + return pid, pid != 0 +}