我正在尝试这个:
$signature = @'
[DllImport("shell32.dll")]
public static extern int ShellExecuteW(
int hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd
);
'@
$exec = Add-Type -memberDefinition $signature -name "win" -namespace Win32Functions -passThru
$exec::ShellExecuteW(0, "open", "notepad.exe", 0, 0, 1)
但是记事本无法启动。如何正确写下来?
最佳答案
立即修复是双重的:
[DllImport("shell32.dll")]
->[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
ShellExecuteW
(API函数的Unicode版本),所以您必须通过属性中的CharSet
字段声明该事实。[1] int hwnd
应该是IntPtr hwnd
。 $exec::ShellExecuteW(0, "open", "notepad.exe", 0, 0, 1)
->$exec::ShellExecute(0, "open", "notepad.exe", [NullString]::Value, [NullString]::Value, 1)
[NullString]::Value
将null
传递给string
类型的参数;默认情况下,PowerShell将字符串上下文中的$null
视为空字符串,而不是null
;但是,在这种特殊情况下,空字符串(以及$null
)也可以工作。 放在一起:
$exec = Add-Type -name "win" -namespace Win32Functions -passThru -memberDefinition @'
[DllImport("shell32.dll", CharSet=CharSet.Unicode)]
public static extern int ShellExecuteW(
IntPtr hwnd,
string lpOperation,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd
);
'@
$exec::ShellExecuteW(
0,
'open',
'notepad.exe',
[NullString]::Value,
[NullString]::Value,
1
)
退后一步:
Start-Process
cmdlet允许您执行相同操作,而无需按需编译的P / Invoke声明:# Use -WorkingDirectory, if needed.
Start-Process Notepad.Exe -Verb Open -WindowStyle Normal
[1]实际上,省略
W
后缀(ShellExecute
)而不指定CharSet
值也是可行的,尽管the docs表示随后会调用ANSI版本。但是,在实践中我看不到这一点:即使将带有ANSI范围之外的字符(例如'file€§ü.txt'
)的参数显式传递给函数的ANSI版本,它们似乎也可以正确传递给记事本。相反,即使明确地以Unicode版本为目标,将这样的字符串传递给控制台应用程序似乎也无法正确传递它们。
关于powershell - PowerShell和WinApi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61170518/