Monday, March 7, 2011

Proper way of converting int to LPCWSTR (Win32)

I'm attempting to learn basic Win32 programming and running into a frustrating problem. I wish to convert a variable (Call it NumClicks) and display it as the application name (as part of a string). From what I've seen, going from int + some block of text to a char* is problematic, because converting it to the requisite end data type(LPCWSTR) is more difficult than a straight casting.

Any ideas or links?

From stackoverflow
  • use wsprintf
    It allows you to compose a string the same way printf allows you to print a line of text.

    Daniel Goldberg : Doesn't fully solve my problem.
    Matteo Italia : In which way it doesn't fully solve your problem? By the way, you'd better use the more secure (and still standard) wsnprintf.
  • _itow_s

    If you're looking for more than just INT to LPWSTR conversion (such as formatting), I'd suggest StringCchPrintfW.

  • rather than use wsprintf i would use sprinf(buf, "%S", "plain old string");

    Matteo Italia : With the result that you don't get a wide string, which instead is what he need (LPCWSTR = const __wchar_t__ *).
  • Here is the code I used, when I needed LPCWSTR:

    
    int f =55;  
    wchar_t buffer[10];
    _itow_s (f, buffer, 10);
    func_using_lpcwstr_as_parameter(buffer);
    

    watch for buffer overflows in this sample

0 comments:

Post a Comment