首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何获取机器的序列号

问如何获取机器的序列号
EN

Stack Overflow用户
提问于 2009-12-29 14:05:37
回答 2查看 6K关注 0票数 2

我想拿到机器的序列号密钥。有谁能帮我拿到机器的序列号吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-12-29 14:16:22

没有用于此的Cocoa API。你必须呼入Carbon。

代码语言:javascript
复制
#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

票数 3
EN

Stack Overflow用户

发布于 2009-12-29 14:20:48

这是来自Technical Note TN1103的

代码语言:javascript
复制
#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);
        }
   }
}

但我还是要小心,它提到了一些关于不要对序列号长度或任何东西做任何假设的警告

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1973148

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档