• Help with convert to hWind

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

    Author
    Topic
    #2019157

    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”.”

    Viewing 0 reply threads
    Author
    Replies
    Viewing 0 reply threads
    Reply To: Reply #2019727 in Help with convert to hWind

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

    Your information:




    Cancel