Mac OS X 中的唯一硬件 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++。

原文由 Gerald 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 977
2 个回答

试试这个终端命令:

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

这里

这是包裹在 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];

原文由 xyz 发布,翻译遵循 CC BY-SA 3.0 许可协议

对于 C/C++:

 #include <IOKit/IOKitLib.h>

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);
}

原文由 yairchu 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题