DllImport 函数的别名

Alias for function from DllImport

[Win32]::ShowWindowAsync<<..>> 有效,但这无效

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

您传递给 Add-Type 的 C# 代码存在多个问题。更改 f_1 方法主体的定义,以便它正确调用 extern 方法(删除您传递的参数前面的类型声明)并确保您 return return从它到调用者的价值:

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