1

How can I find out what code is being called when you right click on a process in Windows Task Manager, and click on "Bring To Front" ?

This is related to https://stackoverflow.com/questions/45046765/bring-window-to-foreground-when-mainwindowhandle-is-0.

The SetForegroundWindow method only works if the MainWindowHandle is not 0. But the "Bring To Front" button in Windows Task Manager works even if the MainWindowHandle is 0.

1
  • Bring To Front is probably just getting the window handle another way. Every window has a handle, but not every process has a main window.
    – osvein
    Aug 1, 2017 at 0:29

1 Answer 1

0

Here's the answer: http://reinventingthewheel.azurewebsites.net/MainWindowHandleIsALie.aspx

I had to use EnumWindows to get the correct handle.

$TypeDef2 = @"

    using System;
    using System.Text;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;

    namespace Api
    {

    public class WinStruct
    {
       public string WinTitle {get; set; }
       public int MainWindowHandle { 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);

       private static bool Callback(int hWnd, int lparam)
       {
           StringBuilder sb = new StringBuilder(256);
           int res = GetWindowText((IntPtr)hWnd, sb, 256);
          _WinStructList.Add(new WinStruct { MainWindowHandle = hWnd, WinTitle = sb.ToString() });
           return true;
       }  

       public static List<WinStruct> GetWindows()
       {
          _WinStructList = new List<WinStruct>();
          EnumWindows(callBackPtr, IntPtr.Zero);
          return _WinStructList;
       }

    }
    }
    "@

    Add-Type -TypeDefinition $TypeDef2 -Language CSharpVersion3

    $excelInstance = [Api.Apidef]::GetWindows() | Where-Object { $_.WinTitle.ToUpper() -eq "Microsoft Excel - Compatibility Checker".ToUpper() }

$TypeDef1 = @"
      using System;
      using System.Runtime.InteropServices;
      public class Tricks {
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool SetForegroundWindow(IntPtr hWnd);
      }
    "@

    Add-Type -TypeDefinition $TypeDef1 -Language CSharpVersion3

    [void][Tricks]::SetForegroundWindow($excelInstance.MainWindowHandle)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.