在Mac OS X中唯一硬件ID
-
06-09-2019 - |
题
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 ++
解决方案
尝试此终端命令:
ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }'
从此处
下面是命令包在可可(这也许可以由位清洁器):
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];
其他提示
有关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);
}
为什么不试试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参数是一个指向一个结构的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.
如果你告诉我们你使用的是什么语言这会更容易回答。这些信息是没有获得通过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];
}
/*
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;
}
运行:
system_profiler | grep 'Serial Number (system)'
在一个终端返回它所可能的唯一id。该工程在我的箱子10.5,我不知道正确的字符串将在OS X的其他版本的东西。
至于有些人上面已经暗示,你可以使用终端命令来获取硬件ID。
我假设你想这样做的代码不过,所以我会看看在可可的NSTask类。基本上,它可以让你运行你的应用程序中终端的命令。
此代码是如何在可可使用NSTask一个例子。它建立了执行“killall”命令环境。它通过它的arguement “搜索”。
这是运行在命令行上的“查找killall”,这显然会杀死查找器的equivilent。
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];
系统概述(在应用程序 - 实用工具)包含了大部分此类信息的。它有你的序列号和MAC地址(没有联系到Mac。所有的计算机都有一个MAC地址,这是非常独特的,以每个网络卡)。