Mac OS X의 고유 한 하드웨어 ID
-
06-09-2019 - |
문제
Mac OS X Development는 저에게 상당히 새로운 동물이며 일부 소프트웨어를 포팅하는 과정에 있습니다. 소프트웨어 라이센스 및 등록의 경우 일종의 하드웨어 ID를 생성 할 수 있어야합니다. 화려할 필요는 없습니다. 이더넷 MAC 주소, 하드 드라이브 직렬, CPU 직렬 등.
나는 그것을 창에서 덮었지만 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]); }'
에서 여기
다음은 Cocoa로 싸인 명령입니다 (아마도 조금 더 깨끗하게 만들 수 있음).
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 Call 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를 생성하는 데 사용하는 하드웨어 식별자는 자체적으로 수정 될 수 있습니다.
대기 인수는 결과를 기다리는 최대 시간을 지정하는 구조 시간 시간에 대한 포인터입니다. TV_SEC 및 TV_NSEC 필드를 0으로 설정하면 완료 될 때까지 무한정 대기를 의미합니다.
반환 값 :
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를 반환합니다. 그것은 내 10.5 박스에서 작동합니다. 다른 버전의 OS X에서 올바른 문자열이 무엇인지 잘 모르겠습니다.
위의 일부 사람들이 암시 한 것처럼 터미널 명령을 사용하여 하드웨어 ID를 얻을 수 있습니다.
그러나 코드 로이 작업을하고 싶다고 생각하므로 코코아의 Nstask 클래스를 살펴 보겠습니다. 기본적으로 응용 프로그램 내에서 터미널 명령을 실행할 수 있습니다.
이 코드는 코코아에서 Nstask를 사용하는 방법의 예입니다. "Killall"명령을 실행하도록 환경을 설정합니다. 그것은 "Finder"주장을 전달합니다.
명령 줄에서 "Killall Finder"를 실행하는 것은 분명히 파인더를 죽일 것입니다.
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 주소가 있습니다).