Page 1 of 1

Re: How to get harddrive serial number?

Posted: Sat Oct 27, 2012 8:25 am
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)

Re: How to get harddrive serial number?

Posted: Sat Oct 27, 2012 8:50 am
by Kukulkan
Whowowow!! GREAT!!! Exactly what I need! :D

Thank you both!

Kukulkan