获取目标C中的图形卡信息

时间:2013-08-06 10:29:22

标签: objective-c macos

我需要在我的应用程序中获取显卡信息。我需要的信息与 Graphics / Displays:部分下 * system_profiler SPDisplays * 命令所显示的信息相同。 我已经考虑过使用 sysctl() ,但我无法在sysctl.h中找到适当的图形卡硬件选择器

任何高度重视的建议。

1 个答案:

答案 0 :(得分:5)

在摆弄IOKit之后,我设法获取了所需的信息。可以看到代码hereoriginal source)及其下方:

- (void)displayGraphicsInfo
{
    // Get dictionary of all the PCI Devicces
    CFMutableDictionaryRef matchDict = IOServiceMatching("IOPCIDevice");

    // Create an iterator
    io_iterator_t iterator;

    if (IOServiceGetMatchingServices(kIOMasterPortDefault,matchDict,
                                     &iterator) == kIOReturnSuccess)
    {
        // Iterator for devices found
        io_registry_entry_t regEntry;

        while ((regEntry = IOIteratorNext(iterator))) {
            // Put this services object into a dictionary object.
            CFMutableDictionaryRef serviceDictionary;
            if (IORegistryEntryCreateCFProperties(regEntry,
                                                  &serviceDictionary,
                                                  kCFAllocatorDefault,
                                                  kNilOptions) != kIOReturnSuccess)
            {
                // Service dictionary creation failed.
                IOObjectRelease(regEntry);
                continue;
            }
            const void *GPUModel = CFDictionaryGetValue(serviceDictionary, @"model");

            if (GPUModel != nil) {
                if (CFGetTypeID(GPUModel) == CFDataGetTypeID()) {
                    // Create a string from the CFDataRef.
                    NSString *modelName = [[NSString alloc] initWithData:
                                           (NSData *)GPUModel encoding:NSASCIIStringEncoding];

                    NSLog(@"GPU Model: %@", modelName);
                    [modelName release];
                }
            }
            // Release the dictionary
            CFRelease(serviceDictionary);
            // Release the serviceObject
            IOObjectRelease(regEntry);
        }
        // Release the iterator
        IOObjectRelease(iterator);
    }
}