Win32应用程序使用printf将输出写入控制台
Win32 application write output to console using printf
我使用Win32应用程序开发了一个EXE。当我运行时(双击)应出现EXE GUI,当我从命令提示符中调用EXE时,应出现在命令控制台中。
我的问题是如何使用printf
将输出重定向到命令窗口?我可以使用AllocConsole()
在命令窗口中打印,但是创建了新的命令窗口,并将输出重定向到新窗口。我想在使用Win32应用程序调用EXE的同一命令窗口中打印输出。任何帮助。
构建Wilx所说的话(对不起,我没有足够的声誉来对他的答案发表评论),您可以使用arthconsole(...);因此,仅在已经有一个可用的情况下,请连接到控制台,您可以使用以下内容:
bool bAttachToConsole()
{
if (!AttachConsole(ATTACH_PARENT_PROCESS))
{
if (GetLastError() != ERROR_ACCESS_DENIED) //already has a console
{
if (!AttachConsole(GetCurrentProcessId()))
{
DWORD dwLastError = GetLastError();
if (dwLastError != ERROR_ACCESS_DENIED) //already has a console
{
return false;
}
}
}
}
return true;
}
然后在您的Winmain中可以做到这一点:
if (bAttachToConsole())
{
//do your io with STDIN/STDOUT
// ....
}
else
{
//Create your window and do IO via your window
// ....
}
此外这几乎可以做到您想要的:
// Win32Project1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <stdio.h> // printf, _dup2
#include <io.h> // _open_osfhandle
void SetupConsole()
{
AttachConsole(ATTACH_PARENT_PROCESS);
HANDLE hConIn = GetStdHandle(STD_INPUT_HANDLE);
int fd0 = _open_osfhandle((intptr_t)hConIn, 0);
_dup2(fd0, 0);
HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
int fd1 = _open_osfhandle((intptr_t)hConOut, 0);
_dup2(fd1, 1);
HANDLE hConErr = GetStdHandle(STD_ERROR_HANDLE);
int fd2 = _open_osfhandle((intptr_t)hConErr, 0);
_dup2(fd2, 2);
}
WNDPROC g_pOldProc;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
if (uMsg == WM_CLOSE)
{
PostQuitMessage(0);
return 0;
}
return CallWindowProc(g_pOldProc, hwnd, uMsg, wParam, lParam);
}
void GUI(HINSTANCE hInstance)
{
HWND hWnd = CreateWindow(
_T("EDIT"),
_T("GUI"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
100, 100, 200,200,
NULL,
NULL,
hInstance,
NULL
);
g_pOldProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)&WindowProc);
SetWindowText(hWnd, _T("Hello world."));
MSG m;
while (GetMessage(&m, NULL, 0, 0))
{
DispatchMessage(&m);
}
DestroyWindow(hWnd);
}
void Console()
{
SetupConsole();
printf("Hello world.");
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (!hConOut)
GUI(hInstance);
else
Console();
return 0;
}
我在这里检查了所有提交的解决方案,它们对我不起作用。您始终可以创建(或更改为)控制台子系统。但是,如果您想从WIN32应用中启用写入控制台,而不是分类为控制台应用程序,则此代码将起作用。调用此功能后,您可以使用printf(或wprintf)。
我的代码使用slocconsole(),然后使用artachconsole(),pssing to to to当前过程ID(通过调用getProcessID获得)。
void enableConsole()
{
AllocConsole();
AttachConsole(GetCurrentProcessId());
HWND Handle = GetConsoleWindow();
freopen("CON", "w", stdout);
}
尝试使用AttachConsole(ATTACH_PARENT_PROCESS)
(或使用PID)连接到现有控制台。
当我已经发布了答案时,TBH,我不确定我是否会完全理解您的问题。我有一些评论的代码(上方AllocConsole()
:
我们在这里忽略返回值。如果我们已经有一个控制台,它将失败。
您确定您不能像我一样无条件地使用AllocConsole()
吗?
尝试以下:
// Win32Project1.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include <stdio.h> // printf
#include <io.h> // _open_osfhandle, _dup2
void SetupConsole()
{
BOOL bCreated = AllocConsole();
if (!bCreated)
return; // We already have a console.
HANDLE hConOut = GetStdHandle(STD_OUTPUT_HANDLE);
int fd = _open_osfhandle((intptr_t)hConOut, 0);
_dup2(fd, 1);
}
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
SetupConsole();
printf("Hello world!");
Sleep(10000);
return 0;
}
- C++:将控制台输出存储在宏中更好吗
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 如何修复此iFile以将.txt输出到控制台
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 控制台输出在 Qt5 中未正确显示
- 有人可以向我解释为什么控制台输出 0 吗?
- 如何从 Win32 C++ 应用程序输出到父控制台窗口?
- 为什么即使使用 for 循环遍历我的向量,它也没有输出到控制台?(C++)
- 使用 std::string_view 的子字符串控制台输出
- 从文件读取并通过矢量输出到控制台
- 在这种情况下,如何防止C++输出/控制台窗体关闭
- 如何在C++中居中输出控制台窗口
- boost::log 输出到 Visual Studio 输出控制台 - 为格式添加额外的 LF/CR
- C++:我如何保持我的程序(输出控制台)的活力
- 如何在Visual Studio中设置输出控制台宽度
- 如何在Qt GUI应用程序中嵌入输出控制台窗口
- 在Visual Studio中使用GLUT库,没有错误,但输出控制台不起作用
- 只在调试模式下输出控制台
- 简单的输入/输出控制台应用程序,不按给定的顺序(结构)打印问题
- Visual Studio 2012 - 嵌入式输出控制台,而不是cmd