[英]How to read UTF-16 encoded StdIn in PowerShell
I am trying to pass (a large number of [1]) strings from a native Windows host application (C++/WinApi) to a PowerShell script, which the host application launches using CreateProcess
.我正在尝试将(大量 [1])字符串从本机 Windows 主机应用程序(C++/WinApi)传递到 PowerShell 脚本,主机应用程序使用
CreateProcess
启动该脚本。
I use an anonymous pipe in STARTUPINFO::hStdInput
as the IPC mechanism.我在
STARTUPINFO::hStdInput
使用匿名管道作为 IPC 机制。 The data being written to the pipe consists of lines of UTF-16LE strings [2].写入管道的数据由多行 UTF-16LE 字符串组成 [2]。 What is printed by a naive PowerShell script
一个简单的 PowerShell 脚本打印了什么
foreach ($line in $input) {
write-host $line
}
however, looks like the data from StdIn is being interpreted in an ANSI code page (each UTF 16 code unit from the input shows up as a pair of letters in the output).然而,来自 StdIn 的数据看起来像是在 ANSI 代码页中被解释(输入中的每个 UTF 16 代码单元在输出中显示为一对字母)。
How can I make PowerShell to recognize the data from StdIn as UTF-16?如何让 PowerShell 将 StdIn 中的数据识别为 UTF-16?
I have already tried to我已经尝试过
$InputEncoding
, $OutputEncoding
and .Net's [Console]::InputEncoding
$InputEncoding
、 $OutputEncoding
和 .Net 的[Console]::InputEncoding
to no avail.无济于事。 Yes, I could write a large text file first and then read it in PowerShell but I would rather not do this.
是的,我可以先编写一个大文本文件,然后在 PowerShell 中读取它,但我宁愿不这样做。
[1] This is why I would like to use a pipe and leverage the stream processing capabilities of PowerShell. [1] 这就是为什么我想使用管道并利用 PowerShell 的流处理功能。
[2] Translating the data to a non-Unicode code page is not an option. [2] 无法将数据转换为非 Unicode 代码页。
Just to finally clean up this old question: Setting up the .Net console input encoding (that is what Powershell builds upon) correctly is a pretty nontrivial issue.只是为了最终解决这个老问题:正确设置 .Net 控制台输入编码(这是 Powershell 构建的基础)是一个非常重要的问题。 I finally worked around the problem, because I didn't want to burden the Powershell-script developers with the input encoding setup.
我终于解决了这个问题,因为我不想给 Powershell 脚本开发人员增加输入编码设置的负担。 So I ended up
所以我结束了
"#< CLIXML\\r\\n"
marker to declare the format to Powershell"#< CLIXML\\r\\n"
标记为流添加前缀以将格式声明为 PowershellThe final point turned out to be necessary, because the handling of clixml comes only after the text goes through the fragile console input decoding process.最后一点被证明是必要的,因为 clixml 的处理只有在文本通过脆弱的控制台输入解码过程之后才会出现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.