How to find a desktop window (by window name) in Windows 8.1 Update 2 OS, using the Win32 API FindWindow() in PowerShell environment?

后端 未结 4 994
鱼传尺愫
鱼传尺愫 2021-01-19 05:53

I don\'t remember having any problem finding a window in older Windows OS\'s, but, I\'m not succeeding in Windows 8.1 Update 2 OS, using PowerShell v4.0.

4条回答
  •  无人共我
    2021-01-19 06:37

    First : not an answer but to help other people working on it, if you use the good class, for example here I code CalcFrame wich is the real class of the main window of calc.exe it works.

    $fw::FindWindow("CalcFrame", $wname) # returns the right value for me if calc.exe is started.
    

    Second : The following works for me ; accordind to Microsoft documentation the first parameter should be null, but accordin to PInvoke site you must pass IntPtr.Zero as the first parameter.

    $sig = @"
      [DllImport("user32.dll", CharSet = CharSet.Unicode)]
      public static extern IntPtr FindWindow(IntPtr sClassName, String sAppName);
    
      [DllImport("kernel32.dll")]
      public static extern uint GetLastError();
    "@
    
    $fw = Add-Type -Namespace Win32 -Name Funcs -MemberDefinition $sig -PassThru
    $wname='Calculatrice' # any existing window name
    
    $fw::FindWindow([IntPtr]::Zero, $wname ) # returns the Window Handle
    $a = $fw::GetLastError()
    $a
    

提交回复
热议问题