windows核心编程|QQ群的一次聊天记录

上次写了 SDK中的消息机制 后有位网友给我留言说
留言于下、、
看了文章之后,写了一个CloseAll的程序:
//------------------------
#include
int main()
{
PostMessage(HWND_BROADCAST,WM_QUIT,0,0);
return 0;
}
//---------------------------
连explore都kill掉了
不过用SendMessage的效果好像不太一样,不知是什么原因

我调试了一下的确是这样的
于是我在QQ群里问了一下,但是还是没有得到满意得答复,不过收获也不小
现在我得问题还是这样的为什么postmessage和sendmessage会产生不一样的效果???????、
那位高手帮我回答一下 偶感激不尽!!!!!
下面就是聊天记录::::::::::::::::::::::::::::::;
:::::::::::::::::::::::::::::::::::::
这家伙很懒(76359121) 12:21:03
Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.
The system only does marshalling for system messages (those in the range 0 to WM_USER). To send other messages (those above WM_USER) to another process, you must do custom marshalling.

这家伙很懒(76359121) 12:21:21
谁帮我翻译一下
这家伙很懒(76359121) 12:26:34
帅哥门就命啊
谁会帮帮忙撒
超文黄(13859915) 12:21:01
在应用程序间通信中,需要使用HWND_BROADCAST通信的应用程序应该用RegisterWindowMessage 函数去获取一个唯一的消息。

这家伙很懒(76359121) 12:29:07
还是有点不明白
这家伙很懒(76359121) 12:30:20
inter-application communication.
--》交互程序通信
超文黄(13859915) 12:24:41
The system only does marshalling for system messages (those in the range 0 to WM_USER). To send other messages (those above WM_USER) to another process, you must do custom marshalling.
系统只对那些值介于0和WM_USER之间的系统消息作处理。如果需要对其他进程发送其他消息(值大于WM_USER的),你就必须自己做处理
超文黄(13859915) 12:25:23
inter-application communication.
--》交互程序通信
就是两个系统中不同应用进程
这家伙很懒(76359121) 12:32:14
就是不同进程间通信?
这家伙很懒(76359121) 12:34:23
那sendmessage和postmessage有什么区别呢
超文黄(13859915) 12:28:45
是阿
比如进程1如果要向进程2发送一个wm_close消息的话,可以直接用sendmessage发送,系统自动会做处理
但是如果你要自定义一个消息的话,你就必须自己声明消息,在进程接收进程中指明接受处理函数
超文黄(13859915) 12:29:27
The SendMessage member function calls the window procedure directly and does not return until that window procedure has processed the message. This is in contrast to the PostMessage member function, which places the message into the window's message queue and returns immediately.
这家伙很懒(76359121) 12:36:49
这个return到底指返回什么东西
超文黄(13859915) 12:30:50
sendmessage发送消息到窗口之后,直到窗口进程处理完毕才返回;postmessage是消息发送到了之后就直接返回
超文黄(13859915) 12:31:21
postmessage只负责发送到消息队列就返回
这家伙很懒(76359121) 12:38:06
sendmessage是不发送到消息队列
是吗?
兰陵笑笑生(64894354) 12:31:59
不是
这家伙很懒(76359121) 12:38:34
直接返回和处理完成后返回有什么区别呢?
兰陵笑笑生(64894354) 12:33:36
处理完成后返回,调用线程将挂起
超文黄(13859915) 12:35:49
打个比方
有人在不停的洗盘子
sendmessage是把盘子送过来,排队,看到别人洗完了他的盘子再走
postmessage就是把盘子丢在那就走
明白?
这家伙很懒(76359121) 12:42:41
这个明白
但是结果不都是一样吗
这家伙很懒(76359121) 12:43:15
也就是在什么时候要注意区分二哥函数的用法呢?
这家伙很懒(76359121) 12:43:28
二哥-》二个
兰陵笑笑生(64894354) 12:37:39
postmessage一般用于同一线程内的消息投递
这家伙很懒(76359121) 12:44:26
#include
int main()
{
PostMessage(HWND_BROADCAST,WM_QUIT,0,0);
return 0;
}
这家伙很懒(76359121) 12:45:20
执行结果是explore被关了
超文黄(13859915) 12:39:22
The Windows PostMessage function can be used to access another application.
兰陵笑笑生(64894354) 12:39:47
当然可以
这家伙很懒(76359121) 12:46:34
我来看看sendmessage是什么效果
这家伙很懒(76359121) 12:47:47
sendmessage没有气作用啊
这家伙很懒(76359121) 12:49:07
这是什么原因??
超文黄(13859915) 12:43:20
The Windows PostMessage function can be used to access another application.
是不是这个原因?

这家伙很懒(76359121) 12:50:07
难道sendmessage不能吗?
这家伙很懒(76359121) 12:51:31
那下面的那个HWND_BROADCAST是不是用于线程间通信的?
Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.
这家伙很懒(76359121) 12:52:58
线程-进程内部
超文黄(13859915) 12:48:00
这个你是在哪看到的阿?
这家伙很懒(76359121) 12:54:47
在我blog上面有人留了这个问题
这家伙很懒(76359121) 12:55:06
http://blog.csdn.net/navyblue1982/archive/2005/03/24/328392.aspx
超文黄(13859915) 12:50:50
PostMessage和SendMessage有什么区别?
在控制别的应用程序的时候,经常需要等待直到某个功能结束,例如:
打开一个窗口-->等待直到窗口结束
这个时候就可以用到SendMessage
如果在打开这个窗口后仍然需要对该窗口的界面进行设置,比如Edit的value等等,比如:
打开一个窗口-->控制窗口的control的属性
这个时候就需要PostMessage
使用一个钩子程序截获消息后,使用SendMessage把消息发送到主处理程序进行处理,但是在主处理程序还没有完成任务的时候,被设置钩子的程序进入了停止的状态,不可以处理
WM_PAINT,
WM_MOVE,
.......等的基本信息,
必须要等SendMessage发送出的消息完成后,才能继续运行,整个界面一片空白,把钩子消息设置成PostMessage的发送消息形式后,问题解决!
我查了MSDN对这两个API的定义,
PostMessage只是把消息放入队列,不管其他程序是否处理都返回,然后继续执行;
而SendMessage必须等待其他程序处理消息后才返回,继续执行。
PostMessage的返回值表示PostMessage函数执行是否正确;
而SendMessage的返回值表示其他程序处理消息后的返回值。
使用这两个发送消息函数的最重要的是要看你的程序是否要对消息的滞后性关注否,PostMessage会造成消息的滞后性,而SendMessage则不会,但如果SendMessage消息处理失败,则会造成程序停止!

