English 中文(简体)
TService不会处理消息。
原标题:
  • 时间:2008-12-10 15:39:53
  •  标签:

我已经创建了一个使用Windows消息系统的Windows服务。当我从调试器测试应用程序时,消息顺利通过,但当我安装它时,我的消息......

vladimir 1tuga

问题回答

服务通常不会接收窗口消息。它们可能根本没有窗口句柄。即使它们有,它们也在单独的桌面上运行。程序无法从一个桌面发送消息到另一个桌面,因此服务只能从另一个服务或由服务启动的程序接收消息。

在Windows Vista之前,您可以配置您的服务与桌面进行交互。这使得服务在已登录用户的同一桌面上运行,因此作为该用户运行的程序可以向您的服务发送消息。但是,Windows Vista隔离了服务;它们不能再与任何用户的桌面互动了。

有很多其他与服务进行通信的方式,它们包括命名管道、邮槽、内存映射文件、信号量、事件和套接字。

例如,通过套接字,您的服务可以在一个开放的端口上侦听,并且需要与其通信的程序可以连接到该端口。这可以打开远程管理的大门,但您也可以将服务限制为仅侦听本地连接。

以上所有内容都试图告诉你,你正在采取错误的方法。但还有一个问题在手边。你的程序在调试器中的行为方式与在调试器外的行为方式不同。如果它没有安装,你首先是如何调试服务的?你的服务正在运行哪个用户帐户?你的调试器?除调试器之外,你尝试过哪些不涉及调试器的调试技术(例如,writeln 到日志文件来跟踪程序的操作)?

当你说它“使用”Windows消息系统时,你的意思是什么?你是消费还是发送Windows消息?

如果您发送Windows消息,您需要确保操作正确。我建议编写一个消息循环以确保您的消息被正确分发。我还建议阅读有关消息循环及其工作原理的资料。

什么是消息循环 (点击标题进入此信息来源)

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
  1. The message loop calls GetMessage(), which looks in your message queue. If the message queue is empty your program basically stops and waits for one (it Blocks).
  2. When an event occures causing a message to be added to the queue (for example the system registers a mouse click) GetMessages() returns a positive value indicating there is a message to be processed, and that it has filled in the members of the MSG structure we passed it. It returns 0 if it hits WM_QUIT, and a negative value if an error occured.
  3. We take the message (in the Msg variable) and pass it to TranslateMessage(), this does a bit of additional processing, translating virtual key messages into character messages. This step is actually optional, but certain things won t work if it s not there.
  4. Once that s done we pass the message to DispatchMessage(). What DispatchMessage() does is take the message, checks which window it is for and then looks up the Window Procedure for the window. It then calls that procedure, sending as parameters the handle of the window, the message, and wParam and lParam.
  5. In your window procedure you check the message and it s parameters, and do whatever you want with them! If you aren t handling the specific message, you almost always call DefWindowProc() which will perform the default actions for you (which often means it does nothing).
  6. Once you have finished processing the message, your windows procedure returns, DispatchMessage() returns, and we go back to the beginning of the loop.

Thank you all for the answers, the issue was the operating system (vista), i tested the with my windows 2000 and everything works.

感谢你的灯,Rob。





相关问题
热门标签