views:

270

answers:

4

Hi. I want to fetch the machine's serial number key. Does any body help me on how to fetch the machine's serial number?

+1  A: 

There is no Cocoa API for this. You have to call into Carbon.

#import <Carbon/Carbon.h>
#import <SystemConfiguration/SystemConfiguration.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;
}

The above code comes from here

Iamamac
Hi. i am getting the following Linking errors while using the above code.1. "_IORegistryEntrySearchCFProperty", referenced from:2. "_IOMasterPort", referenced from:3. "_IORegistryGetRootEntry", referenced from:4. "_IORegistryEntrySearchCFProperty", referenced from:5. "_IOMasterPort", referenced from:6. "_IORegistryGetRootEntry", referenced from:i have included the Carbon/Carbon.h, SystemConfiguration/SystemConfiguration.h files. How to resolve these errors.
Shakti
You will have to add `IOKit` and `Carbon` framework in your project
Iamamac
Thanks a lot lamamac.It's perfectly working fine.
Shakti
Hi. I want to use the value of str outside the function in which it is declared. How to do that?
Shakti
It is the return value of `UKSystemSerialNumber()`.
Iamamac
How to assign that value to other NSString obj. in another function? Pls help me out of this.
Shakti
i m using it like this NSString *strSR = [self UKSystemSerialNumber];
Shakti
Just call it like a plain C function: `NSString* strSR = UKSystemSerialNumber();`
Iamamac
+1  A: 

This is from 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);
        }
   }
}

I'd be careful though, it mentions some caveats about not making any assumptions about the S/N length or anything

Ranman
Hi. i am getting the following Linking errors while using the above code. 1. "_IORegistryEntrySearchCFProperty", referenced from: 2. "_IOMasterPort", referenced from: 3. "_IORegistryGetRootEntry", referenced from: 4. "_IORegistryEntrySearchCFProperty", referenced from: 5. "_IOMasterPort", referenced from: 6. "_IORegistryGetRootEntry", referenced from: i have included the Carbon/Carbon.h, SystemConfiguration/SystemConfiguration.h files. How to resolve these errors.
Shakti
A: 

Have you tried using this:

wmic bios get serialnumber

Makar