windows 如何检测何时插入 SD 卡?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/583593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:02:51  来源:igfitidea点击:

How can I detect when an SD card is plugged in?

.netc++windowswindows-xp

提问by

I'm writing a PC application in C++ that needs to detect when a user connects an SD card to his/her computer. I know how to detect when the card reader is connected, but I do not know how to detect when individual cards are connected/removed.

我正在用 C++ 编写一个 PC 应用程序,它需要检测用户何时将 SD 卡连接到他/她的计算机。我知道如何检测读卡器何时连接,但我不知道如何检测单个卡何时连接/移除。

Does Windows have an IO interface to detect arrival/removal of SD cards?

Windows 是否有 IO 接口来检测 SD 卡的到达/移除?

[Update] the WM_DEVICECHANGE technique that was suggested works only for detecting when an SD Card reader is plugged in. It does not detect when individual cards are inserted into the card reader.

[更新] 建议的 WM_DEVICECHANGE 技术仅用于检测何时插入 SD 卡读卡器。它不会检测何时将单张卡插入读卡器。

回答by Terry

WM_DEVICECHANGE might be your answer. Hereis an example with CDROM.

WM_DEVICECHANGE 可能是您的答案。 是一个带有 CDROM 的示例。

回答by Oscar Cabrero

check the messages from WM_DEVICECHANGEhere is an example

检查来自WM_DEVICECHANGE的消息这里是一个例子

回答by Michael

Look at the Windows Portable Devices API, it has functionality that allows you to attach it to an SD reader (it's known as a storage device) and get events when a card is inserted or removed.

看看 Windows 便携式设备 API,它的功能允许您将其连接到 SD 读卡器(称为存储设备)并在插入或移除卡时获取事件。

It just has two (possibly major) drawbacks:

它只有两个(可能是主要的)缺点:

  • It requires Windows Vista or later
  • It only has a COM API, so if you want to use it from a .NET program you either have a lot of interop ahead of you, or you have to write a wrapper in C++ or C++/CLI
  • 它需要 Windows Vista 或更高版本
  • 它只有一个 COM API,所以如果你想从 .NET 程序中使用它,你要么有很多互操作性,要么你必须用 C++ 或 C++/CLI 编写一个包装器