Created
August 16, 2020 19:10
-
-
Save moddingg33k/4a46c5ed4cb806977a6d4733cbec8c70 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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