1.PostMessage
This function places a message in the message queue associated with the thread that created the specified window and then returns without waiting for the thread to process the message. Messages in a message queue are retrieved by calls to the GetMessage or PeekMessage function.
这个函数将一个消息放入窗口的消息队列,然后直接返回,并不等待对应的窗口处理消息。消息队列中的消息是通过调用Windows的GetMessage或PeekMessage函数来获得的。
可以通过Windows的PostMessage函数来访问其它应用程序。
prototype:
BOOL PostMessage(
HWND hWnd, //可以省略
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
Parameters
hWnd
[in] Handle to the window whose window procedure is to receive the message. Two values have special meanings.
Msg
[in] Specifies the message to be posted.
wParam
[in] Specifies additional message-specific information.
lParam
[in] Specifies additional message-specific information.
Return Values
Nonzero indicates success.
Zero indicates failure.
To get extended error information, call GetLastError.
注意:只能使用wParam或lParam来传递全局指针,否则在堆/栈上的指针很容易被收回而成为非法指针。
2.SendMessage
This function sends the specified message to a window or windows. SendMessage calls the window procedure for the specified window and does not return until the window procedure has processed the message. The PostMessage function, in contrast, posts a message to a thread’s message queue and returns immediately.
这个函数向窗口发送指定的消息。SendMessage成员函数直接调用窗口过程并在窗口过程处理了消息以后才返回。这与PostMessage成员函数形成对比,该函数将消息放入窗口的消息队列并立即返回。
prototype:
LRESULT SendMessage(
HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
);
Parameters
hWnd
[in] Handle to the window whose window procedure will receive the message.
If this parameter is HWND_BROADCAST, the message is sent to all top-level windows in the system,
including disabled or invisible unowned windows, overlapped windows, and pop-up windows;
but the message is not sent to child windows.
Msg
[in] Specifies the message to be sent.
wParam
[in] Specifies additional message-specific information.
lParam
[in] Specifies additional message-specific information.
Return Values
The return value specifies the result of the message processing and depends on the message sent.
3.另外说一下,在多线程中经常会遇到在某一线程中动态申请new了某空间并将返回值赋给指针,但该内存空间还需要使用,需要在另一线程中释放delete该内存空间的情况。这时候new和delete就不能保证在一块出现了,但堆上申请的空间仍然是需要释放的。这时候就需要保证,在delete p时,p指向的空间地址必须是new出来的堆上的空间地址,否则在delete时就会出错,还可能造成内存泄漏。