none
How do you pass byte array to thread parent using PostMessage? RRS feed

  • Question

  • I'm porting a utility written Visual Studio C++ 2003 to Visual Studio C# 2017.

    A thread is created to handle communication processing. It issues PostMessage calls to send data to its parent.

    ::PostMessage(m_hWnd, WM_USER_BOOTLOADER_RESP_OK, (WPARAM)cmd, (LPARAM)&RxData[1] );
    ::PostMessage(m_hWnd, WM_USER_BOOTLOADER_RESP_OK, (WPARAM)cmd, (LPARAM)&RxData[1] );  

    The parent registers it's callback functions for a user defined event using ON_MESSAGE().

    ON_MESSAGE(WM_USER_BOOTLOADER_RESP_OK, OnReceiveResponse)
    ON_MESSAGE(WM_USER_BOOTLOADER_NO_RESP, OnTransmitFailure)

    The LPARAM being passed is an address of an element in the array which the receiving function casts to a structure pointer to access the data.

    I haven't been able to find an example using C# that handles passing a reference to a byte array and have the receiving function access it.

    Any clue on how to do this in C#?

    Wednesday, November 7, 2018 9:20 PM

Answers

  • In the original project used PostMessage in the child thread to send data to the parent form. When the parent form received the message it updated button states on the form.

    I've created an event that the parent form subscribes to. The child thread invokes the parents event handler passing the arguments needed. (I define a class derived from EventArgs for the arguments)

    The parent event handler however, cannot access the buttons on the form.

    To handle updating the buttons I added a timer to the parent that periodically checks some flags and updates the buttons accordingly.

    If there's a way to access objects on the parent form from the child thread it would be nice to know.


    • Edited by OneDogProd Friday, November 9, 2018 4:22 PM
    • Marked as answer by OneDogProd Monday, November 12, 2018 5:56 PM
    Friday, November 9, 2018 4:19 PM

All replies

  • > Any clue on how to do this in C#?

    The purpose of PostMessage is to have processing done in the main thread.  You can use the Invoke method of your forms to force a function to be called in the main thread.

    PostMessage exists outside of the .NET runtime.  It doesn't understand managed objects.  It's possible to allocate an unmanaged array, copy your data into it, and pass that array.  You'll have to undo it at the other end, where you handle the messages.  In general, it's probably easier to redesign it to use C# features.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Wednesday, November 7, 2018 9:40 PM
  • Hi OneDogProd,

    Thank you for posting here.

    For your question, try to call PostMessage function using DllImport in C# directly.

    Here is a example in MSDN for reference.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/021a3294-87fb-4c9c-9b09-2e49d3653eda/postmessages-in-to-different-app-using-c?forum=csharpgeneral

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, November 8, 2018 1:55 AM
    Moderator
  • I've looked at this but it only references the sender's side.

    What was done on the receiver's side to catch the message?

    Thursday, November 8, 2018 2:45 PM
  • Haven't looked at the Invoke method. I'll take a look.


    Thursday, November 8, 2018 3:01 PM
  • If the new application consists of forms and child controls, then, instead of messages, maybe you can simply call some functions of the parent form. You can also consider delegates and evens. Instead of byte arrays, use classes and structures.


    Thursday, November 8, 2018 5:00 PM
  • In the original project used PostMessage in the child thread to send data to the parent form. When the parent form received the message it updated button states on the form.

    I've created an event that the parent form subscribes to. The child thread invokes the parents event handler passing the arguments needed. (I define a class derived from EventArgs for the arguments)

    The parent event handler however, cannot access the buttons on the form.

    To handle updating the buttons I added a timer to the parent that periodically checks some flags and updates the buttons accordingly.

    If there's a way to access objects on the parent form from the child thread it would be nice to know.


    • Edited by OneDogProd Friday, November 9, 2018 4:22 PM
    • Marked as answer by OneDogProd Monday, November 12, 2018 5:56 PM
    Friday, November 9, 2018 4:19 PM