Solved

How to send user defined structure using PostMessage?

Posted on 2004-09-13
5
626 Views
Last Modified: 2013-11-20
Hello,

I am trying to send a structure using PostThread message.
My structure is of the form:
typedef DATA
{
int EventID;
CString ModuleName;
CString ThreatName;
} info;

info info_data;
PostMessage(WM_MYMSG, (info_data&)wParam,NULL);

In the recieving thread I tried this way:
info_data = (info_data&)wParam; // I am getting runtime error here.

Can anyone please help me to pass a user defined structure using PotMessage?

Thanks
Madhavi
0
Comment
Question by:j_madhavi
5 Comments
 
LVL 8

Expert Comment

by:VinExpert
Comment Utility
Hi,

The way u r trying out is fine... but u need to change ur postmessage as below:

info info_data;
/* Fill in values in info_data - optional */
PostMessage(WM_MYMSG, (info_data&)info_data, NULL);

Now try to get it back.

Try it out.
VinExpert
0
 

Author Comment

by:j_madhavi
Comment Utility
Hi,

Thanks for the help.
 When I tried this way,
typedef struct LOG_DATA
{
      CString timeStamp;
      int SequenceNo;
      long EventId;
      int ThreatID;
      CString ModuleName;
      int ModuleId;
      CString ErrorInfo;
      
}LOG_VIEW_DATA;
LOG_VIEW_DATA  is my structure
LOG_VIEW_DATA data = (LOG_VIEW_DATA)*aIterator;//here the structure gets filled
AfxGetMainWnd( )->PostMessage(WM_LOGMSG,  (LOG_VIEW_DATA&)data , 0) ;
I got an error msg saying
error C2664: 'CWnd::PostMessageA' : cannot convert parameter 2 from 'LOG_VIEW_DATA' to 'WPARAM'

Can you please let me know how to proceed?

Thanks
madhavi
0
 
LVL 3

Accepted Solution

by:
double_axe earned 30 total points
Comment Utility
Hi
1. PostMessage returns immediately after call. And variables on a stack can be destroyed when message reach the client. So you should create your variables on a heap:
LOG_VIEW_DATA *info_data;
info_data = new LOG_VIEW_DATA;
PostMessage(WM_MYMSG, (WPARAM)info_data,NULL);
and in the reciever
info_data = (LOG_VIEW_DATA*)wParam;
....
delete info_data;
2. This method will not work between different processes. For different processes you have to call ::SendMessage(WM_COPYDATA,.....);
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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 tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 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

6 Experts available now in Live!

Get 1:1 Help Now