How to convert a LPARAM lParam to a CString

The rookie picture The rookie · Dec 14, 2012 · Viewed 7.1k times · Source

In my worker thread I create a message that I send to my main dialog. Once the main dialog receives the message and goes into my OnStatusUpdate() function I would like to extract the message from LPARAM lParam and display it in the dialogs edit box. Instead of getting my message all I get is garbage.

UINT CGDC74xDlg::LeakTstThread( LPVOID pParam )
{
    HWND *phObjectHandle = static_cast<HWND *>(pParam);
    CString strTmp = "It works!";
    CString * message = new CString(strTmp);
    ::PostMessage(*phObjectHandle, WM_LEAK_TEST_THREAD, 0, (LPARAM)message);
    ::Sleep(5000);
    delete phObjectHandle;
    return( 0 );
}

LRESULT CGDC74xDlg::OnStatusUpdate(WPARAM wParam, LPARAM lParam)
{
    LPTSTR lpMessage = (LPTSTR)lParam;
    CString s = lpMessage;
    delete lpMessage;
    GetDlgItem(IDC_EDIT_LEAK_TEST_RESULTS)->SetWindowText(s);
    return 0;
}

Answer

Jonathan Wood picture Jonathan Wood · Dec 14, 2012

In your case, LPARAM does not contain a CString. It contains a pointer to a CString.

So you would obtain it like this:

CString *message = (CString*)lParam;