Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how to convert from WPARAM or LPARAM to CString

how to convert from WPARAM or LPARAM to CString

Scheduled Pinned Locked Moved C / C++ / MFC
tutorial
7 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    singersinger
    wrote on last edited by
    #1

    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

    N P J 3 Replies Last reply
    0
    • S singersinger

      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

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • S singersinger

        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

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #3

        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 the str goes out of scope (by which time it's destructor is called). Chances are (very likely) that the str object has been destroyed before Handler() is called. You passed a pointer to the internal string buffer inside the str object. That internal string buffer is deleted in the destructor of the str 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!

        Z 1 Reply Last reply
        0
        • S singersinger

          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

          P Offline
          P Offline
          prasad_som
          wrote on last edited by
          #4

          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 by ReleaseBuffer to avoid memory leak.

          Prasad Notifier using ATL | Operator new[],delete[][^]

          J 1 Reply Last reply
          0
          • N Naveen

            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

            S Offline
            S Offline
            singersinger
            wrote on last edited by
            #5

            thnx alot 4 ur fast reply this code solved the problem thanks agian :rose:;)

            1 Reply Last reply
            0
            • P prasad_som

              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 by ReleaseBuffer to avoid memory leak.

              Prasad Notifier using ATL | Operator new[],delete[][^]

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #6

              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!

              1 Reply Last reply
              0
              • J Jorgen Sigvardsson

                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 the str goes out of scope (by which time it's destructor is called). Chances are (very likely) that the str object has been destroyed before Handler() is called. You passed a pointer to the internal string buffer inside the str object. That internal string buffer is deleted in the destructor of the str 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!

                Z Offline
                Z Offline
                Zac Howland
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups