Solved

PostMessage Parameters

Posted on 2006-11-08
2
1,000 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

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