vendredi 23 octobre 2020

Powershell reflection run the function

I have this workable code

[Windows.Forms.Form].
Assembly.GetType(
    'System.Windows.Forms.UnsafeNativeMethods'
).GetMethod('GetAsyncKeyState').Invoke($null,
    (
        0x09 # Tab key code
    )
)

Now im trying to invoke EnumWindows

$EnumWindowsUtil = [Windows.Forms.Form].
Assembly.GetType(
    'System.Windows.Forms.SafeNativeMethods'
).GetMethod('EnumWindows')
    
    
# Create a list to act as a receptacle for all the window handles we're about to enumerate
$WindowHandles = [Collections.Generic.List[IntPtr]]::new()

# Define the callback function
$callback = {
    param([IntPtr]$handle, [IntPtr]$param) 

    # Copy the window handle to our list
    $WindowHandles.Add($handle)

    # Continue (return $false from the callback to abort the enumeration)
    return $true
}

if($EnumWindowsUtil.Invoke($null, ($callback, [IntPtr]::Zero))){
    # $WindowHandles will contain all the window handles now
}

The error is:

Object of type 'System.Management.Automation.ScriptBlock' cannot be converted to type 'System.Windows.Forms.SafeNativeMethods+EnumThreadWindowsCallback'

How to fix types conflict, and run this code?





Aucun commentaire:

Enregistrer un commentaire