How to get harddrive serial number?

Mac OSX specific forum
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3944
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: 1415
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