在 PowerShell 中通过 .NET 程序集调用 DLL 时跳过可选参数

问题描述 投票:0回答:1

我正在尝试从 PowerShell 调用

CascadeWindows
DLL。文档(https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-cascadewindows)指出有可选参数(一个 HWND/IntPtr,一个 HWND/ IntPtrs 和一个 RECT),它可以是 NULL。我如何让 PowerShell 将这些参数作为 NULL 来调用它?

$cw = Add-Type -MemberDefinition '[DllImport("user32.dll")] public static extern ushort CascadeWindows(IntPtr hwndParent, uint wHow, IntPtr lpRect, uint cKids, IntPtr [] lpKids);' -Name 'Cascad' -Namespace 'Win32' -PassThru
# Fails with error, 'Cannot find an overload for "CascadeWindows" and the argument count: "2".'
$cw::CascadeWindows(0,0) 
# Returns 0, i.e. fails. Error code is 203, 'The system could not find the environment option that was entered.'
$cw::CascadeWindows(0,0,0,0,0) 
$gle = Add-Type -MemberDefinition '[DllImport("kernel32.dll")] public static extern uint GetLastError();' -Name 'gle' -Namespace 'Win32' -PassThru
$gle::GetLastError()
powershell winapi dll
1个回答
0
投票

您可以使用

IntPtr.Zero
作为可选的
IntPtr
P/Invoke 参数:

$cw::CascadeWindows(
    [IntPtr]::Zero,
    [uint32] 0,
    [IntPtr]::Zero,
    [uint32] 0,
    [IntPtr[]] [IntPtr]::Zero)
© www.soinside.com 2019 - 2024. All rights reserved.