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. Send a message from a thread to a function with parameters

Send a message from a thread to a function with parameters

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
8 Posts 3 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.
  • W Offline
    W Offline
    willempipi
    wrote on last edited by
    #1

    I've got this working: The Thread: UINT CheckMessages(LPVOID Param) { CString tekst = "This is a tekst or maybe a structure"; ::PostMessage((HWND)Param, WM_DATARECEIVED, (WPARAM)&tekst, 0); return 0; } The header: protected: //{{AFX_MSG(CPLCClientView) afx_msg void OnDataReceived(WPARAM wParam, LPARAM lParam); //}}AFX_MSG DECLARE_MESSAGE_MAP() The Function: BEGIN_MESSAGE_MAP(CPLCClientView, CFormView) //{{AFX_MSG_MAP(CPLCClientView) ON_MESSAGE(WM_DATARECEIVED, OnDataReceived) //}}AFX_MSG_MAP END_MESSAGE_MAP() ... ... void CMyClass::OnDataReceived(WPARAM wParam, LPARAM lParam) { CString tekst = (CString)wParam; // or something like that.... AfxMessageBox(tekst); } If i execute this i get a weird looking character(because i get a number with wParam), how can i send the CString towards my function? me = Visual C++ n00b

    C 1 Reply Last reply
    0
    • W willempipi

      I've got this working: The Thread: UINT CheckMessages(LPVOID Param) { CString tekst = "This is a tekst or maybe a structure"; ::PostMessage((HWND)Param, WM_DATARECEIVED, (WPARAM)&tekst, 0); return 0; } The header: protected: //{{AFX_MSG(CPLCClientView) afx_msg void OnDataReceived(WPARAM wParam, LPARAM lParam); //}}AFX_MSG DECLARE_MESSAGE_MAP() The Function: BEGIN_MESSAGE_MAP(CPLCClientView, CFormView) //{{AFX_MSG_MAP(CPLCClientView) ON_MESSAGE(WM_DATARECEIVED, OnDataReceived) //}}AFX_MSG_MAP END_MESSAGE_MAP() ... ... void CMyClass::OnDataReceived(WPARAM wParam, LPARAM lParam) { CString tekst = (CString)wParam; // or something like that.... AfxMessageBox(tekst); } If i execute this i get a weird looking character(because i get a number with wParam), how can i send the CString towards my function? me = Visual C++ n00b

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      willempipi wrote: void CMyClass::OnDataReceived(WPARAM wParam, LPARAM lParam) { CString tekst = (CString)wParam; // or something like that.... AfxMessageBox(tekst); } Where you cast the wParam to a CString, I think you should cast it to a CString* instead. You will find however that this too will not work as you expect though. By the time you execute the message handler, OnDataReceived, the thread proc will have already finished and the instance of a CString on the stack will no longer exist. I would suggest that you 'new' a CString in the thread proc and then 'delete' the CString in the OnDataReceived method. Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.

      W 1 Reply Last reply
      0
      • C Chris Meech

        willempipi wrote: void CMyClass::OnDataReceived(WPARAM wParam, LPARAM lParam) { CString tekst = (CString)wParam; // or something like that.... AfxMessageBox(tekst); } Where you cast the wParam to a CString, I think you should cast it to a CString* instead. You will find however that this too will not work as you expect though. By the time you execute the message handler, OnDataReceived, the thread proc will have already finished and the instance of a CString on the stack will no longer exist. I would suggest that you 'new' a CString in the thread proc and then 'delete' the CString in the OnDataReceived method. Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.

        W Offline
        W Offline
        willempipi
        wrote on last edited by
        #3

        Chris Meech wrote: I would suggest that you 'new' a CString in the thread proc can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this: void CPLCClientView::OnDataReceived(CString tekst) { AfxMessageBox(tekst); } The Visual C++ n00p

        H C 2 Replies Last reply
        0
        • W willempipi

          Chris Meech wrote: I would suggest that you 'new' a CString in the thread proc can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this: void CPLCClientView::OnDataReceived(CString tekst) { AfxMessageBox(tekst); } The Visual C++ n00p

          H Offline
          H Offline
          HENDRIK R
          wrote on last edited by
          #4

          willempipi wrote: can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this: Since you're dealing with user defined messages, no. Sending your own message and handling it with ON_MESSAGE requires you to provide a function declarated like that you did before (with WPARAM and LPARAM as parameters). Only then the message is handled correctly. It's not comparable to messages like WM_PAINT. These are special messages handled by corresponding functions which for sure can have other parameter types. In fact, you should do it the way described before by creating your CString on the heap (calling new) and passing it as WPARAM to your function.

          W 1 Reply Last reply
          0
          • W willempipi

            Chris Meech wrote: I would suggest that you 'new' a CString in the thread proc can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this: void CPLCClientView::OnDataReceived(CString tekst) { AfxMessageBox(tekst); } The Visual C++ n00p

            C Offline
            C Offline
            Chris Meech
            wrote on last edited by
            #5

            No you can not pass parameters for user defined messages in that way. I would suggest that you thoroughly read this article[^]. It is one of the best on this site explaining multi-threading and how to deal with the many issues. Chris Meech "what makes CP different is the people and sense of community, things people will only discover if they join up and join in." Christian Graus Nov 14, 2002. "AAAAAAAAAHHHHHH!!!!! Those leaks are driving me crazy! How does one finds a memory leak in a garbage collected environment ??! Daniel Turini Nov. 2, 2002.

            1 Reply Last reply
            0
            • H HENDRIK R

              willempipi wrote: can't i send the CString with the Message toward the function (like OnPaint sends a point value with it's message) so that it would look a little like this: Since you're dealing with user defined messages, no. Sending your own message and handling it with ON_MESSAGE requires you to provide a function declarated like that you did before (with WPARAM and LPARAM as parameters). Only then the message is handled correctly. It's not comparable to messages like WM_PAINT. These are special messages handled by corresponding functions which for sure can have other parameter types. In fact, you should do it the way described before by creating your CString on the heap (calling new) and passing it as WPARAM to your function.

              W Offline
              W Offline
              willempipi
              wrote on last edited by
              #6

              Schlaubi wrote: creating your CString on the heap (calling new) how do you mean this precisly? like this: CString tekst = new CString; and, after i created the tekst, do i have to delete it?

              H 1 Reply Last reply
              0
              • W willempipi

                Schlaubi wrote: creating your CString on the heap (calling new) how do you mean this precisly? like this: CString tekst = new CString; and, after i created the tekst, do i have to delete it?

                H Offline
                H Offline
                HENDRIK R
                wrote on last edited by
                #7

                in your thread: CString *tekst = new CString("blablabla"); PostMessage(hWnd, WM_MYMESSAGE, (WPARAM)tekst, 0); in MyFunc: ... CString* pStr = (CString*)wParam; do_sth_with_string delete pStr;

                W 1 Reply Last reply
                0
                • H HENDRIK R

                  in your thread: CString *tekst = new CString("blablabla"); PostMessage(hWnd, WM_MYMESSAGE, (WPARAM)tekst, 0); in MyFunc: ... CString* pStr = (CString*)wParam; do_sth_with_string delete pStr;

                  W Offline
                  W Offline
                  willempipi
                  wrote on last edited by
                  #8

                  Thnx...:-D:-D:-D

                  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