How do I create a message box that lets me display a variable? I also need to know how to display variables with text. Here is the code in the switch(message) statement that I have at the moment that doesn't work.

case WM_CREATE:
            CreateWindow(
                TEXT("button"), TEXT("Click"),
                WS_VISIBLE | WS_CHILD | WS_BORDER,
                10, 30, 55, 20,
                hwnd, (HMENU) 1, NULL, NULL
            );
            break;
        case WM_COMMAND:
            if(LOWORD(wParam) == 1){
                money--; //this has already been declared at 50.
                MessageBox(hwnd, money, "New money value", MB_OK); //also tried money.toString() but that doesn't work either.
            }
            break;

Dani AI

Generated

Short summary and correction of the root cause (ties to ): MessageBox does not accept an integer directly — it expects a pointer to a null‑terminated string (LPCTSTR). Passing money as shown in the first post is a type/ABI mismatch (and can crash, especially on 64‑bit). As noted, .ToString() is a .NET method and is not available to Win32/C++ code. Two safer and practical approaches follow.

A C++11 (wide/Unicode) approach — avoids manual buffers:

// requires <string>
money--;
std::wstring msg = L"New money value: " + std::to_wstring(money);
MessageBoxW(hwnd, msg.c_str(), L"Info", MB_OK);

A safe Win32 C API approach using the Windows Safe String functions:

// requires <strsafe.h>
TCHAR buf[64];
StringCchPrintf(buf, 64, TEXT("New money value: %d"), money);
MessageBox(hwnd, buf, TEXT("Info"), MB_OK);

Practical notes and troubleshooting:

  • MessageBox parameters are (hwnd, lpText, lpCaption, uType) — the text (message body) goes first, then the caption.
  • Match string APIs to the build: use MessageBoxW/std::wstring for Unicode, MessageBoxA/std::string for ANSI, or the generic MessageBox + TEXT/_T macros when using TCHAR.
  • Prefer safe formatters (StringCchPrintf, std::to_string/std::to_wstring, ostringstream) over _stprintf to avoid buffer overflow.
  • Keep buffers big enough (64+ chars for small messages) or compute sizes dynamically.
  • ’s control‑ID tip (start at 1500) is good practice to avoid clashes with system values.
  • For drawing text inside a window (not a modal box), TextOut / DrawText are the correct APIs.

These patterns make the string formatting explicit and eliminate the type mismatch that caused the original failure.

Recommended Answers

All 4 Replies

case WM_CREATE:
  CreateWindow(
     TEXT("button"), TEXT("Click"),
     WS_VISIBLE | WS_CHILD | WS_BORDER,
     10, 30, 55, 20,
     hwnd, (HMENU) 1, NULL, NULL
   );
   break;
case WM_COMMAND:
   if(LOWORD(wParam) == 1)
   {
      TCHAR szBuffer[16];
      money--; //this has already been declared at 50.
      _stprintf(szBuffer,_T("%d"),money);
      MessageBox(hwnd, szBuffer, _T("New money value"), MB_OK); //also tried money.toString() but that doesn't work either.
   }
   break;

.ToString is .NET stuff. Won't work in Win32.

I like to start control ids at 1500, so they don't interfere too badly with MS defined values. Also, use equates...

#define IDC_BUTTON1 1500

Also, for that to compile you should include....

#include <windows.h>
#include <tchar.h>
#include <cstdio>

Let me know if it doesn't work. Maybe I missed something else...

case WM_CREATE:
  CreateWindow(
     TEXT("button"), TEXT("Click"),
     WS_VISIBLE | WS_CHILD | WS_BORDER,
     10, 30, 55, 20,
     hwnd, (HMENU) 1, NULL, NULL
   );
   break;
case WM_COMMAND:
   if(LOWORD(wParam) == 1)
   {
      TCHAR szBuffer[16];
      money--; //this has already been declared at 50.
      _stprintf(szBuffer,_T("%d"),money);
      MessageBox(hwnd, szBuffer, _T("New money value"), MB_OK); //also tried money.toString() but that doesn't work either.
   }
   break;

.ToString is .NET stuff. Won't work in Win32.

I like to start control ids at 1500, so they don't interfere too badly with MS defined values. Also, use equates...

#define IDC_BUTTON1 1500

Also, for that to compile you should include....

#include <windows.h>
#include <tchar.h>
#include <cstdio>

Let me know if it doesn't work. Maybe I missed something else...

Thanks. Also, how would I print some text and a variable?

Thanks. Also, how would I print some text and a variable?

Check out my first GUI tutorial here...

Everything you need to know!

Although the short answer is TextOut(). But, having said that, since you were unsure how to represent an integral value within a character string buffer and output it with MessageBox, it likely wouldn't hurt you to examine the link above.

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.