使用 IOKit 返回 Mac 的序列号返回 4 个额外字符 [英] Using IOKit to return Mac's Serial number returns 4 extra characters
问题描述
我正在使用 IOKit 并有以下代码,总体思路是将 platformExpert 密钥传递给这个小型核心基础命令行应用程序,并让它打印解码后的字符串.测试用例是序列号".运行时下面的代码如下:
./编译序列号
几乎可以工作,但返回字符串开头的序列号的最后 4 个字符,即对于示例序列,例如 C12D2JMPDDQX,它将返回
DDQXC12D2JMPDDQX
有什么想法吗?
#include #include int main (int argc, const char * argv[]) {CFStringRef 参数 = CFSTR("序列号");如果(参数 [1]){参数 = CFStringCreateWithCString(空值,argv[1],kCFStringEncodingUTF8);}CFDataRef 数据;io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));如果(平台专家){数据 = IORegistryEntryCreateCFProperty(platformExpert,范围,kCFAllocatorDefault, 0);}IOObjectRelease(平台专家);CFIndex bufferLength = CFDataGetLength(data);UInt8 *buffer = malloc(bufferLength);CFDataGetBytes(data, CFRangeMake(0,bufferLength), (UInt8*) buffer);CFStringRef 字符串 = CFStringCreateWithBytes(kCFAllocatorDefault,缓冲,缓冲区长度,kCFStringEncodingUTF8,真的);CFShow(字符串);返回0;}
您可能误解了 serial-number 参数的值.如果我使用 ioreg -f -k serial-number
,我会得到这个:
<代码> |序列号"=|00000000: 55 51 32 00 00 00 00 00 00 00 00 00 00 XX XX XX XX UQ2..........XXXX|00000011: XX XX XX XX 55 51 32 00 00 00 00 00 00 00 00 00 00 XXXXUQ2........|00000022:00 00 00 00 00 00 00 00 00 ......
(除了重复的部分,我已经删除了我的 Mac 的序列号.)
显示字符串时看不到空字符,因为它们是空字符.我不知道为什么它看起来像是由空字符分隔的多个字段,但看起来就是这样.
我建议进行进一步调查以确保没有关于如何解释这些数据的规范;如果你没有找到任何东西,我会跳过第一次运行的空值,然后得到所有的东西,直到下一次运行空值.
I'm playing with IOKit and have the following code, the general idea is to pass a platformExpert key to this small core foundation command line application and have it print the decoded string. The test case is "serial-number". The code below when run like:
./compiled serial-number
Almost works but returns the last 4 characters of the serial number at the beginning of the string i.e. for an example serial such as C12D2JMPDDQX it would return
DDQXC12D2JMPDDQX
Any ideas?
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
int main (int argc, const char * argv[]) {
CFStringRef parameter = CFSTR("serial-number");
if (argv[1]) {
parameter = CFStringCreateWithCString(
NULL,
argv[1],
kCFStringEncodingUTF8);
}
CFDataRef data;
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("IOPlatformExpertDevice"));
if (platformExpert)
{
data = IORegistryEntryCreateCFProperty(platformExpert,
parameter,
kCFAllocatorDefault, 0);
}
IOObjectRelease(platformExpert);
CFIndex bufferLength = CFDataGetLength(data);
UInt8 *buffer = malloc(bufferLength);
CFDataGetBytes(data, CFRangeMake(0,bufferLength), (UInt8*) buffer);
CFStringRef string = CFStringCreateWithBytes(kCFAllocatorDefault,
buffer,
bufferLength,
kCFStringEncodingUTF8,
TRUE);
CFShow(string);
return 0;
}
You may be misinterpreting the value of the serial-number parameter. If I use ioreg -f -k serial-number
, I get this:
| "serial-number" = | 00000000: 55 51 32 00 00 00 00 00 00 00 00 00 00 XX XX XX XX UQ2..........XXXX | 00000011: XX XX XX XX 55 51 32 00 00 00 00 00 00 00 00 00 00 XXXXUQ2.......... | 00000022: 00 00 00 00 00 00 00 00 00 .........
(I've X'd out my Mac's serial number except for the repeated part.)
You don't see the null characters when you show the string because, well, they're null characters. I don't know why it has what seems like multiple fields separated by null characters, but that's what it seems to be.
I recommend doing further investigation to make sure there isn't a specification for how this data is supposed to be interpreted; if you don't find anything, I'd skip through the first run of nulls and get everything after that up to the next run of nulls.
这篇关于使用 IOKit 返回 Mac 的序列号返回 4 个额外字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!