2

How to send struct or pointer using SendMessage or PostMessage function?

2 Answers 2

9

Here is simple example:

typedesf struct tagMY_STRUCT{
    unsigned int a;
    unsigned int b;
    unsigned int c;
}MY_STRUCT;

//Some code which sends message
MY_STRUCT *pStruct = new MY_STRUCT;

pStruct->a = 5;
pStruct->b = 4;
pStruct->c = 1;

SendMessage(hWnd, WM_USER + 1, 0, (LPARAM)pStruct);

//WndProc

case (WM_USER + 1) :
{
    MY_STRUCT *pStruct = (MY_STRUCT*)lParam);
    if(pStruct)
    {
        int a = pStruct->a;

        delete pStruct;
    }

}
break;
  • Don't ever try to do it between processes.Study IPC basics first.

  • Don't forget to release pointer to struct once not needed using delete.

UPDATE

As Remy Lebeau mentioned in comment, you can also allocate struct on stack instead on heap using new/malloc, because SendMessage block thread until it's processed in WndProc.This does not apply to PostMessage which add message to window's message queue and return immediately, so it requires heap block.

However, if you plan to pass more complex data struct I recommend heap allocation instead of stack.

Sign up to request clarification or add additional context in comments.

13 Comments

SendMessage() is synchronous, so you don't need to use new to allocate the struct dynamically. You can allocate it on the stack instead and just pass its pointer to SendMessage(). The receiver does not need to free it. PostMessage(), on the other hand, is asynchronous, so the new/delete approach is necessary.
@RemyLebeau : +1, You are correct, but imagine that you need to pass much bigger struct/data block, then this is more convenient way.However, you have right.
Even with larger data, using SendMessage(), the receiver should not free the memory, the sender should after SendMessage() exits.
Disagree, several win32 API do it that way.API allocates, caller free.
@rkosegi - yes, Remy does have a point that SendMessage() type synchronous inter-thread comms will work OK providing that the receiver doesn't keep the object pointer for later or communicate it off to yet another thread. Unfortunately, doing exactly that is very common and I think that it is unreasonable and unsafe for a WndProc to get an object in a custom message that vaporises upon return - the WndProc, and function/s called from it, cannot easily tell whether an object has been posted or sent & so, IMHO, it's safer to not use stack-based objects for any inter-thread comms at all, ever.
|
1

Send/Post it to where?

In general, an in-process communication by casting a pointer to LParam or WParam at one end and casting it back at the other is fairly straightforward. Don't create objects/structs/whatever on local stacks, especially when PostMessaging - use dynamic allocation with new(). Consider the object lifetime - if the receiver does not need to keep a PostMessaged object or pass it on, it should delete() it.

For inter-process comms, Google for 'WM_COPYDATA'.

2 Comments

I have struct with 3 unsigned int values. I need to send it to WndProc.
Then it's fairly easy - @rkosegi has posted an example. As he/she hints at, inter-process comms is a bit more complex - you must have a handle to the target window, use the WM_COPYDATA message number and SendMessage with a special structure to copy your data into.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.