2015-07-09 55 views
-1

我需要获取OSX上的HD序列号。到目前为止,我找不到任何Delphi例子。获取OSX HD序列号

我发现这个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将受到高度赞赏。

@大卫 - 在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; 
+0

查看'System.SysUtils'中的'FileSystemAttributes'(如果您有Delphi Professional或更高版本)。虽然没有找到卷ID,但它确实显示了如何在OS X中获取有关卷的信息。 – Hans

+0

@Hans - 谢谢,但FSCatalogInfo不提供有关卷ID的任何信息。 – DanielH

+1

您似乎在要求我们为您移植代码。什么阻止你编写代码? –

回答

1

原来铸造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;