TService 不会处理消息

发布于 2024-07-09 21:57:13 字数 113 浏览 15 评论 0原文

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

弗拉基米尔 14 分钟前询问 1图加

I have created a windows service that uses Windows Messaging System. When I test the app from the debugger the Messages go through nicely but when I install it my messag … asked 14 mins ago

vladimir
1tuga

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素手挽清风 2024-07-16 21:57:13

服务通常不接收窗口消息。 他们根本不一定有窗把手。 即使它们这样做,它们也会在单独的桌面上运行。 程序无法从一个桌面向另一个桌面发送消息,因此服务只能从另一个服务或从服务启动的程序接收消息。

在 Windows Vista 之前,您可以将服务配置为与桌面交互。 这使得该服务与登录用户在同一桌面上运行,因此以该用户身份运行的程序可以向服务的窗口发送消息。 不过,Windows Vista 隔离了服务; 他们无法再与任何用户的桌面交互。

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

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

以上所有内容都试图告诉您,您采取了错误的方法。 但还有眼前的问题。 您的程序在调试器中以一种方式运行,而在调试器之外以另一种方式运行。 如果服务尚未安装,您首先如何调试该服务? 您的服务以什么用户帐户运行? 你的调试器? 您尝试过哪些不涉及调试器的调试技术(例如 writeln 到日志文件中以跟踪程序的操作)?

Services don't generally receive window messages. They don't necessarily have window handles at all. Even if they do, they run in a separate desktop. Programs cannot send messages from one desktop to another, so a service can only receive messages from another service, or from a program started by a service.

Before Windows Vista, you could have configured your service to interact with the desktop. That makes the service run on the same desktop as a logged-in user, so a program running as that user could send messages to your service's windows. Windows Vista isolates services, though; they can't interact with any user's desktop anymore.

There are many other ways to communicate with services. They include named pipes, mailslots, memory-mapped files, semaphores, events, and sockets.

With a socket, for instance, your service could listen on an open port, and programs that need to communicate with it could connect to that port. This could open the door to remote administration, but you can also restrict the service to listen only for local connections.

All the above is trying to tell you that you're taking the wrong approach. But there's also the matter of the problem at hand. Your program behaves one way in the debugger and another way outside it. How are you debugging the service in the first place, if it's not installed? What user account is your service running as? Your debugger? What debugging techniques have you tried that don't involve the debugger (e.g. writeln to a log file to track your program's actions)?

开始看清了 2024-07-16 21:57:13

当您说它“使用”Windows 消息系统时,您是什么意思? 您正在使用或发送 Windows 消息吗?

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

什么是消息循环(点击标题即可转到此信息的来源)

while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}
  1. 消息循环调用 GetMessage(),
    它会在您的消息队列中查找。
    如果消息队列为空
    程序基本上停止并等待
    其一(它会阻止)。
  2. 当事件发生导致
    要添加到队列的消息
    (例如系统注册了一个
    鼠标单击)GetMessages() 返回一个
    正值表明存在
    要处理的消息,并且它
    已填写MSG成员
    结构我们通过了它。 它返回 0
    如果它击中 WM_QUIT,并且为负数
    发生错误时的值。
  3. 我们将消息(在 Msg
    变量)并将其传递给
    TranslateMessage(),这个有点作用
    额外的处理,
    翻译虚拟按键消息
    到字符消息中。 这一步
    实际上是可选的,但是是确定的
    如果它不存在,事情就无法进行。
  4. 完成后我们传递消息
    到 DispatchMessage()。 什么
    DispatchMessage() 的作用是
    消息,检查它是哪个窗口
    for 然后查找窗口
    窗口的程序。 那么它
    调用该过程,发送为
    参数窗口的句柄,
    消息、wParam 和 lParam。
  5. 在你的窗口过程中你检查
    消息及其参数,以及
    对他们做任何你想做的事! 如果
    你没有处理具体的
    消息,你几乎总是打电话
    DefWindowProc() 将执行
    您的默认操作(其中
    通常意味着它什么都不做)。
  6. 处理完成后
    消息,你的Windows程序
    返回,DispatchMessage() 返回,
    我们回到开头
    循环。

What do you mean when you say it "uses" Windows Messaging System? Are you consuming or sending Windows Messages?

If you send a Windows message, you need ensure you are doing it correctly. I'd suggest writing a message loop to ensure your messages are being dispatched properly. I'd also suggest reading up on message loops and how they work.

What is a Message Loop (click the title to be taken to the source of this info)

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.
终陌 2024-07-16 21:57:13

谢谢大家的解答,
问题是操作系统(vista),我用我的 windows 2000 进行了测试,一切正常。

感谢罗布的光。

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

thanks for the light Rob.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文