質問

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アドレスを持っている)。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top