|
-
September 13th, 2004, 10:47 PM
#1
How to send user defined structure using PostMessage?
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
-
September 13th, 2004, 11:07 PM
#2
Re: How to send user defined structure using PostMessage?
For Sending...
Dynamically allocate memory your strucuture first
Fill Your Structure
Pass your Strucuture
For Reading...
When recieve the message, read type cast the paramter in the respective pointer
read the data
delete the pointer
something like
Code:
struct DATA
{
int EventID;
CString ModuleName;
CString ThreatName;
} info;
...
// For Sending
info* ptr = new info;
ptr->EventID = Whatever;
ptr->ModuleName = Whatever;
ptr-> ThreatName = Whatever;
PostMessage(WM_MYMSG, reinterpret_cast<WPARAM> (ptr) , static_cast<LPARAM> 0);
// For Receiving
info* ptr = reinterpret_cast<info*> wParam; // I assume wParam as the argument name
info obj;
obj.EvenID = ptr->EvenID;
obj.ModuleName = ptr->ModuleName;
obj.ThreatName = ptr->ThreatName;
delete ptr;
// Now, you can use obj onward
btw, do typecast the argument while sending through PostMessage/SendMessage properly, as you didn't typecast LPARAM, and in the release mode, it will crash your application, but in debug it will run smooth.
Last edited by Ejaz; September 13th, 2004 at 11:15 PM.
-
September 14th, 2004, 12:13 AM
#3
Re: How to send user defined structure using PostMessage?
Hello,
Thanks for the help.
I allocated memory dynamically and I was able to post data correctly.
But I have one more problem:
1)Where do I delete the memory allocated to info ptr?
Can I call delete in THREAD 2 after displaying is done - delete ptr?
Can you please let me know where to delete this allocated memory?
Thanks
Madhavi
-
September 14th, 2004, 04:25 AM
#4
Re: How to send user defined structure using PostMessage?
Hi,
Assuming the data is no longer required by another thread then you are correct to delete the pointer in the second thread after it is done with it.
Cheers,
Lee
-
May 5th, 2009, 07:13 AM
#5
Re: How to send user defined structure using PostMessage?
I am also facing the same problem. I am trying to post a registered message from a DLL to another application and I want to send an user defined structure with it. I am allocating the memory for the structure dynamically and then, after filling in the values (one of which is a char array), I am passing it as the wParam. In the other application, I have registered the same message and, in the handler, I have type casted the wParam as the pointer to that structure, it shows the same address that I passed to it from DLL, but it does not show the values in the structure. Whats wrong, Please help me!
Dev
-
May 5th, 2009, 11:09 AM
#6
Re: How to send user defined structure using PostMessage?
You cannot send data from one process to another in this manner (only between threads of the same process). There are a number of ways to send data from one process to another (pipes; TCP/IP; file on disk; etc.). Look up Inter Process Communication.
Viggy
-
May 9th, 2009, 06:35 PM
#7
Re: How to send user defined structure using PostMessage?
If you still want to rely on messages, you can explore WM_COPYDATA message.
-
May 20th, 2009, 06:54 PM
#8
Re: How to send user defined structure using PostMessage?
 Originally Posted by deveshpandey82
I am allocating the memory for the structure dynamically and then, after filling in the values (one of which is a char array), I am passing it as the wParam. In the other application, I have registered the same message and, in the handler, I have type casted the wParam as the pointer to that structure, it shows the same address that I passed to it from DLL, but it does not show the values in the structure. Whats wrong, Please help me!
From what you say, you have the messaging part working ok but the problem is that you are trying to access memory which has been allocated, and is therefore owned, by the sending process. This isn't allowed, as the receiving process is not able to access the sending processes address space in the way you describe.
However, if both processes are running on the same machine, you might be able to modify your application to use Shared Memory without a huge amount of modification to the code you have already. You should be able to find examples of how to use Shared Memory for inter-process communication somewhere on this forum.
Assuming that what you are trying to do is being developed using the WIN32 API, you could try this post - http://www.codeguru.com/forum/showpo...2&postcount=17 - or have a look for more info on CreateFileMapping().
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|