Solved

PostMessage Parameters

Posted on 2006-11-08
2
988 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
2 Comments
 
LVL 49

Assisted Solution

by:DanRollins
DanRollins earned 250 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 250 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 116
Expand data scrubbing tool 13 32
difference between String.subString() and String.subSequence() 6 193
mapBully challenge 6 127
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now