Solved

PostMessage Parameters

Posted on 2006-11-08
2
984 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

19 Experts available now in Live!

Get 1:1 Help Now