locked
Get Active Window Info RRS feed

  • Question

  • Is there a way to get current active window information like handle, name..? thanks
    Saturday, November 11, 2017 2:52 PM

Answers

  • Here is an easy way to get the active windows process info;

    $code = @'
        [DllImport("user32.dll")]
         public static extern IntPtr GetForegroundWindow();
    '@
    Add-Type $code -Name Utils -Namespace Win32
    while(1){
        $hwnd = [Win32.Utils]::GetForegroundWindow()
        Get-Process | 
            Where-Object { $_.mainWindowHandle -eq $hwnd } | 
            Select-Object processName, MainWindowTItle, MainWindowHandle
        sleep -Milliseconds 200
    }

    This will change its output when you select different windows.  Make the widows small enough so you can watch the output as you select a new window.

    If you have dual monitors then put PS on the second monitor and select windows on the other.


    \_(ツ)_/

    • Marked as answer by Ian3 Sunday, November 12, 2017 12:57 AM
    Saturday, November 11, 2017 8:14 PM
  • Even WASP cannot get the active window.  The active window is relative to the current desktop.  You can get this via the WinAPI but not with Net or PowerShell.

    Here is the API version in PowerShell.

    $code = @'
        [DllImport("user32.dll")]
         public static extern IntPtr GetForegroundWindow();
    '@
    Add-Type $code -Name Utils -Namespace Win32
    $hwnd = [Win32.Utils]::GetForegroundWindow()
    


    \_(ツ)_/

    • Marked as answer by Ian3 Sunday, November 12, 2017 12:56 AM
    Saturday, November 11, 2017 5:28 PM

All replies

  • https://wasp.codeplex.com/

    Could be what you're looking for

    Saturday, November 11, 2017 3:47 PM
  • Even WASP cannot get the active window.  The active window is relative to the current desktop.  You can get this via the WinAPI but not with Net or PowerShell.

    Here is the API version in PowerShell.

    $code = @'
        [DllImport("user32.dll")]
         public static extern IntPtr GetForegroundWindow();
    '@
    Add-Type $code -Name Utils -Namespace Win32
    $hwnd = [Win32.Utils]::GetForegroundWindow()
    


    \_(ツ)_/

    • Marked as answer by Ian3 Sunday, November 12, 2017 12:56 AM
    Saturday, November 11, 2017 5:28 PM
  • AutoIt has a little tool that shows you the window name, controls, etc when you mouse over, very useful when writing AutoIt scripts, you can likely use here...

    Jack


    • Edited by JFetter Saturday, November 11, 2017 6:31 PM
    Saturday, November 11, 2017 6:30 PM
  • Here is the Window API call to get the information about the Window.  It can be P/Invoked in PowerSHell.

    GetWindowInfo(hWnd, PWINDOWINFO)


    \_(ツ)_/


    • Edited by jrv Saturday, November 11, 2017 7:59 PM
    Saturday, November 11, 2017 7:58 PM
  • Here is an easy way to get the active windows process info;

    $code = @'
        [DllImport("user32.dll")]
         public static extern IntPtr GetForegroundWindow();
    '@
    Add-Type $code -Name Utils -Namespace Win32
    while(1){
        $hwnd = [Win32.Utils]::GetForegroundWindow()
        Get-Process | 
            Where-Object { $_.mainWindowHandle -eq $hwnd } | 
            Select-Object processName, MainWindowTItle, MainWindowHandle
        sleep -Milliseconds 200
    }

    This will change its output when you select different windows.  Make the widows small enough so you can watch the output as you select a new window.

    If you have dual monitors then put PS on the second monitor and select windows on the other.


    \_(ツ)_/

    • Marked as answer by Ian3 Sunday, November 12, 2017 12:57 AM
    Saturday, November 11, 2017 8:14 PM