1

I am new to win32 api and as a part of my learning, I want to print out the coordinates of my mouse (LOWORD(lParam), HIWORD(lParam)). How do I do that? The OutputDebugString only gets LPCWSTR value, so do I need to convert the int to that? or there is a better way to do that?

Omi Kor
  • 25
  • 4
  • [Windows with C++ - Using Printf with Modern C++](https://learn.microsoft.com/en-us/archive/msdn-magazine/2015/march/windows-with-c-using-printf-with-modern-c). Key takeaway: Your problem is unrelated to `OutputDebugStringW`. – IInspectable Dec 28 '20 at 14:56
  • The problem is that I can't see printf because I'm in SUBSYSTEM:WINDOWS, so how do I view printf? – Omi Kor Dec 28 '20 at 14:59
  • 1
    You can use SUBSYSTEM:CONSOLE with a GUI application. I do this often in debug mode for my Qt applications but have it disabled in release. With this said you may have to us int main() instead of WinMain, I don't remember I have not done winapi directly in 2 decades. – drescherjm Dec 28 '20 at 15:04
  • I have an old answer here that may help: [https://stackoverflow.com/questions/13840942/visual-studio-2012-c-standard-output/13841522#13841522](https://stackoverflow.com/questions/13840942/visual-studio-2012-c-standard-output/13841522#13841522) – drescherjm Dec 28 '20 at 15:07
  • 2
    Sounds like you aren't just new to the Windows API, but C (and C++) as well. Hence my comment. You `printf` into a memory buffer using the [`vsprintf`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/vsprintf-vsprintf-l-vswprintf-vswprintf-l-vswprintf-l) family of functions. – IInspectable Dec 28 '20 at 15:46
  • @IInspectable that is the C way to approach this. The C++ way is to use `std::ostringstream` instead. – Remy Lebeau Dec 28 '20 at 17:59
  • 1
    @rem That's the entirely not sane C++ way. The sane(-ish) C++ way is [std::format](https://en.cppreference.com/w/cpp/utility/format/format). – IInspectable Dec 28 '20 at 18:25
  • Disregarding the C vs. C++ debate, [these debugging report macros](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/rpt-rptf-rptw-rptfw-macros) available to Microsoft Visual C++ compiler are designed to assist with the kind of thing some people use `OutputDebugString` for, if my memory serves me right -- I just came here to share the link. I am open to being proven wrong. The reason I don't want to get into the "is this C or is this C++" is that we should acknowledge the irony that is Microsoft Visual _C++_ compiler interfacing with WinAPI, written in C. – Armen Michaeli Feb 25 '21 at 20:17

1 Answers1

2

OutputDebugString only knows how to send strings of text to the debugger.

OutputDebugString("This works because this is a string.\n");
int x = GetMouseXCoordinate();
OutputDebugString(x);  // This doesn't work because x isn't a string.

If you want to print something that's not already a string of text, you'll first have to create a string with the textual representation and then send that to OutputDebugString.

int x = GetMouseXCoordinate();
// Here's a method commonly used in C and C++ to format the value yourself.
char message[63];
sprintf(message, "%d", x);
OutputDebugString(message);

There are alternatives (like using itoa, std::stringstream, safer versions of sprintf, etc.) Regardless this can be cumbersome if you want to send lots of messages to a debugger's output window. Many of the posts linked in the comments on your question are ways to isolate the formatting step using preprocessor macros, C++ function overloads, or templates. All of these solutions have trade-offs.

My personal solution is to derive my own type of stream buffer from std::basic_streambuf and to override a couple of its virtual methods to direct the output to an attached debugger. Then I create an instance of an output stream called dbg that uses my string buffer. So, when I want to output to the debug window, I just use C++ formatted output:

dbg << "Mouse coordinates: " << x << ", " << y << std::endl;

In release builds, dbg is a dummy class that does nothing, and the optimizer is smart enough to not bother generating any of the formatting and output code.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175