How to send user defined structure using PostMessage?

Posted on 2004-09-13
Last Modified: 2013-11-20

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?

Question by:j_madhavi

Expert Comment

ID: 12051192

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.

Author Comment

ID: 12051958

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  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?


Accepted Solution

double_axe earned 30 total points
ID: 12053001
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,.....);

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
iSeries FTP Exit Program 8 124
Generic progress indicator 6 113
matchUp  challenge 6 53
wordappend challenge 8 149
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

867 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

15 Experts available now in Live!

Get 1:1 Help Now