lolbas
lolbas

Reputation: 794

winapi - display int variable value in MessageBox using macros

For debugging purposes I am trying to make a short macros to display various types, instead of constant copying all the MessageBox functions' params.

For strings I have following macros:

#define DEBUG(x) MessageBox(NULL, x,"DEBUG",MB_ICONINFORMATION|MB_OK);

Calling it working great, whether I pass variable (array of char) or direct string.

Now, I try to make the same thing for int. Have defined macros like this:

#define STRIGIFY(x) #x
#define TOSTRING(x) STRIGIFY(x)
#define DEBUGINT(x) DEBUG(TOSTRING(x))

It works only in case I pass direct integer value:

DEBUGINT(742);

However if I pass int variable, MessageBox displays variable name instead of its value:

int count = 3;
DEBUGINT(count);

The thing I find pretty interesting for me is that I can pass literally anything in DEBUGINT macros and it will still work:

DEBUGINT(some unescaped string)

How do I define a macros that would use a variable value instead of its name?

Upvotes: 0

Views: 262

Answers (4)

Vlad Feinstein
Vlad Feinstein

Reputation: 11311

This doesn't answer the question as it was asked, but I'll risk my reputation and suggest a different solution.

PLEASE, do yourself a favor and never use MessageBox() or other modal UI to display debug information. If you do want to interrupt program execution at that point, use the breakpoint; it also allows you to attach the condition, so that you don't need to examine the value manually.

If you do not want the interruption, just print the value to a debug output window using ::OutputDebugString(). That can be seen in the debugger if it is attached, or via DebugView tool.

Another small suggestion (for Visual Studio users): if you prepend your output with a source file name and the code line number, double-clicking on that line in the output window will take you straight to that line. Just use __FILE__ and __LINE__ in your formatted string.

Upvotes: 3

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

You can use variable argument list

#include <stdio.h>
void message(const char* format, ...)
{
    int len;
    char *buf;
    va_list args;
    va_start(args, format);

    len = _vscprintf(format, args) + 1;  //add room for terminating '\0'
    buf = (char*)malloc(len * sizeof(char));
    vsprintf_s(buf, len, format, args);

    MessageBoxA(0,buf,"debug",0);
    //OutputDebugStringA(buf);
    free(buf);
}

message("test %s %d %d %d", "str", 1, 2, 3);

You might also want to change to unicode version.

Upvotes: 2

Ian Abbott
Ian Abbott

Reputation: 17403

You need to "print" the variable to a buffer (array of char) using something like sprintf (or snprintf in VS 2015) and pass the resulting output to MessageBox as the string to be displayed.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409136

You can't. The preprocessor doesn't know anything about variables or their values, because it doesn't do anything run-time only at compile-time.

Upvotes: 2

Related Questions