All Categories


The Difference Between PostMessage And SendMessage In VC ++

One: the recent use of vc prepared UDP communication procedures, the message mechanism which was sorted. The message handling in vc is different from that in .net. Net has given us a good package, the use of agents can operate the way. In vc, to write their own message response function.

usb backup flash drive1, PostMessage only the message into the queue, regardless of whether other procedures are returned, and then continue to implement, this is an asynchronous message delivery function. And SendMessage must wait for other programs to process the message after the return, continue to perform, this is a synchronous message delivery function. Also, the return value of PostMessage indicates whether the PostMessage function is executed correctly; the return value of SendMessage represents the return value after the other program processes the message. This point we should all understand.

2, if the same thread, PostMessage send a message, the message first put the thread of the message queue, and then through the message loop Dispatch to the target window. SendMessage When sending a message, the system directly calls the target window's message handler and returns the result. SendMessage does not send messages in the same thread into the thread message queue. If in different threads. It is best to use PostThreadMessage instead of PostMessage, he works well. SendMessage sends a message to the message queue of the thread to which the target window belongs, and then sends the thread waiting for the message (in fact, he should still do some monitoring work, such as monitoring the QS_SENDMESSAGE flag) until the target window is processed and the result is returned and the message is sent Thread to continue running. This is the general situation of SendMessage, in fact, the process to be more complex. For example, backup usb when plugged in a thread that sends a message monitors a message with another window SendMessage, he calls the window handler (reentrant) and returns the processing result (which does not require support for GetMessage in the message loop).

3, msdn: If you send a message in the range below WM_USER to the asynchronous message functions (PostMessage, SendNotifyMessage, and SendMessageCallback), its message parameters can not include pointers. Otherwise, the operation will fail.

If the message code sent under WM_USER (non-custom message) and the message parameter with a pointer, then PostMessage, SendNotifyMessage, SendMessageCallback these asynchronous message sending function will call failed. It is best not to use PostMessage to send a message with a pointer parameter.

Two: PostThreadMessage
Function function: the function will be a message into (sent) to the specified thread of the message queue, do not wait for the thread to process the message to return.
Function Prototype: BOOL PostThreadMessage (DWORD idThread, UINT Msg, WPARAM wParam, LPARAM IParam);
IdThread: The thread identifier of the thread whose message will be sent. If the thread does not have a message queue, this function will fail. When the thread first calls a Win32 USER or GDI function, the system creates a message queue for the thread. For more information, see note.
Msg: Specifies the type of message to be sent.
WParam: Specifies additional message-specific information.
IParam: Specifies additional message-specific information.
Return Value: Returns a nonzero value if the function call succeeds. If the function call fails, the return value is zero. To get more error messages, call the GetLastError function. If idThread is not a valid thread identifier or a thread determined by idThread no message team
Column, GetLastError returns ERROR_INVALID_THREAD.
Note: The message will be sent to the thread must create a message queue, otherwise call PostThreadMessage will fail. Use one of the following methods to handle this situation:
Call PostThreadMessage. If it fails, call Sleep, and then call PostThreadMessage, repeatedly executed until PostThreadMessage succeeds.
Create an event object and create a thread. Before calling PostThreadMessage, the special event is set to be told by the function WaitForSingleObject. The message sent to the thread calls PeekMessage (£ msg, NULL, WM_USER, WM_USER, PM_NOREMOVE) to force the system to create a message queue. Set the event to indicate that the thread is ready to receive the sent message.
The message will be sent to the thread by calling GetMesssge or PeekMesssge to get the message. The hwnd member in the returned MSG structure is NULL.

Function: This function checks the thread message queue for a message and places the message (if it exists) in the specified structure.
Function Prototype: BOOL PeekMessage (LPMSG IpMsg, HWND hWnd, UINT wMSGfilterMin, UINT wMsgFilterMax, UINT wRemoveMsg);
LpMsg: MSG structure pointer to receive message information.
HWnd: the handle of the window whose messages were checked.
WMsgFilterMin: Specifies the first message in the range of messages to be checked.
WMsgFilterMax: Specifies the last message in the range of messages to be checked.
WRemoveMsg: Determines how the message is being processed. This parameter can take one of the following values:
PM_NOREMOVE: PeekMessage processing, the message is not removed from the queue.
PM_REMOVE: PeekMessage After processing, the message is removed from the queue.
PM_NOYIELD can be arbitrarily combined to PM_NOREMOVE or PM_REMOVE. This flag causes the system not to release threads that are waiting for the calling program to be idle.
By default, all types of messages are processed. If only certain messages are processed, specify one or more of the following values:
PM_QS_INPUT: Windows NT5.0 and Windows 98: Handles mouse and keyboard messages.
PM_QS_PAINT: Windows NT 5.0 and Windows 98: Handle Paint Messages.
PM_QS_POSTMESSAGE: Windows NT 5.0 and Windows 98: Handles all sent messages, including timers and hotkeys.

About the Author

Hello! My name is Tania. I am satisfied that I could unify to the entire world.
I live in Great Britain, backup usb when plugged in the south region. I dream to go to the various nations, to obtain acquainted with fascinating individuals.


No comments yet! Be the first:

Your Response


Most Viewed - All Categories