Re: HW Serial Number
Re: HW Serial Number
- Subject: Re: HW Serial Number
- From: "Tomas Zahradnicky, Jr." <email@hidden>
- Date: Wed, 5 Dec 2001 23:31:37 +0100
Is there an API that will get the hardware serial number from the machine?
(same as what is displayed in the System Profiler)
Sure. this snippet does it for you. I don't know if NameRegistry is
accessible from within cocoa, but i'd try to look for it in
CoreServices.framework.
-Tomas
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* MacOS specific includes */
#include <MacMemory.h>
#include <NameRegistry.h>
int main(void)
{
OSStatus err;
RegPropertyValueSize size;
RegPropertyValue value;
RegEntryID entry;
/* the following variables are only used in the
cleaning up of the serial number */
char *thing, *serial;
int i = 0;
/* this is the start of the actual OF retrieval code
Taken from 'Designing PCI Cards & Drivers for Power Macintosh
Computers' (available from Apple's Dev site, Chapter 10*/
err = RegistryCStrEntryLookup(NULL, "Devices:device-tree", &entry);
if (err) {
return 1;
}
err = RegistryPropertyGetSize(&entry, "serial-number", &size);
if (err) {
return 1;
}
value = malloc(size);
if (value == NULL) {
return 1;
}
err = RegistryPropertyGet(&entry, "serial-number", value, &size);
if (err != noErr) {
free(value);
value = NULL;
return 1;
}
/* that's it - the data is in the 'value' variable. The remainder
of the code converts that data into a useful format (a c string)
I havent done extensive testing - this format cleaning up works
on gigabit ethernet G4s, but may be different on other machines.
Use at own risk! */
thing = (char *)malloc(size);
if (thing == NULL) {
return 1;
}
strcpy(thing, "");
/* Get the data into a useful type of variable */
BlockMoveData(value, thing, size);
serial = (char *)malloc(size);
if (serial == NULL) {
return 1;
}
strcpy(serial, "");
/* strip out all the null characters in the data which we don't want
*/
for (i = 0; i < size; i++) {
if (thing[i] != '\0') {
strncat(serial, &thing[i], 1);
}
}
strcpy(thing, "");
/* for some reason the last 3 characters of the serial number are the
first three in the data we retrieved from OF. So, we swap them
around */
for (i = 3; i < 11; i++) {
strncat(thing, &serial[i], 1);
}
for (i = 0; i < 3; i++) {
strncat(thing, &serial[i], 1);
}
printf("serial number: %s\n", thing);
free(serial);
free(thing);
free(value);
return 0;
}
-Tomas
--
# Tomas Zahradnicky, Jr
# The Czech Technical University
# FEL-CTU, Prague