我的C++应用程序需要打开和关闭终端窗口以及进行复杂的终端操作。它在新的带有选项卡系统的Windows终端中无法正常工作。我不想强制用户更改默认终端来运行我的应用程序。我已经编写了代码来检查我的应用程序是否在新终端中,如果是,则希望在旧终端中启动一个新副本,然后退出当前终端。不幸的是,cmd.exe仍然会启动新终端。
如何使用旧的Windows终端启动应用程序?
void StartInOldTerminal() {
// Get path of current executable
wchar_t exePath[MAX_PATH];
if (GetModuleFileName(NULL, exePath, MAX_PATH) == 0) {
// Failed to get executable path, handle error
std::cerr << "Failed to get executable path\n";
exit(1);
}
// Check if running in the new Windows Terminal
DWORD consoleMode;
if (GetConsoleMode(GetStdHandle(STD_OUTPUT_HANDLE), &consoleMode) == 0) {
// Failed to get console mode, handle error
std::cerr << "Failed to get console mode\n";
exit(1);
}
if ((consoleMode & ENABLE_VIRTUAL_TERMINAL_PROCESSING) != 0) {
std::wstring cmdWStr = L"cmd.exe /k \"" + std::wstring(exePath) + L"\""; //Still starts in new terminal
std::string cmdStr = Text::wstring_to_utf8(cmdWStr);
system(cmdStr.c_str());
exit(0);
}
}