Mac OS X中的唯一硬件ID

时间:2009-06-01 03:32:36

标签: c++ macos unique hardware-id

Mac OS X开发对我来说是一个相当新的动物,我正在移植一些软件。对于软件许可和注册,我需要能够生成某种硬件ID。它不一定是任何花哨的东西;以太网MAC地址,硬盘串口,CPU串口等等。

我已经在Windows上覆盖了它,但我对Mac没有任何线索。知道我需要做什么,或者我可以在哪里获取有关此信息的信息会很棒!

编辑:

对于任何对此感兴趣的人,这是我最终使用Qt的QProcess类的代码:

QProcess proc;

QStringList args;
args << "-c" << "ioreg -rd1 -c IOPlatformExpertDevice |  awk '/IOPlatformUUID/ { print $3; }'";
proc.start( "/bin/bash", args );
proc.waitForFinished();

QString uID = proc.readAll();

注意:我正在使用C ++。

8 个答案:

答案 0 :(得分:35)

对于C / C ++:

void get_platform_uuid(char * buf, int bufSize) {
    io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/");
    CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
    IOObjectRelease(ioRegistryRoot);
    CFStringGetCString(uuidCf, buf, bufSize, kCFStringEncodingMacRoman);
    CFRelease(uuidCf);    
}

答案 1 :(得分:15)

尝试此终端命令:

ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }'

来自here

这是用Cocoa包装的命令(可能会更清洁一点):

NSArray * args = [NSArray arrayWithObjects:@"-rd1", @"-c", @"IOPlatformExpertDevice", @"|", @"grep", @"model", nil];
NSTask * task = [NSTask new];
[task setLaunchPath:@"/usr/sbin/ioreg"];
[task setArguments:args];

NSPipe * pipe = [NSPipe new];
[task setStandardOutput:pipe];
[task launch];

NSArray * args2 = [NSArray arrayWithObjects:@"/IOPlatformUUID/ { split($0, line, \"\\\"\"); printf(\"%s\\n\", line[4]); }", nil];
NSTask * task2 = [NSTask new];
[task2 setLaunchPath:@"/usr/bin/awk"];
[task2 setArguments:args2];

NSPipe * pipe2 = [NSPipe new];
[task2 setStandardInput:pipe];
[task2 setStandardOutput:pipe2];
NSFileHandle * fileHandle2 = [pipe2 fileHandleForReading];
[task2 launch];

NSData * data = [fileHandle2 readDataToEndOfFile];
NSString * uuid = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

答案 2 :(得分:8)

如果您告诉我们您使用的语言,这将更容易回答。通过SystemConfiguration框架可以在没有任何shell命令的情况下获取信息,如果你想让你的手真的很脏,也可以通过IOKit获取。

- (NSString*) getMACAddress: (BOOL)stripColons {
    NSMutableString         *macAddress         = nil;
    NSArray                 *allInterfaces      = (NSArray*)SCNetworkInterfaceCopyAll();
    NSEnumerator            *interfaceWalker    = [allInterfaces objectEnumerator];
    SCNetworkInterfaceRef   curInterface        = nil;

    while ( curInterface = (SCNetworkInterfaceRef)[interfaceWalker nextObject] ) {
        if ( [(NSString*)SCNetworkInterfaceGetBSDName(curInterface) isEqualToString:@"en0"] ) {
            macAddress = [[(NSString*)SCNetworkInterfaceGetHardwareAddressString(curInterface) mutableCopy] autorelease];

            if ( stripColons == YES ) {
                [macAddress replaceOccurrencesOfString: @":" withString: @"" options: NSLiteralSearch range: NSMakeRange(0, [macAddress length])];
            }

            break;
        }
    }

    return [[macAddress copy] autorelease];
}

答案 3 :(得分:8)

为什么不试试gethostuuid()?以下是Mac OS X系统调用手册中的文档:

NAME:

 gethostuuid -- return a unique identifier for the current machine

概要:

 #include <unistd.h>

 int gethostuuid(uuid_t id, const struct timespec *wait);

描述:

  

gethostuuid()函数返回由id指定的16字节uuid_t,它唯一地标识当前计算机。请注意,gethostuuid()用于生成UUID的硬件标识符本身可以修改。

     

wait参数是指向struct timespec的指针,指定等待结果的最长时间。将tv_sec和tv_nsec字段设置为零意味着无限期地等待它完成。

返回值:

  

gethostuuid()函数在成功时返回零,或在出错时返回-1。

错误

  

如果出现以下情况,gethostuuid()函数将失败:

 [EFAULT]           wait points to memory that is not a valid part of the
                    process address space.

 [EWOULDBLOCK]      The wait timeout expired before the UUID could be
                    obtained.

答案 4 :(得分:5)

/*
g++ mac_uuid.cpp -framework CoreFoundation -lIOKit
*/


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

using namespace std;

void get_platform_uuid(char * buf, int bufSize)
{
   io_registry_entry_t ioRegistryRoot = IORegistryEntryFromPath(kIOMasterPortDefault, "IOService:/");
   CFStringRef uuidCf = (CFStringRef) IORegistryEntryCreateCFProperty(ioRegistryRoot, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, 0);
   IOObjectRelease(ioRegistryRoot);
   CFStringGetCString(uuidCf, buf, bufSize, kCFStringEncodingMacRoman);
   CFRelease(uuidCf);
}

int main()
{
   char buf[512] = "";
   get_platform_uuid(buf, sizeof(buf));
   cout << buf << endl;
}

答案 5 :(得分:1)

运行:

system_profiler | grep 'Serial Number (system)'
终端中的

返回它可能是唯一ID。这适用于我的10.5盒子,我不确定OS X的其他版本中的正确字符串是什么。

答案 6 :(得分:1)

正如上面的一些人暗示的那样,您可以使用终端命令获取硬件ID。

我假设您想在代码中执行此操作,因此我将查看Cocoa中的NSTask类。它基本上允许您在应用程序中运行终端命令。

此代码是如何在Cocoa中使用NSTask的示例。它设置了执行“killall”命令的环境。它通过了辩论“Finder”。

在命令行上运行“killall Finder”是等效的,这会明显杀死Finder。

NSTask *aTask = [[NSTask alloc] init];
NSMutableArray *args = [NSMutableArray array];

[aTask setLaunchPath: @"/usr/bin/killall"];
[args addObject:[@"/Applications/Finder" lastPathComponent]];
[aTask setArguments:args];
[aTask launch];

[aTask release];

答案 7 :(得分:0)

System Profiler(在Applications - Utilities中)包含大部分此类信息。它有您的序列号和您的mac地址(与Mac无关。所有计算机都有一个mac地址,这对每个网卡来说都是独一无二的。)