xec86 xec86 - 18 days ago 5
C++ Question

Cannot output cursor location to debug string (WIN32)

For some reason, when I am trying to output the location of the mouse cursor in a given WIN32 window using the following code:

//Global Variable
POINT cursorLocation;

// Win32 Windowing subsystem code redacted

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){

cursorLocation.x = GET_X_LPARAM(lParam);
cursorLocation.y = GET_Y_LPARAM(lParam);

switch(message){
case WM_MOUSEMOVE:
// mouse movement handle
OutputDebugString(cursorLocation.x + "," + cursorLocation.y);
OutputDebugString("\n");
// WM_MOUSEMOVE break
break;
}
}


Now when I run the program and move the mouse, the following text is logged to the console:


a smaller data type has caused a loss of data.

If this was intentional, you should mask the source of the cast with the appropriate bitmask.

For example: char c = (i & 0xFF);

Changing the code in this way will not affect the quality of the resulting optimized code.


I even tried typecasting the variables passed to
OutputDebugString
to a
LONG
, because that is the type of the variables in the
POINT
class and there was no difference.

Does anyone know how to have the values passed through the
GET_X_LPARAM
and
GET_Y_LPARAM
to the debug console?
Thanks.

Answer

This is not a string concatenation, but adding .x and .y to pointer to ",":

cursorLocation.x + "," + cursorLocation.y

Instead, try e.g.:

char s[256];
sprintf_s(s, "%d,%d", cursorLocation.x, cursorLocation.y);
OutputDebugString(s);