Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Alias for function from DllImport

[Win32]::ShowWindowAsync<<..>> works, but this doesn’t

Add-Type -TypeDefinition @'
using System;       // IntPtr
using System.Runtime.InteropServices;       // DllImport

public class Win32 {
    [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr a, int b);
    
    public static bool f_1(IntPtr a, int b) {
        return (ShowWindowAsync(IntPtr a, int b));
    }
}
'@

write-host ([Win32]::f_1((Get-Process -Id $pid).MainWindowHandle, 2))

read-host 'end'

12345678901234567890123456789012345678901234567890123456789012345678901234567890

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are multiple problems with the C# code you pass off to Add-Type. Change the definition of the f_1 method body so that it invokes the extern method correctly (remove the type declarations in front of the arguments you’re passing) and make sure you return the return value from it to the caller:

public static bool f_1(IntPtr a, int b) {
    return ShowWindowAsync(a, b);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading