2009-12-29 3 views

ответ

3

Для этого не существует API Cocoa. Вы должны позвонить в Carbon.

#import <Carbon/Carbon.h> 
#import<IOKit/IOKitLib.h> 
#import <mach/mach.h> 

NSString* UKSystemSerialNumber() 
{ 
    mach_port_t    masterPort; 
    kern_return_t   kr = noErr; 
    io_registry_entry_t  entry; 
    CFTypeRef    prop; 
    CFTypeID    propID; 
    NSString*    str = nil; 

    kr = IOMasterPort(MACH_PORT_NULL, &masterPort); 
    if(kr != noErr) 
     goto cleanup; 
    entry = IORegistryGetRootEntry(masterPort); 
    if(entry == MACH_PORT_NULL) 
     goto cleanup; 
    prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, CFSTR("serial-number"), nil, kIORegistryIterateRecursively); 
    if(prop == nil) 
     goto cleanup; 
    propID = CFGetTypeID(prop); 
    if(propID != CFDataGetTypeID()) 
     goto cleanup; 

    const char* buf = [(NSData*)prop bytes]; 
    int   len = [(NSData*)prop length], 
       x; 

    char secondPart[256]; 
    char firstPart[256]; 
    char* currStr = secondPart; // Version number starts with second part, then NULLs, then first part. 
    int  y = 0; 

    for(x = 0; x < len; x++) 
    { 
     if(buf[x] > 0 && (y < 255)) 
      currStr[y++] = buf[x]; 
     else if(currStr == secondPart) 
     { 
      currStr[y] = 0;  // Terminate string. 
      currStr = firstPart; 
      y = 0; 
     } 
    } 
    currStr[y] = 0; // Terminate string. 

    str = [NSString stringWithFormat: @"%s%s", firstPart, secondPart]; 

cleanup: 
    mach_port_deallocate(mach_task_self(), masterPort); 

    return str; 
} 

Приведенный выше код приходит от here

+0

Hi. Я получаю следующие ошибки привязки при использовании вышеуказанного кода. 1. "_IORegistryEntrySearchCFProperty", ссылка из: 2. "_IOMasterPort", ссылка из: 3. "_IORegistryGetRootEntry", ссылка из: 4. "_IORegistryEntrySearchCFProperty", ссылки из: 5. "_IOMasterPort", ссылки из: 6. «_IORegistryGetRootEntry», ссылка от: Я включил файлы Carbon/Carbon.h, SystemConfiguration/SystemConfiguration.h. Как разрешить эти ошибки. – Shakti

+0

Вам нужно будет добавить в проект проект 'IOKit' и' Carbon' framework – iamamac

+0

Спасибо большое lamamac.It отлично работает отлично. – Shakti

3

Это из Technical Note TN1103

#include <CoreFoundation/CoreFoundation.h> 
#include <IOKit/IOKitLib.h> 

// Returns the serial number as a CFString. 
// It is the caller's responsibility to release the returned CFString when done with it. 
void CopySerialNumber(CFStringRef *serialNumber) 
{ 
    if (serialNumber != NULL) { 
     *serialNumber = NULL; 

     io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, 
             IOServiceMatching("IOPlatformExpertDevice")); 

     if (platformExpert) { 
      CFTypeRef serialNumberAsCFString = 
       IORegistryEntryCreateCFProperty(platformExpert, 
              CFSTR(kIOPlatformSerialNumberKey), 
              kCFAllocatorDefault, 0); 
      if (serialNumberAsCFString) { 
       *serialNumber = serialNumberAsCFString; 
      } 

      IOObjectRelease(platformExpert); 
     } 
    } 
} 

я быть осторожным, хотя, он упоминает некоторые оговорки о не делает никаких предположений о длине S/N или ничего

+0

Привет. Я получаю следующие ошибки привязки при использовании вышеуказанного кода. 1. «_IORegistryEntrySearchCFProperty», на который ссылаются: 2. «_IOMasterPort», на который ссылаются: 3. «_IORegistryGetRootEntry», на который ссылаются: 4. «_IORegistryEntrySearchCFProperty», на который ссылаются: 5. «_IOMasterPort», на который ссылаются: 6. «_IORegistryGetRootEntry», , на который ссылаются: я включил файлы Carbon/Carbon.h, SystemConfiguration/SystemConfiguration.h. Как разрешить эти ошибки. – Shakti

Смежные вопросы