我想獲取機器的序列號密鑰。任何機構能幫助我獲取機器的序列號嗎?如何獲取機器的序列號
2
A
回答
3
這裏沒有Cocoa API。你必須打電話給碳。
#import <Carbon/Carbon.h>
#import<IOKit/IOKitLib.h>
#import <mach/mach.h>
NSString* UKSystemSerialNumber()
{
mach_port_t masterPort;
kern_return_t kr = noErr;
io_registry_entry_t entry;
CFTypeRef prop;
CFTypeID propID;
NSString* str = nil;
kr = IOMasterPort(MACH_PORT_NULL, &masterPort);
if(kr != noErr)
goto cleanup;
entry = IORegistryGetRootEntry(masterPort);
if(entry == MACH_PORT_NULL)
goto cleanup;
prop = IORegistryEntrySearchCFProperty(entry, kIODeviceTreePlane, CFSTR("serial-number"), nil, kIORegistryIterateRecursively);
if(prop == nil)
goto cleanup;
propID = CFGetTypeID(prop);
if(propID != CFDataGetTypeID())
goto cleanup;
const char* buf = [(NSData*)prop bytes];
int len = [(NSData*)prop length],
x;
char secondPart[256];
char firstPart[256];
char* currStr = secondPart; // Version number starts with second part, then NULLs, then first part.
int y = 0;
for(x = 0; x < len; x++)
{
if(buf[x] > 0 && (y < 255))
currStr[y++] = buf[x];
else if(currStr == secondPart)
{
currStr[y] = 0; // Terminate string.
currStr = firstPart;
y = 0;
}
}
currStr[y] = 0; // Terminate string.
str = [NSString stringWithFormat: @"%s%s", firstPart, secondPart];
cleanup:
mach_port_deallocate(mach_task_self(), masterPort);
return str;
}
上述代碼來自here
3
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>
// Returns the serial number as a CFString.
// It is the caller's responsibility to release the returned CFString when done with it.
void CopySerialNumber(CFStringRef *serialNumber)
{
if (serialNumber != NULL) {
*serialNumber = NULL;
io_service_t platformExpert = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching("IOPlatformExpertDevice"));
if (platformExpert) {
CFTypeRef serialNumberAsCFString =
IORegistryEntryCreateCFProperty(platformExpert,
CFSTR(kIOPlatformSerialNumberKey),
kCFAllocatorDefault, 0);
if (serialNumberAsCFString) {
*serialNumber = serialNumberAsCFString;
}
IOObjectRelease(platformExpert);
}
}
}
我很小心,它提到關於不作出有關的S/N長度的任何假設一些注意事項或任何東西
+0
嗨。我在使用上面的代碼時遇到以下鏈接錯誤。 1.「_IORegistryEntrySearchCFProperty」,引用自:2.「_IOMasterPort」,引用來自:3.「_IORegistryGetRootEntry」,引用自:4.「_IORegistryEntrySearchCFProperty」,引用自:5.「_IOMasterPort」,引用來自:6.「_IORegistryGetRootEntry」 ,引用自:我已經包含了Carbon/Carbon.h,SystemConfiguration/SystemConfiguration.h文件。如何解決這些錯誤。 – Shakti 2009-12-29 08:46:43
相關問題
- 1. 如何獲取監視器序列號?
- 2. 如何使用python獲取計算機序列號?
- 3. 如何使用HTA獲取計算機序列號
- 4. 如何從EXIF獲取相機序列號?
- 5. 如何獲取硬盤序列號?
- 6. 如何獲取電話序列號(IMEI)
- 7. 如何動態獲取序列號?
- 8. 獲取xargs的序列號
- 9. 如何通過僅使用SIM卡序列號獲取手機號碼
- 10. 獲取列表中每臺計算機的序列號(Powershell)
- 11. 如何獲取手機的型號?
- 12. 如何獲取系統驅動器卷序列號
- 13. C/C++如何在linux上獲取處理器序列號
- 14. 獲取OSX HD序列號
- 15. 獲取BIOS序列號
- 16. 獲取USB序列號C++
- 17. 從程序獲取EV3的序列號
- 18. 如何通過C#.Net獲取所有計算機硬件的序列號?
- 19. C++/Windows的:獲取唯一的機器ID:MAC地址,卷序列號,
- 20. 我如何通過Chromecast上的接收器應用程序獲取序列號
- 21. 獲取tcp socket序列號ack號碼
- 22. 獲取脫機磁盤的邏輯磁盤序列號
- 23. 如何獲取qtextedit中的列號?
- 24. 如何獲取C++中的列號?
- 25. Windows VC++獲取機器型號名稱
- 26. 獲取機器人SIM號碼編程
- 27. 從CRL列表中獲取序列號
- 28. 如何根據運動類型獲取一系列序列號?
- 29. 如何獲取pendrive的驅動器號
- 30. 如何在Android應用程序中獲取手機號碼?
嗨。我在使用上面的代碼時遇到以下鏈接錯誤。 1. 「_IORegistryEntrySearchCFProperty」,從引用: 2. 「_IOMasterPort」,從引用: 3. 「_IORegistryGetRootEntry」,從引用: 4. 「_IORegistryEntrySearchCFProperty」,從引用: 5. 「_IOMasterPort」,從引用: 6.「_IORegistryGetRootEntry」,引用自: 我已經包含了Carbon/Carbon.h,SystemConfiguration/SystemConfiguration.h文件。如何解決這些錯誤。 – Shakti 2009-12-29 08:46:05
你將不得不在你的項目中添加'IOKit'和'Carbon'框架 – iamamac 2009-12-29 09:01:49
非常感謝lamamac.It完美的工作。 – Shakti 2009-12-29 10:09:03