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

    Viewing 1 reply thread
    • Author
      • #2019157 Reply


        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 {
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool SetForegroundWindow(IntPtr hWnd);
        sleep -sec 2
        $h = (Get-Process Chrome).MainWindowHandle


        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

        cheers, Paul

    Viewing 1 reply thread

    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.