检测 osx 上卷的出现/消失

发布于 2024-07-07 06:37:15 字数 174 浏览 5 评论 0原文

我想在用户插入 USB 密钥、添加外部磁盘和安装磁盘映像时更新存储设备列表。 IOKit 的 IOServiceAddInterestNotification 看起来像是可行的方法,但在 kIOMediaClass 中注册一般兴趣的明显用途只是为您提供卸载卷的通知,而且只是有时而已。

这样做的正确方法是什么?

I want to update a list of storage devices as the user inserts USB keys, adds external disks and mounts disk images. IOKit's IOServiceAddInterestNotification looks like the way to go, but the obvious use of registering general interest in kIOMediaClass only gives you notifications for unmounting of volumes and then only sometimes.

What's the right way to do this?

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

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

发布评论

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

评论(4

奢欲 2024-07-14 06:37:15

DiskArbitration.h 中的以下调用完全符合我的要求:

  • DARegisterDiskAppearedCallback
  • DARegisterDiskDisappearedCallback
  • DARegisterDiskDescriptionChangedCallback

这些涵盖插入、删除(甚至是不可安装的卷)
元数据更改事件。

PS 不要忘记将您的 DASession 添加到运行循环
否则您将不会收到任何回调。

The following calls in DiskArbitration.h do exactly what I want:

  • DARegisterDiskAppearedCallback
  • DARegisterDiskDisappearedCallback
  • DARegisterDiskDescriptionChangedCallback

These cover insertion, removal (even of unmountable volumes)
metadata change events.

P.S. Don't forget to add your DASession to a runloop
or you won't get any callbacks.

心奴独伤 2024-07-14 06:37:15

我想在用户插入 USB 密钥、添加外部磁盘和安装磁盘映像时更新存储设备列表。

我可以用这段代码为您提供三分之二,我想不需要做更多的工作即可为您提供第三个。

File:               USBNotificationExample.c

Description:        This sample demonstrates how to use IOKitLib and IOUSBLib to set up asynchronous
                    callbacks when a USB device is attached to or removed from the system.
                    It also shows how to associate arbitrary data with each device instance.

http://opensource .apple.com/source/IOUSBFamily/IOUSBFamily-385.4.1/Examples/Another%20USB%20Notification%20Example/USBNotificationExample.c

我个人使用了很长时间(此代码的稍微修改的副本) ,监控 USB HDD 的连接。

正如您从这个小示例中看到的,它可以轻松地证明适用于监视已安装的驱动器。 或者也可能不会。 YMMV。

    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);        // Interested in instances of class
                                                                    // IOUSBDevice and its subclasses

以及何时匹配

void DeviceAdded(void *refCon, io_iterator_t iterator)
{
    kern_return_t           kr;
    io_service_t            usbDevice;
    IOCFPlugInInterface     **plugInInterface=NULL;
    SInt32                  score;
    HRESULT                 res;    

    while ( (usbDevice = IOIteratorNext(iterator)) )
    {
        io_name_t                   deviceName;
        CFStringRef                 deviceNameAsCFString;
        MyPrivateData               *privateDataRef = NULL;
        UInt32                      locationID;

        printf("Device 0x%08x added.\n", usbDevice);

等等。

I want to update a list of storage devices as the user inserts USB keys, adds external disks and mounts disk images.

I can get you two out of three with this piece of code, which I imagine wouldn't require a lot more work to give you the third.

File:               USBNotificationExample.c

Description:        This sample demonstrates how to use IOKitLib and IOUSBLib to set up asynchronous
                    callbacks when a USB device is attached to or removed from the system.
                    It also shows how to associate arbitrary data with each device instance.

http://opensource.apple.com/source/IOUSBFamily/IOUSBFamily-385.4.1/Examples/Another%20USB%20Notification%20Example/USBNotificationExample.c

I've personally used (a slightly modified copy of this code) for a long time, to monitor the connection of USB HDDs.

As you can see from this small sample, it may easily prove adaptable to monitor mounted drives. Or it may not. YMMV.

    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);        // Interested in instances of class
                                                                    // IOUSBDevice and its subclasses

and when it matches

void DeviceAdded(void *refCon, io_iterator_t iterator)
{
    kern_return_t           kr;
    io_service_t            usbDevice;
    IOCFPlugInInterface     **plugInInterface=NULL;
    SInt32                  score;
    HRESULT                 res;    

    while ( (usbDevice = IOIteratorNext(iterator)) )
    {
        io_name_t                   deviceName;
        CFStringRef                 deviceNameAsCFString;
        MyPrivateData               *privateDataRef = NULL;
        UInt32                      locationID;

        printf("Device 0x%08x added.\n", usbDevice);

and so forth, and so on.

我的痛♀有谁懂 2024-07-14 06:37:15

观察 /Volumes 的变化是否能满足您的需要?

Would watching /Volumes for changes do what you need?

星軌x 2024-07-14 06:37:15

如果您恰好在 Cocoa 级别工作,您还可以注册以接收来自 NSWorkspace

  • NSWorkspaceDidMountNotification
  • NSWorkspaceDidRenameVolumeNotification
  • NSWorkspaceWillUnmountNotification
  • NSWorkspaceDidUnmountNotification

If you happen to be working at the Cocoa level, you can also register to receive the following notifications from NSWorkspace:

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