Skip to content

Commit

Permalink
P/Invoke some Windows Window APIs
Browse files Browse the repository at this point in the history
IdleTime                  Equals                    GetTopWindow              GetWindowText             IsWindowVisible
LastInput                 FindWindow                GetWindow                 GetWindowTextLength       ReferenceEquals
LastInputTicks            GetForegroundWindow       GetWindowRect             GetWindowThreadProcessId  SetForegroundWindow
  • Loading branch information
guyrleech authored Jan 19, 2022
1 parent c65f7bb commit 8b729d0
Showing 1 changed file with 116 additions and 0 deletions.
116 changes: 116 additions & 0 deletions Window Helper Functions.ps1
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
'@

0 comments on commit 8b729d0

Please sign in to comment.