how to convert from WPARAM or LPARAM to CString
-
i want to post a thread message that contain a CString variable like this:
PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)sString.GetBuffer(sString.GetLength()));
and then handle the msg at the Thread side to process this CString variable like this:CString s = (LPTSTR)pMsg->lParam; AfxMessageBox(s);
the code compile without errors but the data displayed in the Message box is not the same CString variable that have been sent so i wanna know wat is wrong with this code thnx 4 ur time and concern -
i want to post a thread message that contain a CString variable like this:
PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)sString.GetBuffer(sString.GetLength()));
and then handle the msg at the Thread side to process this CString variable like this:CString s = (LPTSTR)pMsg->lParam; AfxMessageBox(s);
the code compile without errors but the data displayed in the Message box is not the same CString variable that have been sent so i wanna know wat is wrong with this code thnx 4 ur time and concernsingersinger wrote:
PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)sString.GetBuffer(sString.GetLength()));
u pass a pointer to the function. Some times by the time the message was recieved by the thread, sString have got desctructed. So the memory that the lParam was pointing will b a invalid one. Try the bolow approch. LPTSTR lpMessage = new TCHAR[sString.GetLength()]; _tcscpy( lpMessage , sString); PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)lpMessage); and in the thread side LPTSTR lpMessage = (LPTSTR)pMsg->lParam;; CString s = lpMessage; delete lpMessage; AfxMessageBox(s);
nave
-
i want to post a thread message that contain a CString variable like this:
PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)sString.GetBuffer(sString.GetLength()));
and then handle the msg at the Thread side to process this CString variable like this:CString s = (LPTSTR)pMsg->lParam; AfxMessageBox(s);
the code compile without errors but the data displayed in the Message box is not the same CString variable that have been sent so i wanna know wat is wrong with this code thnx 4 ur time and concernIf you post a message, you can never be sure when in time the message will arrive on the other side. As such, you cannot use a CString object, unless you know for sure that will be alive when it arrives at the message's recipient. Consider this:
{
CString str = ...;
PostThreadMessage(m_pThread->m_nThreadID, WM_MSG, 0, LPARAM(str.GetBuffer());
}
// By now, the str object is dead (destructor has been called)// Recipient function
LRESULT Handler(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
LPCTSTR lpsz = LPCTSTR(lParam);
DoStuff(lpsz);
}Can you see the race condition? This will work, if and only if, the
Handler()
function gets to execute before thestr
goes out of scope (by which time it's destructor is called). Chances are (very likely) that thestr
object has been destroyed beforeHandler()
is called. You passed a pointer to the internal string buffer inside thestr
object. That internal string buffer is deleted in the destructor of thestr
object. So,Handler()
will receive a dangling pointer... It is much safer if you do this:{
CString str = ...;
TCHAR* copy = new TCHAR[str.GetLength() + 1];
lstrcpy(copy, str.GetBuffer()); str.ReleaseBuffer();
PostThreadMessage(m_pThread->m_nThreadID, WM_MSG, 0, LPARAM(copy));
}// Recipient function
LRESULT Handler(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
LPTSTR lpsz = LPCTSTR(lParam);
DoStuff(lpsz);
delete [] lpsz;
}You allocate a copy of the string in the which you pass to the handler. It is then up to the handler to delete the string.
-- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
-
i want to post a thread message that contain a CString variable like this:
PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)sString.GetBuffer(sString.GetLength()));
and then handle the msg at the Thread side to process this CString variable like this:CString s = (LPTSTR)pMsg->lParam; AfxMessageBox(s);
the code compile without errors but the data displayed in the Message box is not the same CString variable that have been sent so i wanna know wat is wrong with this code thnx 4 ur time and concernsingersinger wrote:
PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)sString.GetBuffer(sString.GetLength()));
I agree what Navin has said. sString is destroyed by the time you tried to access it. One more thing,
GetBuffer
call must accompanied byReleaseBuffer
to avoid memory leak.Prasad Notifier using ATL | Operator new[],delete[][^]
-
singersinger wrote:
PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)sString.GetBuffer(sString.GetLength()));
u pass a pointer to the function. Some times by the time the message was recieved by the thread, sString have got desctructed. So the memory that the lParam was pointing will b a invalid one. Try the bolow approch. LPTSTR lpMessage = new TCHAR[sString.GetLength()]; _tcscpy( lpMessage , sString); PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)lpMessage); and in the thread side LPTSTR lpMessage = (LPTSTR)pMsg->lParam;; CString s = lpMessage; delete lpMessage; AfxMessageBox(s);
nave
thnx alot 4 ur fast reply this code solved the problem thanks agian :rose:;)
-
singersinger wrote:
PostThreadMessage(m_pThread->m_nThreadID,WM_MSG,0,(LPARAM)sString.GetBuffer(sString.GetLength()));
I agree what Navin has said. sString is destroyed by the time you tried to access it. One more thing,
GetBuffer
call must accompanied byReleaseBuffer
to avoid memory leak.Prasad Notifier using ATL | Operator new[],delete[][^]
prasad_som wrote:
One more thing, GetBuffer call must accompanied by ReleaseBuffer to avoid memory leak.
No, that is not true. The CString object's methods are however not safe to use before a matching call to ReleaseBuffer().
-- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
-
If you post a message, you can never be sure when in time the message will arrive on the other side. As such, you cannot use a CString object, unless you know for sure that will be alive when it arrives at the message's recipient. Consider this:
{
CString str = ...;
PostThreadMessage(m_pThread->m_nThreadID, WM_MSG, 0, LPARAM(str.GetBuffer());
}
// By now, the str object is dead (destructor has been called)// Recipient function
LRESULT Handler(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
LPCTSTR lpsz = LPCTSTR(lParam);
DoStuff(lpsz);
}Can you see the race condition? This will work, if and only if, the
Handler()
function gets to execute before thestr
goes out of scope (by which time it's destructor is called). Chances are (very likely) that thestr
object has been destroyed beforeHandler()
is called. You passed a pointer to the internal string buffer inside thestr
object. That internal string buffer is deleted in the destructor of thestr
object. So,Handler()
will receive a dangling pointer... It is much safer if you do this:{
CString str = ...;
TCHAR* copy = new TCHAR[str.GetLength() + 1];
lstrcpy(copy, str.GetBuffer()); str.ReleaseBuffer();
PostThreadMessage(m_pThread->m_nThreadID, WM_MSG, 0, LPARAM(copy));
}// Recipient function
LRESULT Handler(UINT nMsg, WPARAM wParam, LPARAM lParam)
{
LPTSTR lpsz = LPCTSTR(lParam);
DoStuff(lpsz);
delete [] lpsz;
}You allocate a copy of the string in the which you pass to the handler. It is then up to the handler to delete the string.
-- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!
Jörgen Sigvardsson wrote:
lstrcpy(copy, str.GetBuffer()); str.ReleaseBuffer();
There is no need to call GetBuffer here. Just use the CString's implicit cast to LPCTSTR.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac