1

我想在用户插入 USB 密钥、添加外部磁盘和挂载磁盘映像时更新存储设备列表。IOKit 的 IOServiceAddInterestNotification 看起来像是要走的路,但是在 kIOMediaClass 中注册一般兴趣的明显用途只会给你通知卸载卷,然后只是有时。

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

4

4 回答 4

3

DiskArbitration.h 中的以下调用正是我想要的:

  • DARegisterDiskAppearedCallback
  • DARegisterDiskDisappearedCallback
  • DARegisterDiskDescriptionChangedCallback

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

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

于 2008-10-11T21:35:33.343 回答
1

我想在用户插入 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 硬盘的连接。

正如您从这个小样本中看到的那样,它很容易被证明适用于监控已安装的驱动器。或者它可能不会。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);

等等,等等。

于 2012-01-09T14:57:50.220 回答
0

观察/Volumes变化会满足你的需要吗?

于 2008-10-05T13:32:14.287 回答
0

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

  • NSWorkspaceDidMountNotification
  • NSWorkspaceDidRenameVolumeNotification
  • NSWorkspaceWillUnmountNotification
  • NSWorkspaceDidUnmountNotification
于 2011-12-30T14:36:43.360 回答