超文黄(13859915) 12:53:02
The SendMessage member function calls the window procedure directly and does not return until that window procedure has processed the message. This is in contrast to the PostMessage member function, which places the message into the window's message queue and returns immediately.
这家伙很懒(76359121) 13:00:10

hWnd: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.

这家伙很懒(76359121) 13:01:31
在控制别的应用程序的时候,经常需要等待直到某个功能结束
这句怎么理解
超文黄(13859915) 12:55:15
好好理解一下这段话
sendmessage是直接调用窗口过程的,那么也就是说它可以让它的消息直接得到处理,是不是意味着这个消息有消息优先权呢?
postmessage只是把消息投放到消息队列就返回,具体还要等消息循环把消息通过Dispathmessage挨个发送给窗口过程
这家伙很懒(76359121) 13:03:54
#include
int main()
{
PostMessage(HWND_BROADCAST,WM_QUIT,0,0);
/ ndMessage(HWND_BROADCAST,WM_QUIT,0,0);
return 0;
}
为什么会有区别呢
这家伙很懒(76359121) 13:04:26
/ ndMessage(HWND_BROADCAST,WM_QUIT,0,0); -------》SendMessage(HWND_BROADCAST,WM_QUIT,0,0);
夜→风(125676885) 12:58:16
晕.还不如说sendmessage是同步的.postmessage是异步的呢

这家伙很懒(76359121) 13:06:34


这家伙很懒(76359121) 13:08:49
图片你们能看见吗?
兰陵笑笑生(64894354) 13:03:01
候捷书里面的图吧
这家伙很懒(76359121) 13:09:22

兰陵笑笑生(64894354) 13:03:51
advanced windows nt这本书里面好像有一个图比这复杂得多
兰陵笑笑生(64894354) 13:04:24
真正的消息流程比这复杂的
这家伙很懒(76359121) 13:10:46
上面的意思是不是
用postmessage是其他窗口发送到制定窗口的消息队列
然后用sendmessage取出消息队列中的消息取执行
这家伙很懒(76359121) 13:11:25
也就是sendmessage不能进行进程间通信
兰陵笑笑生(64894354) 13:05:25
sendmessage是不经过消息队列的
这家伙很懒(76359121) 13:11:42
各位看看我理解的有问题吗??
超文黄(13859915) 13:05:56
你那个程序太黑了
把我所有有窗口的进程全杀了
这家伙很懒(76359121) 13:13:06
但是换成了
SendMessage(HWND_BROADCAST,WM_QUIT,0,0);
就没有效果了
搞不明白为什么有区别

超文黄(13859915) 13:07:33
SendMessage应该调用窗口句柄才能发送
超文黄(13859915) 13:08:12
postmessage只是将消息发送到消息队列
前面你那个消息是发送到所有top-level的程序把
这家伙很懒(76359121) 13:14:41
但是MSDN上明明说明了
Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.

超文黄(13859915) 13:09:23
http://ce.szpt.edu.cn/longen/VCVB(A)/SendvsPostMsg().htm
看看这个描述
超文黄(13859915) 13:10:29
SendMessage你把指明程序窗体,那么就是给当前窗体发送了:)
这家伙很懒(76359121) 13:20:16
既然指定给了一个窗口
那么还要这个HWND_BROADCAST干什么?
这家伙很懒(76359121) 13:22:17
那么广播在sendmessage是不是就没有必要了?
超文黄(13859915) 13:17:21
搞错了:)
HWND_BROADCAST就是指明发送到top-level的窗体
但是为什么sendmessage不行久不知道了
这家伙很懒(76359121) 13:24:26
【windows核心编程|QQ群的一次聊天记录】

超文黄(13859915) 13:18:26
看不到你的图:)
这家伙很懒(76359121) 13:25:01
就是你刚才那个网上的
兰陵笑笑生(64894354) 13:19:31
每个线程都有自己的消息队列吧
这家伙很懒(76359121) 13:26:10
进程吧?

这家伙很懒(76359121) 13:26:17

自由鸟(303214605) 13:20:49
<<中断>>
请问"这家伙很懒"你的图是那本书上的呀,我觉得讲得很清楚.我想深入了解一下?
这家伙很懒(76359121) 13:27:23
http://ce.szpt.edu.cn/longen/VCVB(A)/SendvsPostMsg().htm
自由鸟(303214605) 13:23:28
多谢
这家伙很懒(76359121) 13:30:35
msdn上对sendmessage是这样说明的
Applications that need to communicate using HWND_BROADCAST should use the RegisterWindowMessage function to obtain a unique message for inter-application communication.

这家伙很懒(76359121) 13:31:20
inter-application communication可不可以理解为 进程间通信
超文黄(13859915) 13:26:46
应该可以把
不过application和process还是有点不一样吧
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

    推荐阅读