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);
}
class Consumer
{
LRESULT On_MyCustomMessage(WPARAM , LPARAM lpWstring)
{
wchar_t *pString = (wchar_t *) lpWstring;
delete[] pString; return 0L;
}
};
If you're using
CString
, see the
AllocSysString()
member function. BSTRs are meant to be passed around.
Hope this helps,
Pablo.