How to get harddrive serial number?

Mac OSX specific forum
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

How to get harddrive serial number?

Post by Kukulkan »

Hi,

I need to get the root drive serial number (volume UUID). I found code for Windows and Linux, but nothing for Mac OS / OSX.

Has anybody already some code for this?

I found this link, but I'm not that deep in Mac. I think it can get done using CocoaMessage() function, but how?
http://stackoverflow.com/questions/9334 ... n-mac-os-x

I don't want to use some commandline function as I need to call this multiple times and it must be fast.

[EDIT]Also interested in getting the MAC address of eth0 directly without commandline call. All code found is only Windows or Linux.[/EDIT]

Kukulkan
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get harddrive serial number?

Post by wilbert »

I think you need these functions for the UUID
http://developer.apple.com/library/mac/ ... TP40011609
but I'm not familiar with them.
From what I've read you should be able to get the UUID using kDADiskDescriptionMediaUUIDKey .
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: How to get harddrive serial number?

Post by Kukulkan »

Hi wilbert,

I'm also not familiar with and this is no API that is covered by PB. So far I dont know how to utilize this :(

Any idea about getting the MAC address of eth0? This would also help me.

Kukulkan
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: How to get harddrive serial number?

Post by Kukulkan »

I think this Cocoa code should return the MAC address of the WiFi. Is someone able to convert this to PB using CocoaMessage()?

Code: Select all

NICInfoSummary* summary = [[[NICInfoSummary alloc] init] autorelease];

// en0 is for WiFi 
NICInfo* wifi_info = [summary findNICInfo:@"en0"];

// you can get mac address in 'XX-XX-XX-XX-XX-XX' form
NSString* mac_address = [wifi_info getMacAddressWithSeparator:@"-"];

// ip can be multiple
if(wifi_info.nicIPInfos.count > 0)
{
    NICIPInfo* ip_info = [wifi_info.nicIPInfos objectAtIndex:0];
    NSString* ip = ip_info.ip;
    NSString* netmask = ip_info.netmask;
    NSString* broadcast_ip = ip_info.broadcastIP;
}
else
{
    NSLog(@"WiFi not connected!");
}
Thank you!
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: How to get harddrive serial number?

Post by Shardik »

The following code finds out the UUID and serial number of the Mac hardware. It works in PB 4.61 and PB 5.00 Beta 6 x86 with Cocoa (no subsystem) and subsystem Carbon (tested on Snow Leopard 10.6.8 ):

Code: Select all

EnableExplicit

#kCFAllocatorDefault  = 0
#kCFStringEncodingMacRoman = 0
#kIOMasterPortDefault = 0

ImportC "/System/Library/Frameworks/IOKit.framework/IOKit"
  IOObjectRelease(IOKitObject.L)
  IORegistryEntryCreateCFProperty(IORegistryEntry.L, KeyRef.L, Allocator.L, OptionBits.L)
  IORegistryEntryFromPath(MasterPort.L, Path.S)
EndImport

ImportC ""
  CFStringGetCString(CFStringRef.L, *StringBuffer, BufferSize.L, CFStringEncoding.L)
EndImport

Procedure.S GetRegistryEntry(Property.S)
  Protected Content.S = Space(128)
  Protected EntryRef.L
  Protected IORegistryRoot.L

  IORegistryRoot = IORegistryEntryFromPath(#kIOMasterPortDefault, "IOService:/")
  
  If IORegistryRoot
    EntryRef = IORegistryEntryCreateCFProperty(IORegistryRoot, CFStringCreateWithCString_(#kCFAllocatorDefault, Property, #kCFStringEncodingMacRoman), #kCFAllocatorDefault, 0)
    IOObjectRelease(IORegistryRoot)
    
    If EntryRef
      CFStringGetCString(EntryRef, @Content, Len(Content), #kCFStringEncodingMacRoman)
      CFRelease_(EntryRef)
      ProcedureReturn  Trim(Content)
    EndIf
  EndIf
EndProcedure

Define Info.S

Info + "Hardware UUID = " + GetRegistryEntry("IOPlatformUUID") + #CR$
Info + "Serial number = " + GetRegistryEntry("IOPlatformSerialNumber")

MessageRequester("Machine infos", Info)
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: How to get harddrive serial number?

Post by wilbert »

Thanks for helping out Shardik. I didn't have a clue how to get that information :shock:

Here the same code with a few adaptations so it also works when compiled on x64 or with unicode enabled.
I also changed encoding to ISOLatin1 since I believe that's what PureBasic uses internally.

Code: Select all

EnableExplicit

#kCFAllocatorDefault  = 0
#kCFStringEncodingISOLatin1 = $0201
#kIOMasterPortDefault = 0

ImportC "/System/Library/Frameworks/IOKit.framework/IOKit"
  IOObjectRelease(object.i)
  IORegistryEntryCreateCFProperty(entry.i, key.i, allocator.i, options.i)
  IORegistryEntryFromPath(masterPort.i, path.p-ascii)
EndImport

ImportC ""
  CFStringCreateWithCString(alloc.i, cStr.p-ascii, encoding.i)
  CFStringGetCString(theString.i, *buffer, bufferSize.i, encoding.i)
EndImport

Procedure.s GetRegistryEntry(Property.s)
  Protected Content.s = Space(128)
  Protected Key.i, EntryRef.i
  Protected IORegistryRoot.i

  IORegistryRoot = IORegistryEntryFromPath(#kIOMasterPortDefault, "IOService:/")
  
  If IORegistryRoot
    Key = CFStringCreateWithCString(#kCFAllocatorDefault, Property, #kCFStringEncodingISOLatin1)
    EntryRef = IORegistryEntryCreateCFProperty(IORegistryRoot, Key, #kCFAllocatorDefault, 0)
    CFRelease_(Key)
    IOObjectRelease(IORegistryRoot)
    
    If EntryRef
      CFStringGetCString(EntryRef, @Content, Len(Content), #kCFStringEncodingISOLatin1)
      CFRelease_(EntryRef)
      ProcedureReturn Trim(PeekS(@Content, -1, #PB_Ascii))
    EndIf
  EndIf
EndProcedure

Define Info.S

Info + "Hardware UUID = " + GetRegistryEntry("IOPlatformUUID") + #CR$
Info + "Serial number = " + GetRegistryEntry("IOPlatformSerialNumber")

MessageRequester("Machine infos", Info)
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: How to get harddrive serial number?

Post by Kukulkan »

Whowowow!! GREAT!!! Exactly what I need! :D

Thank you both!

Kukulkan
Post Reply