CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Aug 2004
    Posts
    91

    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

  2. #2
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    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.

  3. #3
    Join Date
    Aug 2004
    Posts
    91

    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

  4. #4
    Join Date
    Apr 2001
    Posts
    514

    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

  5. #5
    Join Date
    May 2009
    Posts
    1

    Unhappy 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

  6. #6
    Join Date
    Feb 2002
    Posts
    4,640

    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

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: How to send user defined structure using PostMessage?

    If you still want to rely on messages, you can explore WM_COPYDATA message.

  8. #8
    Join Date
    Apr 2001
    Posts
    514

    Re: How to send user defined structure using PostMessage?

    Quote Originally Posted by deveshpandey82 View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured