diff --git a/Window Helper Functions.ps1 b/Window Helper Functions.ps1 new file mode 100644 index 0000000..4d3639d --- /dev/null +++ b/Window Helper Functions.ps1 @@ -0,0 +1,116 @@ +<# +.SYNOPSIS + Dot source this module and call the P/Invoke members as required + +.EXAMPLE + . 'Z:\Guys Scripts\Scripts\Window Helper Functions.ps1' + [PInvoke.Win32.UserInput]::IdleTime + +.NOTES + Modification# History: + + @guyrleech 2022-01-19 First standalone public release +#> + +## Borrowed from http://stackoverflow.com/a/15846912 and adapted +Add-Type @' +using System; +using System.Text; +using System.Diagnostics; +using System.Runtime.InteropServices; + +namespace PInvoke.Win32 +{ + public static class UserInput + { + [DllImport("user32.dll", SetLastError=true)] + public static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId); + + [DllImport("user32.dll")] + public static extern IntPtr GetTopWindow(IntPtr hWnd); + + [DllImport("user32.dll", SetLastError = true)] + public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd); + + public enum GetWindow_Cmd : uint { + GW_HWNDFIRST = 0, + GW_HWNDLAST = 1, + GW_HWNDNEXT = 2, + GW_HWNDPREV = 3, + GW_OWNER = 4, + GW_CHILD = 5, + GW_ENABLEDPOPUP = 6 + } + + [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] + public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); + + [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)] + public static extern int GetWindowTextLength(IntPtr hWnd); + + [DllImport("user32.dll")] + public static extern IntPtr FindWindow(IntPtr sClassName, String sAppName); + + [DllImport("user32.dll")] + public static extern bool IsWindowVisible(IntPtr hWnd); + + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); + + public struct RECT + { + public int Left; + public int Top; + public int Right; + public int Bottom; + } + [DllImport("user32.dll")] + public static extern IntPtr GetForegroundWindow(); + + [DllImport("user32.dll", SetLastError=true)] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool SetForegroundWindow(IntPtr hWnd); + + [DllImport("user32.dll")] + public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId); + + [DllImport("user32.dll", SetLastError=false)] + private static extern bool GetLastInputInfo(ref LASTINPUTINFO plii); + + [StructLayout(LayoutKind.Sequential)] + private struct LASTINPUTINFO + { + public uint cbSize; + public int dwTime; + } + public static DateTime LastInput + { + get + { + DateTime bootTime = DateTime.UtcNow.AddMilliseconds(-Environment.TickCount); + DateTime lastInput = bootTime.AddMilliseconds(LastInputTicks); + return lastInput; + } + } + public static TimeSpan IdleTime + { + get + { + return DateTime.UtcNow.Subtract(LastInput); + } + } + public static int LastInputTicks + { + get + { + LASTINPUTINFO lii = new LASTINPUTINFO(); + lii.cbSize = (uint)Marshal.SizeOf(typeof(LASTINPUTINFO)); + GetLastInputInfo(ref lii); + return lii.dwTime; + } + } + } +} +'@ +