22

I've tried OutputDebugString function and most of the time I get error like :

error C2664: 'OutputDebugStringA' : cannot convert parameter 1 from 'int' to 'LPCSTR'

Examples

Attempt 1:

//ERROR: sprintf is unsafe. Use sprintf_s instead
int x = 4;
char s[256];
sprintf(s, "There is %d numbers", x);
OutputDebugString((LPCWSTR)s);

Attempt 2:

//FAIL: outputs junk (sprintf_s doesn't understand unicode?)
int x = 4;
char s[256];
sprintf_s(s, "There is %d numbers", x);
OutputDebugString((LPCWSTR)s);

Attempt 3:

//ERROR: no instance of overloaded function "sprintf_s" matches the argument list
int x = 4;
TCHAR s[256];
sprintf_s(s, "There is %d numbers", x);
OutputDebugString((LPCWSTR)s);

Attempt 4:

//ERROR: no instance of overloaded function "sprintf_s" matches the argument list
int x = 4;
TCHAR s[256];
sprintf_s(s, L"There is %d numbers", x);
OutputDebugString((LPCWSTR)s);

Attempt 5:

//ERROR: no instance of overloaded function "swprintf" matches the argument list
int x = 4;
TCHAR s[256];
swprintf(s, "There is %d numbers", x);
OutputDebugString(s);

Attempt 6:

//ERROR: 'swprintf': function has been changed to confirm with the ISO C standard, adding an extra character count parameter
int x = 4;
TCHAR s[256];
swprintf(s, L"There is %d numbers", x);
OutputDebugString(s);
Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
understack
  • 11,212
  • 24
  • 77
  • 100

6 Answers6

29

It only accepts a string as a parameter, not an integer. Try something like

sprintf(msgbuf, "My variable is %d\n", integerVariable);
OutputDebugString(msgbuf);

For more info take a look at http://www.unixwiz.net/techtips/outputdebugstring.html

peter_mcc
  • 816
  • 5
  • 11
  • 5
    + @Jon: Even better yet, consider using std::stringstream. – Billy ONeal Jul 05 '10 at 12:28
  • 1
    @Billy ONeal: I used std::stringstream instead of sprintf/sprintf_s as you suggested. Thanks. – understack Jul 05 '10 at 17:05
  • @BillyONeal Can you explain why using stringstream would be better? Just curious. – Trevor Hart Mar 23 '17 at 05:03
  • 1
    @TrevorHart Bill of 2017 doesn't wholly agree with Bill of 2010. The primary advantage is not needing to worry about the destination buffer size. But there are plenty of uses for sprintf:) – Billy ONeal Mar 23 '17 at 18:17
  • @BillyONeal How would you use `std::stringstream`? What is the syntax? – Ian Boyd Oct 30 '21 at 19:45
  • 1
    What do you declare `msgbuf` as? `sprintf` takes a `char *` and `OutputDebugString` takes a `LPCWSTR`. So i can't figure out a declaration of `msgbuf` that would compile. – Ian Boyd Oct 30 '21 at 19:47
15

For debugging purposes you could use _RPT macros.

For instance,

_RPT1( 0, "%d\n", my_int_value );
Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
11

The most common way I'm aware of is the TRACE macro:

http://msdn.microsoft.com/en-us/library/4wyz8787%28VS.80%29.aspx

For example:

int x = 1;
int y = 16;
float z = 32.0;
TRACE( "This is a TRACE statement\n" );

TRACE( "The value of x is %d\n", x );

TRACE( "x = %d and y = %d\n", x, y );

TRACE( "x = %d and y = %x and z = %f\n", x, y, z );
Inverse
  • 4,408
  • 2
  • 26
  • 35
7

I found this answer when searching the error message: https://stackoverflow.com/a/29800589

Basically, you just need put an "L" in front of your output string when using OutputDebugString:

OutputDebugString(L"test\n");

It worked great for me.

Edit:

For formatting strings with data, I ended up using

char buffer[100];
sprintf_s(buffer, "check it out: %s\n", "I can inject things");
OutputDebugStringA(buffer);

By no means am I an expert, I just found something that worked and moved on.

trickymind
  • 557
  • 5
  • 21
Jangles
  • 510
  • 6
  • 18
  • That doesn't account for if I want to format the string and pass data to it. – Trevor Hart Mar 23 '17 at 05:04
  • I added my solution that worked for injection. I specialize in the iOS platform and languages, please don't regard this as an expert solution :) – Jangles Mar 24 '17 at 16:06
  • 2
    Depending on your compiler options and settings `OutputDebugString` is translated into one of two targets, `OutputDebugStringW` for wide text characters (`wchart_t`) or `OutputDebugStringA` for narrow text characters (`char`). The `L` is required for creating a `wchar_t` wide character string as in `L"wide char"` versus `"narrow char"`. The `sprintf_s()` function uses narrow characters, standard `char` type, so the `char buffer[100];` content is printed to the Output window using `OutputDebugStringA()` to force the use of narrow character text. – Richard Chambers Apr 09 '18 at 20:52
1

Use:

OutputDebugStringA("Some random text");

Or:

OutputDebugString("Some random text");
Kingeh
  • 29
  • 4
0

To use OutputDebugString(), provide char * or const char * as parameter:

OutputDebugString("This is an output");
Donotalo
  • 12,748
  • 25
  • 83
  • 121