mac osx 获取系统信息

获取mac地址

bool ComputerDevice::GetMacAddress(std::vector<std::string>& vctMacAddress_)
{
    kern_return_t kr;
    CFMutableDictionaryRef matchDict;
    io_iterator_t iterator;
    io_registry_entry_t entry;

    matchDict = IOServiceMatching("IOEthernetInterface");
    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchDict, &iterator);

    NSDictionary *resultInfo = nil;

    while ((entry = IOIteratorNext(iterator)) != 0)
    {
        CFMutableDictionaryRef properties=NULL;
        kr = IORegistryEntryCreateCFProperties(entry,
                                               &properties,
                                               kCFAllocatorDefault,
                                               kNilOptions);
        if (properties)
        {
            resultInfo = (NSDictionary *)properties;
            NSString *bsdName = [resultInfo objectForKey:@"BSD Name"];
            NSData *macData = [resultInfo objectForKey:@"IOMACAddress"];
            if (!macData)
            {
                continue;
            }

            NSMutableString *macAddress = [[NSMutableString alloc] init];
            const UInt8 *bytes = (const UInt8*)[macData bytes];
            for (NSUInteger i=0; i<macData.length; i++)
            {
                [macAddress appendFormat:@"%02x",*(bytes+i)];

            }

            //打印Mac地址
            if (bsdName && macAddress)
            {
                NSLog(@"网卡:%@\nMac地址:%@\n",bsdName,macAddress);
                vctMacAddress_.push_back([macAddress UTF8String]);
            }
        }
    }
    IOObjectRelease(iterator);
    return true;
}

获取CPU 序列号

std::string ComputerDevice::GetCpuIdSerial()
{
    NSProcessInfo* pinfo = [NSProcessInfo processInfo];
    NSString* ret = [pinfo globallyUniqueString];
    return [ret UTF8String];
}

获取操作系统序列号

std::string ComputerDevice::GetSoftSytemSerial()
{
    NSString * ret = nil;  
    io_service_t platformExpert ;  
    platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")) ;  

    if (platformExpert) {  
        CFTypeRef uuidNumberAsCFString ;  
        uuidNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, CFSTR("IOPlatformSerialNumber"), kCFAllocatorDefault, 0) ;  
        if (uuidNumberAsCFString)   {  
            ret = [(NSString *)(CFStringRef)uuidNumberAsCFString copy];  
            CFRelease(uuidNumberAsCFString); uuidNumberAsCFString = NULL;  
        }  
        IOObjectRelease(platformExpert); platformExpert = 0;  
    }  

    std::string str = [ret UTF8String];
    [ret autorelease];
    return str;
}

获取硬盘序列号


std::string ComputerDevice::GetHardWareSerial()
{

    NSString *ret = nil;  
    io_service_t platformExpert ;  
    platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice")) ;  

    if (platformExpert) {  
        CFTypeRef serialNumberAsCFString ;  
        serialNumberAsCFString = IORegistryEntryCreateCFProperty(platformExpert, CFSTR("IOPlatformUUID"), kCFAllocatorDefault, 0) ;  
        if (serialNumberAsCFString) {  
            ret = [(NSString *)(CFStringRef)serialNumberAsCFString copy];  
            CFRelease(serialNumberAsCFString); serialNumberAsCFString = NULL;  
        }  
        IOObjectRelease(platformExpert); platformExpert = 0;  
    }  
    std::string str = [ret UTF8String];
    [ret autorelease];
    return str;
}

深圳利程电子有限公司


所有图片均来自网络


   转载规则


《mac osx 获取系统信息》 smoking 采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
  目录