Skip to content

Instantly share code, notes, and snippets.

@chamons
Created October 9, 2014 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chamons/82ab06f5e83d2cb10193 to your computer and use it in GitHub Desktop.
Save chamons/82ab06f5e83d2cb10193 to your computer and use it in GitHub Desktop.
[DllImport ("/System/Library/Frameworks/IOKit.framework/IOKit")]
static extern uint IOServiceGetMatchingService(uint masterPort, IntPtr matching );
[DllImport ("/System/Library/Frameworks/IOKit.framework/IOKit")]
static extern IntPtr IOServiceMatching (string s);
[DllImport ("/System/Library/Frameworks/IOKit.framework/IOKit")]
static extern IntPtr IORegistryEntryCreateCFProperty(uint entry, IntPtr key, IntPtr allocator, uint options);
[DllImport ("/System/Library/Frameworks/IOKit.framework/IOKit")]
static extern int IOObjectRelease(uint o);
static string GetSerialNumber ()
{
string serial = string.Empty;
uint platformExpert = IOServiceGetMatchingService (0, IOServiceMatching ("IOPlatformExpertDevice"));
if (platformExpert != 0) {
NSString key = (NSString)"IOPlatformSerialNumber";
IntPtr serialNumber = IORegistryEntryCreateCFProperty (platformExpert, key.Handle, IntPtr.Zero, 0);
if (serialNumber != IntPtr.Zero) {
serial = new NSString (serialNumber);
}
IOObjectRelease (platformExpert);
}
return serial;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment