2014-02-06 49 views
2

我可以通過這個命令行檢索系統序列:我如何獲得OSX唯一ID C++ Builder中

AnsiString serial = ExecSysCommand("ioreg -l | awk '/IOPlatformSerialNumber/ { print $4;}'") 
AnsiString ExecSysCommand(AnsiString command) 
{ 
#ifdef __APPLE__ 
    FILE* pipe = popen(command.c_str(), "r"); 
    if (!pipe) 
     return "ERROR"; 
    char buffer[128]; 
    AnsiString result; 
    while (!feof(pipe)) { 
     if (fgets(buffer, 128, pipe) != NULL) 
      result += buffer; 
    } 
    pclose(pipe); 
    return result; 
#elif _Windows 
    return ""; 
#endif 
} 

我怎麼能做到這一點編程的C++ Builder中不使用命令行?

回答

1

使用由於IOKit:

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; 
}