Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PostMessage Parameters

Posted on 2006-11-08
2
Medium Priority
?
1,007 Views
Last Modified: 2013-11-20

When I post a message from a thread back to the process which started the thread, can I pass a pointer to a text message as one of the parameters?

::PostMessage(*pParent, WM_THREADMESSAGE, (pointer to a CString or a char buffer), 0);

Please include a code fragment (for the code at both ends)
Thanks
Phil
0
Comment
Question by:BeerFizz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 1000 total points
ID: 17903462
Sure.  Think of the Win32 API -- it defines lots of messages that pass text around.

The only real issues are 1) data persistance and 2) possible collision/reentrance (especially with multi-threaded apps).

Simple scenario:
    static szMsg[100];

    //---------------- sender:
    strcpy( szMsg, "Hi there!");
    ::PostMessage(*pParent, WM_MyUserMsg, (WPARAM)szMsg, 0);

    //---------------- reciever:
    CMyWnd::OnWndMsg( UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult )
    {
          if ( message == WM_MyUserMsg ) {
                CString sMsg=(LPCSTR)wParam;
                MessageBox( sMsg );
          }
         *pResult= 1;
    }

=-=-=-=-=-=-=--=
Note that szMsg (the message being sent is a static variable == that is to make sure that it does not disappear by the time the receiver gets the message (which would happen with an auto char or CString variable).

Collission and re-entrancy can become a problem in a multi-threading scenario.  For instance, if the sender function gets called twice before the receiver gets a chance to pick up the message, then the first text string will be lost.  One way to handle it is to allocate a new buffer when you create the text message to send.

    pszMsg= new char[100];

The catch is that the sender cannot deallocate it.  The reciever must be responsible for that.

If you need more help. just ask.

-- Dan
0
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 17903484
Within the same process, a pointer is OK as long as you ensure that it remains valid, i.e. the variable it points to does not go out of scope which can be done by allocating the memory on the heap, e.g.

// sender
char* buf = new char[255];

::PostMessage(*pParent, WM_THREADMESSAGE, buf, 0);

// receiver

// .h
class CMyParent : public CWnd
{

//...

BEGIN_MESSAGE_MAP( CMyParent, CWnd )
   //{{AFX_MSG_MAP( CMyParent )
    ON_MESSAGE(WM_THREADMESSAGE, OnThreadMessage)
    //...
   //}}AFX_MSG_MAP
END_MESSAGE_MAP( )

afx_msg LRESULT OnThreadMessage(WPARAM wp, LPARAMlp);

//...
};

//.cpp
afx_msg LRESULT CMyParent::OnThreadMessage(WPARAM wp, LPARAM lp)
{

char* buf = (char*) wp;

// do something with it

delete [] buf;

return 0;
}

//...



0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question