如何检测 Windows 已完成添加/删除存储设备(不是作为驱动器号,仅作为设备)
我有一些装有简化 Windows 的计算机,其中添加/删除的存储设备不会自动获取驱动器号。 所以我必须找到一种方法来检测添加/删除的内容。 一种方法(我知道)是使用 WM_DEVICECHANGE 消息。 在这些计算机上,消息仅使用 WParam 7 和 LParam 0 发送到 Windows。 因此,当我的应用程序收到此消息时,我可以运行我的检测代码。 问题是设备需要一些时间才能被完全识别;与此同时,我的应用程序受到 WM_DEVICECHANGE 消息“齐射”的“轰炸”,检测代码甚至启动了 6..7 次。 为了解决这个问题,我可以添加一个由新消息重新初始化的计时器;如果一段时间(1..2 秒)后,应用程序没有收到消息,则启动检测代码。 但我百分百不喜欢这种方法,因为它没有考虑其他因素,例如:计算机的速度、其他降低 Windows 速度的程序、不同的存储设备类型等。事实上,检测代码在延迟有点烦人。
有人知道更专业的解决方案吗...?
感谢您的帮助。
I have some computers with a simplified Windows in which storage devices that are added/removed don't get automatically drive letters.
So I have to find a way to detect that are added/removed.
One way (that I know) is with WM_DEVICECHANGE message.
On those computers the message is sent to windows only with WParam 7 and LParam 0.
So I could run my detection code when this message is received by my application.
The problem is that it takes some time for the device to be completely recognized; meantime my application is "bombarded" with a "salvo" of WM_DEVICECHANGE messages and the detection code is started even 6..7 times.
To solve I could add a timer who is reinitialized by a new message; if, after some time (1..2 sec), the application is not receiving the message, the detection code is started.
But I don't like this approach 100% because it doesn't take into consideration other factors as: the speed of the computer, other programs that slow down Windows, different storage device types etc. And the fact that the detection code finishes after a delay it's a little annoying.
Does anybody know a more professional solution...?
Thank you for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我昨天正在处理这些消息,在我的测试程序中,如果我过滤消息,我只会收到一条用于添加的消息和一条用于删除的消息。
请参阅我的博客更详细的描述。
I was playing with this these messages yesterday and in my testprogram I get only one message for adding and one for removal if I filter the messages.
See my blog for a more detailed description.
确实,在大多数情况下您会收到大量消息。我的解决方案与您的类似,只是我使用了线程而不是计时器。在这种情况下,我的等待时间是 15 秒(在 DBT_DEVICEARRIVAL 之后)。
事实上,当我查看您问题的措辞时,DBT_DEVICEARRIVAL 正是您正在寻找的。即使随后出现更多通知,设备此时仍可用。插入 USB 闪存盘时,我收到以下信息:
出于您的目的,DBT_DEVNODES_CHANGED 通知是无关紧要的。您只对 DBT_DEVICEARRIVAL(用 Delphi 的说法是 $8000)感兴趣。
此外,如果您通过消息的符号名称而不是编号来引用消息,那将会非常有帮助。
It's true, for most cases you get numerous messages. My solution to this was similar to yours, except that instead of the timer I used a thread. My waiting time was 15s in this case (after DBT_DEVICEARRIVAL).
In fact DBT_DEVICEARRIVAL is exactly the one you're looking for when I look at the wording of your question. The device is available at that time, even if more notifications follow. I get the following when plugging in a USB key:
For your purpose the DBT_DEVNODES_CHANGED notifications are irrelevant. You are interested solely in DBT_DEVICEARRIVAL ($8000 in Delphi parlance).
Besides, it would really help if you would refer to the messages by their symbolic name, not their number.