Ryan Brown
Ryan Brown

Reputation: 1085

Win32 console write (C/C++)

I'd like to write to the console in a C++ program without using the 'std' libraries, that is, just using the functions in "Windows.h". The reason is I want to dig into the portable executable and see this one function being called, not a bunch of function layers. Anyone know how to do this and/or where I can find a guide to the "Windows.h" functions?

Upvotes: 4

Views: 11719

Answers (2)

KaiserKatze
KaiserKatze

Reputation: 1569

Alternative 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 is a macro.

Reference:

Upvotes: -1

josh poley
josh poley

Reputation: 7479

Using pure Win APIs:

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 is one of your best sources for documentation:

Upvotes: 12

Related Questions