Win32控制台写入(C/C++)

Win32 console write (C/C++)

本文关键字:C++ 控制台 Win32      更新时间:2023-10-16

我想在C++程序中编写控制台,而不使用"std"库,也就是说,只使用"Windows.h"中的函数。原因是我想深入研究可移植的可执行文件,看到这一个函数被调用,而不是一堆函数层。有人知道如何做到这一点和/或在哪里可以找到"Windows.h"功能指南吗?

使用纯Win API:

HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (stdOut != NULL && stdOut != INVALID_HANDLE_VALUE)
{
    DWORD written = 0;
    const char *message = "hello world";
    WriteConsoleA(stdOut, message, strlen(message), &written, NULL);
}

MSDN是您最好的文档来源之一:

  • GetStdHandle
  • WriteConsole

替代Win32 API:

#ifndef UNICODE
    std::stringstream ss;
    ss << TEXT("Hello world!") << std::endl;
    OutputDebugString(ss.str().c_str());
    OutputDebugStringA(ss.str().c_str());
#endif
#ifdef UNICODE
    std::wstringstream wss;
    wss << TEXT("Hello world!") << std::endl;
    OutputDebugString(ss.str().c_str());
    OutputDebugStringW(wss.str().c_str());
#endif

OutputDebugString是一个宏

参考:

  • 调试时使用OutputDebugString
  • OutputDebugStringA函数(debugapi.h)-Win32应用程序|Microsoft文档
  • OutputDebugStringW函数(debugapi.h)-Win32应用程序| Microsoft文档