- 1.调用系统API
#include <IOKit/IOKitLib.h>
#include <iostream>
using namespace std;
QString GetMacUUID()
{
char buf[512] = "";
int bufSize = sizeof(buf);
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);
QString qsUUID(buf);
return qsUUID;
}
配置文件中添加依赖
macx {
LIBS += -framework CoreFoundation -framework IOKit
}
- 2.通过调用命令行
QString GetMacUUID()
{
QString ret = "";
QProcess proc;
QStringList args;
args << "-c" << "ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { print $3; }'";
proc.start( "/bin/bash", args );
proc.waitForStarted(-1);
proc.waitForFinished(-1); //-1的意思就是等待你的事件真的结束
ret = proc.readAll();
ret.replace("\n","");
ret.replace("\"", "");
ret.replace("\\", "");
return ret;
}