c++winapimfccstringpostmessage

How do I convert a wParam to a CString?


I have a pMsg->wParam from a WM_KEYDOWN message, and I want to convert it into a CString. How can I do that?

I have tried the following code:

TCHAR ch[2];
ch[0] = pMsg->wParam;
ch[1] = _T('\0');
CString ss(ch);

but it does not work for high ASCII characters.


Solution

  • The problem is that wParam contains a pointer to an array of characters. It is not a single character, so you can't create the string yourself by assigning it to ch[0] as you're trying to do here.

    The solution turns out to be a lot easier than you probably expected. The CString class has a constructor that takes a pointer to a character array, which is precisely what you have in wParam.
    (Actually, it has a bunch of constructors, one for pretty much everything you'll ever need...)

    So all you have to do is:

    CString ss(pMsg->wParam);
    

    The constructor will take care of the rest, copying the string pointed to by wParam into the ss type.