2013-08-06 51 views
3

我需要在我的應用程序中獲取圖形卡信息。我需要的信息與* system_profiler SPDisplays *顯示的命令相同,該命令在部分下顯示/顯示:。 我已經使用的sysctl()考慮,但我無法在sysctl.h在目標中獲取圖形卡信息C

高度appericiated任何建議,找到顯卡適當的硬件選擇。

+1

您應該使用代替的sysctl由於IOKit:https://developer.apple.com/library/mac/#documentation/devicedrivers/conceptual/IOKitFundamentals/Introduction /Introduction.html – Macmade

+0

感謝您的建議@Macmade。我將直接從IOKit開始。 – rsharma

回答

3

擺弄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); 
    } 
} 
+1

請包含您目前正在停機的網站代碼。 (另見[你的答案在另一座城堡中:何時答案不是答案?](http://meta.stackexchange.com/questions/225370/your-answer-is-in-another-castle-when-is -an回答 - 不的回答))。 –

+0

你好@rsharma!我已向您的答案提交了一個版本,以包含該鏈接中的代碼,以便即使網站已被所有者關閉,您的答案仍然有效。 – NSNoob

+0

是的,這段代碼看起來像是來自[這裏](http://www.starcoder.com/wordpress/2011/10/using-iokit-to-detect-graphics-hardware/),沒有任何歸屬。 – trojanfoe