如何在C ++ Builder中获取OSX唯一ID

时间:2014-02-06 11:21:03

标签: c++builder firemonkey

我可以通过以下命令行检索系统序列号:

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 个答案:

答案 0 :(得分: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;
}