问题描述
我需要获得硬盘序列号才能将其用作许可软件的密钥.我在这个 url 中使用了 diskid32 代码:http://www.winsim.com/diskid32/diskid32.html它使用 DeviceIoControl Win32 API 和 IOCTL_STORAGE_QUERY_PROPERTY 的 IO 控制代码.
I need to get HDD serial number to use it as a key for licensing a software.I used diskid32 code in this url: http://www.winsim.com/diskid32/diskid32.htmlIt used the DeviceIoControl Win32 API with the IO control code of IOCTL_STORAGE_QUERY_PROPERTY.
它奏效了.然而,当我仔细检查打印在硬盘本身上的实际序列号时,我发现该数字的每 2 个字节都被翻转了.
It worked. However, when I double check with the actual serial number printed on the HDD itself, I found that every 2 bytes of the number was flipped.
一个简单的解决方案可能是简单地翻转字节.它适用于 Windows XP、Vista 和 7,但在 Windows 8 中不需要翻转!
A simple solution could be to simply flip the bytes back. It worked in Windows XP, Vista and 7 but in windows 8 not need to be flipped!
我想知道在 Windows XP、Vista 和 7 中字节翻转的确切原因,而在 Windows 8 中为什么不翻转.下一个 Windows 怎么样?
I wish to know the exact reason why the bytes were flipped in Windows XP, Vista and 7, and why not flipped in Windows 8. What about next Windows?
部分代码略有改动:
int drive = 0;
HANDLE hPhysicalDriveIOCTL = 0;
char driveName [256];
sprintf (driveName, "\\\\.\\PhysicalDrive%d", drive);
// Windows NT, Windows 2000, Windows XP - admin rights not required
hPhysicalDriveIOCTL = CreateFile (driveName, 0,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (hPhysicalDriveIOCTL != INVALID_HANDLE_VALUE)
{
_STORAGE_PROPERTY_QUERY query;
DWORD cbBytesReturned = 0;
char buffer [10000];
memset ((void *) & query, 0, sizeof (query));
query.PropertyId = StorageDeviceProperty;
query.QueryType = PropertyStandardQuery;
memset (buffer, 0, sizeof (buffer));
if ( DeviceIoControl (hPhysicalDriveIOCTL, IOCTL_STORAGE_QUERY_PROPERTY,
& query,
sizeof (query),
& buffer,
sizeof (buffer),
& cbBytesReturned, NULL) )
{
_STORAGE_DEVICE_DESCRIPTOR * descrip = (_STORAGE_DEVICE_DESCRIPTOR *) & buffer;
char serialNumber [1000];
char modelNumber [1000];
char vendorId [1000];
char productRevision [1000];
flipAndCodeBytes (buffer,
descrip -> SerialNumberOffset,
1, serialNumber );
...
}
推荐答案
只需在 windows 8 或更高版本时使用 flibAndCodeBytes 函数的翻转"标志关闭翻转.
Just turn off the flip using the "flip" flag of the flibAndCodeBytes function when windows 8 or greater.
bool shoulFlipBytes = true;
if(IsWin8OrLater()) shouldFlipBytes = false;
flipAndCodeBytes(buffer,
descrip->SerialNumberOffset,
shouldFlipBytes,
serialNumber);
您可以使用它来检查 Windows 版本:
You can use this to check for windows version:
#include <windows.h>
bool IsWin8OrLater() {
DWORD version = GetVersion();
DWORD major = (DWORD) (LOBYTE(LOWORD(version)));
DWORD minor = (DWORD) (HIBYTE(LOWORD(version)));
return (major > 6) || ((major == 6) && (minor >= 2));
}
根据 http://msdn.microsoft.com/en-us/library/ms724832%28VS.85%29.aspx(感谢 ChrisV 确定操作系统是否为 Windows 7)
According to http://msdn.microsoft.com/en-us/library/ms724832%28VS.85%29.aspx(Thanks to ChrisV Determine if O/S is Windows 7)
这篇关于硬盘序列号在 Windows XP、Vista 和 7 中每 2 个字节翻转一次,但在 Windows 8 中没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!