xcode 如何创建一个程序来列出 Mac 中的所有 USB 设备?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7567872/
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-14 22:02:23  来源:igfitidea点击:

How to create a program to list all the USB devices in a Mac?

c++xcodemacosiokit

提问by Dili

I have a limited exposure to the Mac OS X operating system and now I have started using Xcode and am studying about I/O kit. I need to create a program in Xcode under command line tool in order to list all USB devices connected in a Mac system. Those who have previous experience under this, please help me. If anyone could provide me with sample code then it will be of great use, as I am looking for starting point.

我对 Mac OS X 操作系统的接触有限,现在我已经开始使用 Xcode 并且正在研究 I/O 套件。我需要在命令行工具下的 Xcode 中创建一个程序,以便列出 Mac 系统中连接的所有 USB 设备。有过这方面经验的人,请帮帮我。如果有人可以为我提供示例代码,那么它将非常有用,因为我正在寻找起点。

回答by Hasturkun

You can adapt USBPrivateDataSampleto your needs, the sample sets up a notifier, lists the currently attached devices, then waits for device attach/detach. If you do, you will want to remove the usbVendorand usbProductmatching dictionaries, so all USB devices are matched.

您可以根据需要调整USBPrivateDataSample,示例设置通知程序,列出当前连接的设备,然后等待设备连接/分离。如果你这样做,你将要删除usbVendorusbProduct匹配字典,因此,所有的USB设备相匹配。

Alternately, you can use IOServiceGetMatchingServicesto get an iterator for all current matching services, using a dictionary created by IOServiceMatching(kIOUSBDeviceClassName).

或者,您可以使用IOServiceGetMatchingServices由创建的字典来获取所有当前匹配服务的迭代器IOServiceMatching(kIOUSBDeviceClassName)

Here's a short sample (which I've never run):

这是一个简短的示例(我从未运行过):

#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>

int main(int argc, const char *argv[])
{
    CFMutableDictionaryRef matchingDict;
    io_iterator_t iter;
    kern_return_t kr;
    io_service_t device;

    /* set up a matching dictionary for the class */
    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
    if (matchingDict == NULL)
    {
        return -1; // fail
    }

    /* Now we have a dictionary, get an iterator.*/
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
    if (kr != KERN_SUCCESS)
    {
        return -1;
    }

    /* iterate */
    while ((device = IOIteratorNext(iter)))
    {
        /* do something with device, eg. check properties */
        /* ... */
        /* And free the reference taken before continuing to the next item */
        IOObjectRelease(device);
    }

   /* Done, release the iterator */
   IOObjectRelease(iter);
   return 0;
}

回答by Paul R

You just need to access the IOKit Registry. You may well be able to use the ioregtool to do this (e.g. run it via system()or popen()). If not then you can at least use it to verify your code:

您只需要访问 IOKit注册表。您很可能可以使用该ioreg工具来执行此操作(例如,通过system()或运行它popen())。如果没有,那么您至少可以使用它来验证您的代码:

Info on ioregtool:

ioreg工具信息:

$ man ioreg

Get list of USB devices:

获取 USB 设备列表:

$ ioreg -Src IOUSBDevice

回答by Nicholas Smith

If you run system_profiler SPUSBDataTypeit'll list all the USB devices connected to the system, you can then interact with that data either by dumping it into a text file or reading it from the command into the application and working with it there.

如果您运行system_profiler SPUSBDataType它,它将列出连接到系统的所有 USB 设备,然后您可以通过将其转储到文本文件中或从命令中将其读取到应用程序中并在那里使用它来与该数据进行交互。