Using IOKit to return Mac's Serial number retu

2020-08-01 05:09发布

I'm playing with IOKit and have the following code, the general idea is to pass a platformExpert key to this small core foundation command line application and have it print the decoded string. The test case is "serial-number". The code below when run like:

./compiled serial-number

Almost works but returns the last 4 characters of the serial number at the beginning of the string i.e. for an example serial such as C12D2JMPDDQX it would return

DDQXC12D2JMPDDQX

Any ideas?

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

int main (int argc, const char * argv[]) {
    CFStringRef parameter = CFSTR("serial-number");
    if (argv[1]) {
       parameter = CFStringCreateWithCString(
                                 NULL,
                                 argv[1],
                                 kCFStringEncodingUTF8);
    }

    CFDataRef data;

    io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
    if (platformExpert)
    {
        data = IORegistryEntryCreateCFProperty(platformExpert,
                                               parameter,
                                               kCFAllocatorDefault, 0);
    }

    IOObjectRelease(platformExpert);
    CFIndex bufferLength = CFDataGetLength(data);  
    UInt8 *buffer = malloc(bufferLength);
    CFDataGetBytes(data, CFRangeMake(0,bufferLength), (UInt8*) buffer);
    CFStringRef string = CFStringCreateWithBytes(kCFAllocatorDefault,
                                                 buffer,
                                                 bufferLength,
                                                 kCFStringEncodingUTF8,
                                                 TRUE);
    CFShow(string);
    return 0;
}

2条回答
▲ chillily
2楼-- · 2020-08-01 05:43

You may be misinterpreting the value of the serial-number parameter. If I use ioreg -f -k serial-number, I get this:

    |   "serial-number" = 
    |     00000000: 55 51 32 00 00 00 00 00 00 00 00 00 00 XX XX XX XX UQ2..........XXXX
    |     00000011: XX XX XX XX 55 51 32 00 00 00 00 00 00 00 00 00 00 XXXXUQ2..........
    |     00000022: 00 00 00 00 00 00 00 00 00                         .........

(I've X'd out my Mac's serial number except for the repeated part.)

You don't see the null characters when you show the string because, well, they're null characters. I don't know why it has what seems like multiple fields separated by null characters, but that's what it seems to be.

I recommend doing further investigation to make sure there isn't a specification for how this data is supposed to be interpreted; if you don't find anything, I'd skip through the first run of nulls and get everything after that up to the next run of nulls.

查看更多
太酷不给撩
3楼-- · 2020-08-01 05:52

A more simplified solution:

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

int main()
{
    CFMutableDictionaryRef matching = IOServiceMatching("IOPlatformExpertDevice");
    io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault, matching);
    CFStringRef serialNumber = IORegistryEntryCreateCFProperty(service,
        CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault, 0);
    const char* str = CFStringGetCStringPtr(serialNumber,kCFStringEncodingMacRoman);
    printf("%s\n", str); //->stdout
    //CFShow(serialNumber); //->stderr
    IOObjectRelease(service);
    return 0;
}

compile with:

clang -framework IOKit -framework ApplicationServices cpuid.c -o cpuid

Fork from github if you like ;)

https://github.com/0infinity/IOPlatformSerialNumber

查看更多
登录 后发表回答