News, tips, advice, support for Windows, Office, PCs & more. Tech help. No bull. We're community supported by donations from our Plus Members, and proud of it
Home icon Home icon Home icon Email icon RSS icon
  • Help with convert to hWind

    Home Forums Developers, developers, developers DevOps Lounge Help with convert to hWind

    This topic contains 1 reply, has 2 voices, and was last updated by  Paul T 2 months ago.

    • Author
      Posts
    • #2019157 Reply

      anonymous

      Hello,

      I am new to PInvoke and I am trying to figure out why I can not convert HWind from the System.Object[]. So if you know what’s cause of the error, please let me know.

      Thank you so much in advance for all of the helps.

      Attached is a simple program that I found on Google, In this program , I passed in the $h as the window handle pointer to the un-managed code Tricks program

      Add-Type @”
      using System;
      using System.Runtime.InteropServices;
      public class Tricks {
      [DllImport(“user32.dll”)]
      [return: MarshalAs(UnmanagedType.Bool)]
      public static extern bool SetForegroundWindow(IntPtr hWnd);
      }
      “@
      sleep -sec 2
      $h = (Get-Process Chrome).MainWindowHandle
      [Tricks]::SetForegroundWindow($h)

       

      The system gave me an error stating that it did not recognize $h as the pointer:

      Cannot convert argument “hWnd”, with value: “System.Object[]”, for “SetForegroundWindow” to type “System.IntPtr”: “Cannot convert the “System.Object[]” value of type “System.Object[]” to
      type “System.IntPtr”.”

    • #2019727 Reply

      Paul T
      AskWoody MVP

      This seems to be the correct way to do it.
      $hwnd = @(Get-Process Notepad)[0].MainWindowHandle

      https://www.reddit.com/r/PowerShell/comments/6pozdj/bringing_a_window_to_the_front_when/

      cheers, Paul

    Please follow the -Lounge Rules- no personal attacks, no swearing, and politics/religion are relegated to the Rants forum.

    Reply To: Help with convert to hWind

    You can use BBCodes to format your content.
    Your account can't use Advanced BBCodes, they will be stripped before saving.