問題描述
Mac OS X 開發對我來說是一個相當新的動物,我正在移植一些軟件.對于軟件許可和注冊,我需要能夠生成某種硬件 ID.它不必是任何花哨的東西;以太網MAC地址、硬盤串口、CPU串口之類的.
Mac OS X development is a fairly new animal for me, and I'm in the process of porting over some software. For software licensing and registration I need to be able to generate some kind of hardware ID. It doesn't have to be anything fancy; Ethernet MAC address, hard drive serial, CPU serial, something like that.
我已經在 Windows 上了解了它,但在 Mac 上我沒有任何線索.任何我需要做什么的想法,或者我可以去哪里了解這方面的信息都會很棒!
I've got it covered on Windows, but I haven't a clue on Mac. Any idea of what I need to do, or where I can go for information on this would be great!
對于對此感興趣的其他人,這是我最終在 Qt 的 QProcess 類中使用的代碼:
For anybody else that is interested in this, this is the code I ended up using with Qt's QProcess class:
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++.
Note: I'm using C++.
推薦答案
試試這個終端命令:
ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, """); printf("%s
", line[4]); }'
來自這里
這是用 Cocoa 包裝的命令(可能會更簡潔一些):
Here is that command wrapped in Cocoa (which could probably be made a bit cleaner):
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];
這篇關于Mac OS X 中的唯一硬件 ID的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!