Hello all.
I want to show a boolean value with MessageBox. But it only accepts strings. I'm currently looking throught win32 sdk but cant find any function to convert a bool to a string(something like .Tostring in c#). Any help ?

Dani AI

Generated

As suggested, the Win32 API does not provide a dedicated "bool->string" helper. The usual approaches are simple mapping (fast and clear) or using C++ string/stream facilities when a textual form like "true"/"false" is needed.

A compact, portable pattern that works in ANSI and Unicode builds:

#include <windows.h>

void ShowBool(bool flag)
{
    MessageBox(NULL, flag ? TEXT("true") : TEXT("false"), TEXT("Flag"), MB_OK);
}

If an explicit conversion to a std::string/std::wstring is preferred (or the result needs further processing), two common options:

  • Ternary or small lookup array (fast, no streams).
  • Streams with std::boolalpha to produce the words "true"/"false".

Examples:

#include <windows.h>
#include <sstream>

void ShowBoolStream(bool flag)
{
    std::ostringstream oss;
    oss << std::boolalpha << flag;   // "true" or "false"
    MessageBoxA(NULL, oss.str().c_str(), "Flag", MB_OK);
}

Notes and best practices:

  • std::to_string(true) will produce "1", not "true" (because bool converts to an integer), so use boolalpha or explicit mapping for readable words.
  • For Unicode builds, use wide strings (MessageBoxW or TEXT/L prefixes) or std::wostringstream.
  • For user-facing text prefer meaningful labels ("Enabled"/"Disabled", "On"/"Off") and localize via resource strings rather than raw "true"/"false".
  • For quick debugging, the ternary/operator or const char* array is simplest and cheapest; streams are fine when code clarity or formatting is more important.

The OP accepted the simple-mapping idea; the examples above show concise, safe ways to implement that in real Win32/C++ code.

Recommended Answers

All 2 Replies

Why not just make a switch/case or simple variable statement?
If true = one string, If its false = another string.

Ok thanks, nice ideia =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.