Domanda

lo sviluppo di Mac OS X è un nuovo animale per me, e io sono nel processo di porting su alcuni software. Per licenze software e la registrazione ho bisogno di essere in grado di generare una sorta di ID hardware. Esso non deve essere qualcosa di fantasia; indirizzo MAC Ethernet, seriale del disco rigido, di serie di CPU, una cosa del genere.

Non ho capito coperto su Windows, ma non ho la più pallida idea su Mac. Qualsiasi idea di che cosa ho bisogno di fare, o dove posso andare per informazioni su questo sarebbe grande!

Modifica:

Per chiunque altro che è interessato a questo, questo è il codice ho finito per usare con la classe QProcess di Qt:

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();

. Nota: sto usando C ++

È stato utile?

Soluzione

Provate questo comando del Terminale:

ioreg -rd1 -c IOPlatformExpertDevice | awk '/IOPlatformUUID/ { split($0, line, "\""); printf("%s\n", line[4]); }'

qui

Ecco quel comando avvolto in Cocoa (che probabilmente potrebbe essere fatto un po 'più pulito):

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];

Altri suggerimenti

Per 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);    
}

Perché non provi gethostuuid()? Ecco la documentazione del sistema Mac OS X di chiamate Manuale:

Nome:

 gethostuuid -- return a unique identifier for the current machine

SINOSSI:

 #include <unistd.h>

 int gethostuuid(uuid_t id, const struct timespec *wait);

Descrizione:

  

La funzione gethostuuid () restituisce un uuid_t 16 byte specificato da id, che identifica in modo univoco il computer corrente. Essere consapevoli del fatto che gli identificatori hardware che gethostuuid () utilizza per generare l'UUID può essere modificato se stessi.

     

L'argomento di attesa è un puntatore ad una struct timespec che specifica il tempo massimo di attesa per il risultato. Impostazione della tv_sec e campi tv_nsec a zero significa aspettare a tempo indeterminato fino a quando non viene completata.

valori di ritorno:

  

La funzione gethostuuid () restituisce zero in caso di successo o -1 su errore.

ERRORI

  

Il gethostuuid () funzioni non riesce se:

 [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.

Questo sarebbe più facile rispondere se lei ci ha detto che lingua si stava utilizzando. L'informazione è ottenibile senza i comandi della shell attraverso il quadro SystemConfiguration, e anche attraverso IOKit se si vuole mettere le mani davvero sporco.

- (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;
}

In esecuzione:

system_profiler | grep 'Serial Number (system)'

in un terminale restituisce quello che probabilmente un ID univoco. Che funziona sulla mia 10.5 scatola, io non sono sicuro di quello che la stringa corretta sarà in altre versioni di OS X.

Come alcune persone sopra hanno accennato, è possibile utilizzare un comando di Terminale per ottenere un ID hardware.

Presumo che si vuole fare questo nel codice tuttavia quindi vorrei dare un'occhiata alla classe NSTask in Cocoa. Esso consente in sostanza di eseguire comandi da terminale all'interno della vostra applicazione.

Questo codice è un esempio di come utilizzare NSTask in Cocoa. Si imposta l'ambiente per eseguire il comando "killall". Si passa l'arguement "Finder".

E 'l'equivilent di correre "killall Finder" sulla linea di comando, che ucciderà il Finder, ovviamente.

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];

System Profiler (in Applicazioni - Utility) contiene la maggior parte di questo tipo di informazioni. Ha il numero di serie e l'indirizzo MAC (nessuna parentela con Mac. Tutti i computer hanno un indirizzo MAC che è praticamente unico per ogni scheda di rete).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top