接收服务中的Windows消息 [英] Receive Windows Messages in a Service

查看:271
本文介绍了接收服务中的Windows消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用VC ++编写了服务.我遵循了此处的教程.现在,我试图找出如何接收像DBT_DEVICEARRIVAL,DBT_DEVICEREMOVECOMPLETE,WM_COPYDATA等消息,就像具有顶层窗口的常规应用程序一样.在搜索时,我遇到了这篇MSDN文章

I have written a service in VC++. I followed the tutorial here. Now, I am trying to find out how to receive messages like DBT_DEVICEARRIVAL, DBT_DEVICEREMOVECOMPLETE, WM_COPYDATA etc., just like a regular application that has a top level window. When searching for it, I came across this MSDN article

在广播消息"部分的最后几段:

In the "Broadcasting Messages" section, in the final paragraphs:

应用程序通过其窗口过程接收消息 顶层视窗.消息不会发送到子窗口. 服务 可以通过窗口过程或其服务接收消息 控制处理程序.

Applications receive messages through the window procedure of their top-level windows. Messages are not sent to child windows. Services can receive messages through a window procedure or their service control handlers.

但是几乎找不到任何方法的例子.

But it is almost impossible to find any example of how to do it.

  1. 如何将WndProc与我的服务关联,以便它接收消息?

  1. How can I associate a WndProc with my service so that it receives messages?

或者,如何使我的服务控制处理程序函数接收Windows消息?我的服务控制处理程序只有一个DWORD参数,而没有WndProc的UINT,WPARAM,LPARAM等.

Or, how can I make my service control handler function to receive windows messages? My service control handler has only one DWORD parameter and not the UINT, WPARAM, LPARAM etc of a WndProc.

我已经阅读了1)使用隐藏窗口和2)仅消息窗口等内容,但是我认为我无法在服务中使用它们;不想.如果我能以MSDN提到的两种方式中的任何一种来完成它,那将是很高兴的.

I have read about 1) using a hidden window and 2) a message only window etc., but I don't think I can use them in a service; don't want to. It'd be happy if I can accomplish it in either of those two ways which MSDN mentions.

服务说明:

该服务将检测USB设备的插入并将一些文件复制到其中.它还必须跟踪对某些目录和文件的更改,以便知道要复制的目录和文件.

The service will detect USB device insertion and copy some files to it. It also has to keep track of changes to some directories and files so that it knows which ones to copy.

将来,此基本功能可能会扩展为包括其他内容.因此,我可能必须能够接收许多其他我现在不知道的Windows消息.

This basic functionality may be extended to include other things, in the future. So, I may have to be able to receive many other windows messages which I am not aware of now.

上面提到的示例消息只是从我习惯于开发常规Windows应用程序时获得的.在编写服务时,我了解它们是否不合适或不安全.

The example messages mentioned above are simply taken from what I am used to, when developing a regular windows app. I understand if they are not suitable or safe, when writing a service.

推荐答案

嗯,您只需创建一个普通的消息循环即可,就像编写Win32窗口应用程序的纯C实现一样,无需任何框架.

Uhm you simply create an ordinary message loop as you would if you wrote a pure C implementation of a Win32 windowed application - without any frameworks involved.

示例:

while(GetMessage(...)) ...

您可以使用PeekMessage GetMessage (请参阅链接的文档).但是后者更为常规,并将其从消息队列中删除.

You can either use PeekMessage or GetMessage (see the linked docs). But the latter is more conventional and removes it from the queue of messages.

即您甚至不需要窗户.每个线程都可以有一个消息循环.因此它将被阻塞,但仅限当前线程.您必须自己弄清楚如何将信息中继到需要此信息的其他线程.

I.e. you don't even need a window. Every thread can have a message loop. So it will be blocking, but only the current thread. You gotta figure out on your own how to relay the information to the other thread requiring it.

但是,您应该 IPC 服务技术(从MMF到管道,再到带有信号灯,互斥锁和事件的组合,有很多可用的方法.

But instead of working on compromising something that MS put up for you not to shoot yourself your lower body off, you should read about Shatter Attacks over on Wikipedia and use a proper IPC technique for a service (there are plenty available, from MMF to pipes to combinations with semaphores, mutexes and events).

如果您打算在用户桌面上接收具有特权上下文的窗口消息(无论如何应避免会话分离),那么这部分就很重要.

This part is relevant if you intend to receive window messages on a user desktop but with your privileged context (which session separation should prevent anyway).

这篇关于接收服务中的Windows消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