打印输出到cmd.exe后如何显示cmd提示? [英] How to display cmd prompt after printing output to cmd.exe?
问题描述
我有一个 winmain
应用程序,该应用程序是从 cmd.exe
执行的,并输出输出。我使用 AttachConsole(ATTACH_PARENT_PROCESS)
访问cmd.exe。执行应用程序并将输出打印到 cmd.exe
后,命令行提示符不会显示,并且看起来应用程序仍在运行(已关闭)。在关闭我的应用程序之前,我使用 FreeConsole()
释放控制台。
I have an winmain
application that is executed from cmd.exe
and prints output to it. I atach to the cmd.exe using AttachConsole(ATTACH_PARENT_PROCESS)
. After application is executed and output is printed to cmd.exe
command line prompt is not displayed and it looks like application is stil running(while it is already closed). Before closing my application I release the console using FreeConsole()
.
#include <iostream>
#include <fstream>
#include <windows.h>
int wWinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nCmdShow
)
{
AttachConsole(ATTACH_PARENT_PROCESS);
std::wofstream console_out("CONOUT$");
std::wcout.rdbuf(console_out.rdbuf());
std::wcout << L"\nSome Output" << std::endl;
FreeConsole();
return 0;
}
当前结果:
Current result:
我的目标: < img src = https://i.stack.imgur.com/W2KPr.png alt =在此处输入图片描述>
应该如何在 myapp.exe
打印输出并关闭后,出现提示 C:New folder>
。
How should I make prompt C:New folder>
appear after myapp.exe
has printed its output and is closed.
推荐答案
如果尚未回答问题(经过这么长的时间),则需要模拟实际按下通过将相应的WM_KEYDOWN消息发送(或最好是发布)到控制台窗口来在控制台窗口中输入键,即在
<$ c $之后
In case the question has not been answered yet (after such a long time), it is required to simulate the actual pressing of the 'Enter' key in the console window by sending (or, preferably, posting) the corresponding WM_KEYDOWN message to the console window, i.e.
c> std :: wcout<< L \n某些输出<< std :: endl;
after
std::wcout << L"\nSome Output" << std::endl;
,然后调用
FreeConsole()
,
插入以下内容:
and before calling
FreeConsole()
,
insert the following:
HWND hWndCon_ = ::GetConsoleWindow();
if( hWndCon_ ) {
::PostMessage( hWndCon_, WM_KEYDOWN, VK_RETURN, 0 );
}
或简单地
:: PostMessage(:: GetConsoleWindow(),WM_KEYDOWN,VK_RETURN,0);
这篇关于打印输出到cmd.exe后如何显示cmd提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!