Skip to content

Instantly share code, notes, and snippets.

@moddingg33k
Created August 16, 2020 19:10
Show Gist options
  • Save moddingg33k/4a46c5ed4cb806977a6d4733cbec8c70 to your computer and use it in GitHub Desktop.
Save moddingg33k/4a46c5ed4cb806977a6d4733cbec8c70 to your computer and use it in GitHub Desktop.
Add-Type -TypeDefinition @'
using System;
using System.Runtime.InteropServices;
public static class Win32Api
{
[System.Runtime.InteropServices.DllImportAttribute( "User32.dll", EntryPoint = "GetWindowThreadProcessId" )]
public static extern int GetWindowThreadProcessId ( [System.Runtime.InteropServices.InAttribute()] System.IntPtr hWnd, out int lpdwProcessId );
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
}
'@
# HWND to PID
function Convert-HwndToPid ($HWND) {
$HWND | ForEach-Object {
$myPid = [IntPtr]::Zero;
$null = [Win32Api]::GetWindowThreadProcessId( $PSItem, [ref] $myPid );
Select-Object @{n = 'HWND'; e = {$PSItem}}, @{n = 'PID'; e = {$myPid}}, @{n = 'Name'; e = {(Get-Process -PID $myPid).Name}} -InputObject ''
}
}
# Get Microsoft Words HWND
function Get-MsWordHwnd {
$application = New-Object -ComObject "word.application"
$caption = [guid]::NewGuid()
$application.Caption = $caption
[Win32Api]::FindWindow( 'OpusApp', $caption )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment