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 System Calls Manual のドキュメントです。
名前:
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() 関数は、成功した場合は 0 を返し、エラーの場合は -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のフレームワークを通じ、また、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を返します。それは私が正しい文字列は、OS Xの他のバージョンではどうなるかわからないんだけど、私の10.5ボックス上で動作します。
は、ハードウェア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アドレスを持っている)。