Solved

PostMessage Parameters

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
not able to insert into temp table 68 157
Problem to open Excel file 15 99
Way to decrease size of apk file 9 46
"Black Box" Testing of Control System Software 2 26
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

861 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

24 Experts available now in Live!

Get 1:1 Help Now