简体   繁体   English

WriteConsole 不适用于 PowerShell ISE?

[英]WriteConsole doesn't work with PowerShell ISE?

WriteConsole does not work with PowerShell ISE. WriteConsole不适用于 PowerShell ISE。

Neither WriteConsoleW or WriteConsoleA do. WriteConsoleWWriteConsoleA都不会。

See, for example, this program:例如,请参阅此程序:

#include <iostream>
#include <Windows.h>

void w() {
  DWORD written;
  BOOL const success = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"Printed\n", 8, &written, nullptr);
  std::wcout << (success ? L"Success" : L"Failure") << L". Wrote " << written << L" characters." << std::endl;
}

void a() {
  DWORD written;
  BOOL const success = WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), "Printed\n", 8, &written, nullptr);
  std::cout << (success ? "Success" : "Failure") << ". Wrote " << written << " characters." << std::endl;
}

int main() {
  w();
  a();
  return 0;
}

Ran from PowerShell (or Command Prompt, or Git Bash), it prints:从 PowerShell(或命令提示符,或 Git Bash)运行,它打印:

Printed
Success (wrote 8 characters)
Printed
Success (wrote 8 characters)

But from PowerShell ISE:但来自 PowerShell ISE:

Failure (wrote 0 characters)
Failure (wrote 0 characters)

The reason why is shown by this program:这个程序显示的原因:

#include <iostream>
#include <Windows.h>

int main() {
  DWORD const file_type = GetFileType(GetStdHandle(STD_OUTPUT_HANDLE));
  if (file_type == FILE_TYPE_CHAR) {
    std::cout << "char" << std::endl;
  } else if (file_type == FILE_TYPE_PIPE) {
    std::cout << "pipe" << std::endl;
  } else {
    std::cout << file_type << std::endl;
  }
  return 0;
}

When run from PowerShell (or Command Prompt, or Git Bash), it prints:从 PowerShell(或命令提示符,或 Git Bash)运行时,它会打印:

char

But from PowerShell ISE:但来自 PowerShell ISE:

pipe

WriteConsole cannot write through a pipe, and thus fails. WriteConsole无法通过 pipe 写入,因此失败。 The same thing happens when run from PowerShell / Command Prompt / Git Bash if the output is piped.从 PowerShell / 命令提示符 / Git Bash 运行时会发生同样的事情,如果 Z78E6221F6393D135CE8668 是管道的。

To provide background information to Bertie Wheen's helpful answer :Bertie Wheen 的有用答案提供背景信息:

  • Perhaps surprisingly, the Windows PowerShell ISE does not allocate a console by default .或许令人惊讶的是, Windows PowerShell ISE默认分配控制台 (The console-like UI that the ISE presents is not a true Windows console). (ISE 呈现的类似控制台的 UI 并不是真正的 Windows 控制台)。

  • A console is allocated on demand , the first time a console-subsystem program is run in a session (eg, cmd /c ver )控制台按需分配,第一次控制台子系统程序在 session 中运行(例如cmd /c ver

    • Even once that has happened, however, interactive console-subsystem programs are fundamentally unsupported (try choice /m "Prompt me" , for instance).然而,即使发生这种情况,交互式控制台子系统程序也基本上不受支持(例如,尝试choice /m "Prompt me" )。

Interactively, you can test if a console has been allocated or not with the following command: [Console]::WindowTop ;交互式地,您可以使用以下命令测试是否已分配控制台: [Console]::WindowTop ; if there's no console, you'll get a The handle is invalid error.如果没有控制台,你会得到一个The handle is invalid错误。


It follows from the above that your program cannot assume that a console is present when run in the ISE .从上面可以看出,您的程序不能假定在 ISE 中运行时存在控制台

One option is to simply not support running in the ISE , given that it is:一种选择是根本不支持在 ISE 中运行,因为它是:

As for a successor environment: The actively developed, cross-platform editor that offers the best PowerShell development experience is Visual Studio Code with its PowerShell extension .至于后续环境:提供最佳 PowerShell 开发体验的积极开发的跨平台编辑器是带有PowerShell 扩展Visual Studio Code


As for the potential reason for the poor console support in the ISE : zett42 notes:至于ISE 中控制台支持不佳的潜在原因zett42说明:

A possible reason why ISE developers choose not to allocate a console could stem from the historic difficulties of creating a custom, embedded console within an app's own window. ISE 开发人员选择不分配控制台的一个可能原因可能源于在应用程序自己的 window 中创建自定义嵌入式控制台的历史困难。 Developers had to resort to hackish, unsupported ways of doing that.开发人员不得不求助于骇人听闻的、不受支持的方式来做到这一点。 Only recently (2018) Windows got a dedicated pseudo-console (ConPTY) API .直到最近(2018 年) Windows 才获得了专用的伪控制台(ConPTY) API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM