2009-12-29 40 views

回答

3

這裏沒有Cocoa API。你必須打電話給碳。

#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

嗨。我在使用上面的代碼時遇到以下鏈接錯誤。 1. 「_IORegistryEntrySearchCFProperty」,從引用: 2. 「_IOMasterPort」,從引用: 3. 「_IORegistryGetRootEntry」,從引用: 4. 「_IORegistryEntrySearchCFProperty」,從引用: 5. 「_IOMasterPort」,從引用: 6.「_IORegistryGetRootEntry」,引用自: 我已經包含了Carbon/Carbon.h,SystemConfiguration/SystemConfiguration.h文件。如何解決這些錯誤。 – Shakti 2009-12-29 08:46:05

+0

你將不得不在你的項目中添加'IOKit'和'Carbon'框架 – iamamac 2009-12-29 09:01:49

+0

非常感謝lamamac.It完美的工作。 – Shakti 2009-12-29 10:09:03

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 2009-12-29 08:46:43