Skip to content

Instantly share code, notes, and snippets.

@BobVul
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BobVul/890f38bc36deecb821c9 to your computer and use it in GitHub Desktop.
Save BobVul/890f38bc36deecb821c9 to your computer and use it in GitHub Desktop.
Powershell list untitled windows' process names (modified version of http://powershell-knowhow.de/powershell/?p=321)
$TypeDef = @"
using System;
using System.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Diagnostics;
namespace Api
{
public class WinStruct
{
public string WinTitle {get; set; }
public int WinHwnd { get; set; }
public string ProcName { get; set; }
public bool Visible { get; set; }
}
public class ApiDef
{
private delegate bool CallBackPtr(int hwnd, int lParam);
private static CallBackPtr callBackPtr = Callback;
private static List<WinStruct> _WinStructList = new List<WinStruct>();
[DllImport("User32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(CallBackPtr lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError=true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowVisible(IntPtr hWnd);
private static bool Callback(int hWnd, int lparam)
{
StringBuilder sb = new StringBuilder(256);
int res = GetWindowText((IntPtr)hWnd, sb, 256);
try {
uint pid;
GetWindowThreadProcessId((IntPtr)hWnd, out pid);
Process p = Process.GetProcessById((int)pid);
_WinStructList.Add(new WinStruct { WinHwnd = hWnd, WinTitle = sb.ToString(), ProcName = p.Modules[0].FileName, Visible = IsWindowVisible((IntPtr)hWnd) });
return true;
} catch {}
_WinStructList.Add(new WinStruct { WinHwnd = hWnd, WinTitle = sb.ToString() });
return true;
}
public static List<WinStruct> GetWindows()
{
_WinStructList.Clear();
EnumWindows(callBackPtr, IntPtr.Zero);
return _WinStructList;
}
}
}
"@
Add-Type -TypeDefinition $TypeDef -Language CSharpVersion3
[Api.Apidef]::GetWindows() | Where-Object { $_.WinTitle -eq "" -and $_.Visible } | Sort-Object -Property ProcName | Select-Object ProcName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment