Click here to Skip to main content
14,694,269 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to pass CString as argument to the PostMessage? and again how to convert LPARAM/WPARAM back to CString? Help me!
Posted

1 solution

You don't.
When you post a message, you don't know when it will be received. It can be received after the string you want to pass is already out of scope.
If you're posting a message to another window in the same process, use the source/sink idiom (AKA producer/consumer):

void producer()
{
    wchar_t * wstring = new wchar_t[80];
    wcscpy(wstring, L"HELLO");
    ::PostMessage(HWND, MY_CUSTOM_MESSAGE, 0, (LPARAM) wstring);
    // DO NOT DELETE wstring;
}

class Consumer
{
    LRESULT On_MyCustomMessage(WPARAM /*notused*/, LPARAM lpWstring)
    {
        wchar_t *pString = (wchar_t *) lpWstring;
        // Do something with pString... You can even pass it to a CStringW constructor. 
        delete[] pString; // and don't forget this!
        return 0L;
    }   
};

If you're using CString, see the AllocSysString() member function. BSTRs are meant to be passed around.

Hope this helps,
Pablo.
   
Comments
Member 12038742 7-Oct-15 3:29am
   
where do I place the consumer in my code?
Pablo Aliskevicius 8-Oct-15 2:20am
   
Typically, in the class that receives the message that was posted. It may handle the message by converting the raw pointer to a CString or CStringW, passing it to the business logic API that required it in the first place, and deleting it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900