获取 OSX 高清序列号

Get OSX HD Serial Number

我需要在 OSX 上获取高清序列号。到目前为止,我找不到任何 Delphi 个示例。

我找到了这个 C++ Builder 示例:

AnsiString GetSerialNumber()
{
    AnsiString result;

    io_service_t platformExpert =
        IOServiceGetMatchingService(kIOMasterPortDefault,
            IOServiceMatching("IOPlatformExpertDevice"));

    if (platformExpert) {
        CFTypeRef serialNumberAsCFString =
            IORegistryEntryCreateCFProperty(platformExpert,
                                            CFSTR(kIOPlatformSerialNumberKey),
                                            kCFAllocatorDefault, 0);
        if (serialNumberAsCFString)
        {
            result = CFStringGetCStringPtr((CFStringRef) serialNumberAsCFString, 0);
            CFRelease(serialNumberAsCFString);
        }

        IOObjectRelease(platformExpert);
    }

    return result;
}

我正在使用 XE7。

帮助将此移植到 Delphi 将不胜感激。

@David - 在 Macapi.IOKit 中,IOServiceGetMatchingService 指向 CFDictionaryRef,而 IOServiceMatching 指向 CFMutableDictionaryRef

我找不到任何关于如何将 CFMutableDictionaryRef 转换为 CFDictionaryRef 的文档。

这就是我到目前为止想出的:

function GetMacSerialNo: String;
  Const
  kIOPlatformSerialNumberKey = 'IOPlatformSerialNumber';
  Var
  PlatformExpert: io_service_t;
  M: CFMutableDictionaryRef;
  SerialNumberAsCFString: CFTypeRef;
  _AnsiChar: PAnsiChar;
begin

  M := IOServiceMatching('IOPlatformExpertDevice');

  PlatformExpert := IOServiceGetMatchingService(kIOMasterPortDefault,M); --> E2010 Incompatible types: 'CFDictionaryRef' and 'CFMutableDictionaryRef'

  SerialNumberAsCFString := IORegistryEntryCreateCFProperty(PlatformExpert,
                            CFSTR(kIOPlatformSerialNumberKey),kCFAllocatorDefault,0);

  _AnsiChar := CFStringGetCStringPtr(SerialNumberAsCFString,0);

  Result := String(AnsiString(_AnsiChar));

end;

原来转换 CFMutableDictionaryRef 比我想象的要简单。 这是可能需要它的任何人的工作代码。

Function GetMacSerialNo: String;
  Const
  kIOPlatformSerialNumberKey = 'IOPlatformSerialNumber';
  Var
  PlatformExpert: io_service_t;
  M: CFMutableDictionaryRef;
  SerialNumberAsCFString: CFTypeRef;
  _AnsiChar: PAnsiChar;
begin

  M := IOServiceMatching('IOPlatformExpertDevice');
  PlatformExpert := IOServiceGetMatchingService(kIOMasterPortDefault,CFDictionaryRef(M));

  SerialNumberAsCFString := IORegistryEntryCreateCFProperty(PlatformExpert,
                            CFSTR(kIOPlatformSerialNumberKey),kCFAllocatorDefault,0);

  _AnsiChar := CFStringGetCStringPtr(SerialNumberAsCFString,0);

  Result := String(AnsiString(_AnsiChar));

  IOObjectRelease(PlatformExpert);

End;