Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SendMessage and PostMessage which one is better to use?

searching the web for the difference between these two function calls, I've found that:

The difference between these two API calls is the way that they return control to the calling application. With SendMessage control is not returned to the calling application until the window that the message was sent to has completed processing the sent message, however with PostMessage control is returned to the calling application immediately, regardless of weather or not the sent message has been processes.

postMessage: Sends a message in the message queue associated with the thread and returns without waiting for the thread to process that messaage.

SendMessage: calls the window procedure for the specified window and does not return until the window procedure has processed the message.

PostMessage is a Asynchronous function where as SendMessage is a synchronous function.

Now the question is for my application ( which needs high performance ) which function is better to be used?

Note that I'm using MFC in visual studio 2010 and for this code:

CWnd *pParentOfClass = CWnd::GetParent();
pParentOfClass ->  

I just have these functions for sending messsages:

  • PostMessageW

  • SendMessage

  • SendMessageW

More questions:
Can you tell me the difference between SendMessage and SendMessageW?

like image 699
Sepideh Abadpour Avatar asked Aug 30 '13 07:08

Sepideh Abadpour


People also ask

What is postMessage used for?

postMessage() is used by the application to allow cross-origin communication between different window objects, e.g. between a page and a pop-up that it spawned or between a page and an iframe embedded within it.

Is window postMessage secure?

Security-Reviewing Uses of postMessage()postMessage is generally considered very secure as long as the programmer is careful to check the origin and source of an arriving message. Acting on a message without verifying its source opens a vector for cross-site scripting attacks.

What is SendMessage?

Sends the specified message to a window or windows. The SendMessage function calls the window procedure for the specified window and does not return until the window procedure has processed the message.

What is Targetorigin postMessage?

postMessage method allows different windows or iframes to communicate directly, even if they were loaded from different origins, circumventing the usual same-origin policy. The sender of the message can restrict the origin of the receiver by specifying a target origin.


1 Answers

You actually already answered your own question by describing SendMessage and PostMessage.

SendMessage: Sends a message and waits until the procedure which is responsible for the message finishes and returns.

PostMessage: Sends a message to the message queue and returns immediately. But you don't know when that message is actually being processed. Therefore, if you should be expecting an answer from that processed message, you will most likely get it through a message as well.

It really depends which one to use, but the end results are pretty much the same, it's just about timing. Also, PostMessage is especially useful in multi-threaded applications, which allows you to safely communicate between threads via their created windows.

PostMessage or SendMessage that end with A or W are just indicators how strings will be interpreted, i.e. single or multibyte, respectively. The ones without the A or W ending are preprocessor macros and will delegate to whatever you're application is set up to.

like image 150
bcause Avatar answered Oct 16 '22 09:10

bcause