#include "serial_number.h"
int MasterHardDiskSerial::ReadPhysicalDriveInNTWithAdminRights(void)
{
int iDone = FALSE;
int iDrive = 0;
for (iDrive = 0; iDrive < MAX_IDE_DRIVES; iDrive++)
{
HANDLE hPhysicalDriveIOCTL = 0;
// Try to get a handle to PhysicalDrive IOCTL, report failure
// and exit if can't.
char cszDriveName[256];
sprintf_s(cszDriveName, 256, "\\\\.\\PhysicalDrive%d", iDrive);
// Windows NT, Windows 2000, must have admin rights
hPhysicalDriveIOCTL = CreateFileA(cszDriveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, 0, NULL);
if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
{
SecureZeroMemory(m_cszErrorMessage, sizeof(m_cszErrorMessage));
sprintf_s(m_cszErrorMessage, 256, "%d ReadPhysicalDriveInNTWithAdminRights ERROR ,CreateFileA(%s) returned INVALID_HANDLE_VALUE", __LINE__, cszDriveName);
}
else
{
GETVERSIONOUTPARAMS VersionParams;
DWORD dwBytesReturned = 0;
// Get the version, etc of PhysicalDrive IOCTL
memset((void*)&VersionParams, 0, sizeof(VersionParams));
if (!DeviceIoControl(hPhysicalDriveIOCTL, DFP_GET_VERSION,
NULL,
0,
&VersionParams,
sizeof(VersionParams),
&dwBytesReturned, NULL))
{
DWORD dwErr = GetLastError();
SecureZeroMemory(m_cszErrorMessage, sizeof(m_cszErrorMessage));
sprintf_s(m_cszErrorMessage, 256, "%d ReadPhysicalDriveInNTWithAdminRights ERROR DeviceIoControl() %d, DFP_GET_VERSION) returned 0, error is %d\n", __LINE__, (int)hPhysicalDriveIOCTL, (int)dwErr);
}
// If there is a IDE device at number "iI" issue commands
// to the device
if (VersionParams.bIDEDeviceMap <= 0)
{
SecureZeroMemory(m_cszErrorMessage, sizeof(m_cszErrorMessage));
sprintf_s(m_cszErrorMessage, 256, "%d ReadPhysicalDriveInNTWithAdminRights ERROR No device found at iPosition %d (%d)", __LINE__, (int)iDrive, (int)VersionParams.bIDEDeviceMap);
}
else
{
BYTE bIDCmd = 0; // IDE or ATAPI IDENTIFY cmd
SENDCMDINPARAMS scip;
//SENDCMDOUTPARAMS OutCmd;
// Now, get the ID sector for all IDE devices in the system.
// If the device is ATAPI use the IDE_ATAPI_IDENTIFY command,
// otherwise use the IDE_ATA_IDENTIFY command
bIDCmd = (VersionParams.bIDEDeviceMap >> iDrive & 0x10) ? \
IDE_ATAPI_IDENTIFY : IDE_ATA_IDENTIFY;
memset(&scip, 0, sizeof(scip));
memset(byIdOutCmd, 0, sizeof(byIdOutCmd));
if (DoIDENTIFY(hPhysicalDriveIOCTL,
&scip,
(PSENDCMDOUTPARAMS)&byIdOutCmd,
(BYTE)bIDCmd,
(BYTE)iDrive,
&dwBytesReturned))
{
DWORD dwDiskData[256];
int iIjk = 0;
USHORT *punIdSector = (USHORT *)
((PSENDCMDOUTPARAMS)byIdOutCmd)->bBuffer;
for (iIjk = 0; iIjk < 256; iIjk++)
dwDiskData[iIjk] = punIdSector[iIjk];
PrintIdeInfo(iDrive, dwDiskData);
iDone = TRUE;
}
}
CloseHandle(hPhysicalDriveIOCTL);
}
}
return iDone;
}
int MasterHardDiskSerial::ReadPhysicalDriveInNTUsingSmart(void)
{
int iDone = FALSE;
int iDrive = 0;
for (iDrive = 0; iDrive < MAX_IDE_DRIVES; iDrive++)
{
HANDLE hPhysicalDriveIOCTL = 0;
// Try to get a handle to PhysicalDrive IOCTL, report failure
// and exit if can't.
char cszDriveName[256];
sprintf_s(cszDriveName, 256, "\\\\.\\PhysicalDrive%d", iDrive);
// Windows NT, Windows 2000, Windows Server 2003, Vista
hPhysicalDriveIOCTL = CreateFileA(cszDriveName,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
// if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
// printf ("Unable to open physical iDrive %d, error code: 0x%lX\n",
// iDrive, GetLastError ());
if (hPhysicalDriveIOCTL == INVALID_HANDLE_VALUE)
{
SecureZeroMemory(m_cszErrorMessage, sizeof(m_cszErrorMessage));
sprintf_s(m_cszErrorMessage, 256, "%d ReadPhysicalDriveInNTUsingSmart ERROR, CreateFileA(%s) returned INVALID_HANDLE_VALUE Error Code %d", __LINE__, cszDriveName, GetLastError());
}
else
{
GETVERSIONINPARAMS GetVersionParams;
DWORD dwBytesReturned = 0;
// Get the version, etc of PhysicalDrive IOCTL
memset((void*)& GetVersionParams, 0, sizeof(GetVersionParams));
if (!DeviceIoControl(hPhysicalDriveIOCTL, SMART_GET_VERSION,
NULL,
0,
&GetVersionParams, sizeof(GETVERSIONINPARAMS),
&dwBytesReturned, NULL))
{
DWORD dwErr = GetLastError();
SecureZeroMemory(m_cszErrorMessage, sizeof(m_cszErrorMessage));
sprintf_s(m_cszErrorMessage, 256, "\n%d ReadPhysicalDriveInNTUsingSmart ERROR DeviceIoControl(%d, SMART_GET_VERSION) returned 0, error is %d", __LINE__, (int)hPhysicalDriveIOCTL, (int)dwErr);
}
else
{
// Print the SMART version
// PrintVersion (& GetVersionParams);
// Allocate the command cszBuffer
ULONG CommandSize = sizeof(SENDCMDINPARAMS) + IDENTIFY_BUFFER_SIZE;
PSENDCMDINPARAMS Command = (PSENDCMDINPARAMS)malloc(CommandSize);
// Retrieve the IDENTIFY data
// Prepare the command
#define ID_CMD 0xEC // Returns ID sector for ATA
Command->irDriveRegs.bCommandReg = ID_CMD;
DWORD BytesReturned = 0;
if (!DeviceIoControl(hPhysicalDriveIOCTL,
SMART_RCV_DRIVE_DATA, Command, sizeof(SENDCMDINPARAMS),
Command, CommandSize,
&BytesReturned, NULL))
{
SecureZeroMemory(m_cszErrorMessage, sizeof(m_cszErrorMessage));
sprintf_s(m_cszErrorMessage, 256, "SMART_RCV_DRIVE_DATA IOCTL");
// Print the error
//PrintError ("SMART_RCV_DRIVE_DATA IOCTL", GetLastError());
}
else
{
// Print the IDENTIFY data
DWORD dwDiskData[256];
USHORT *punIdSector = (USHORT *)
(PIDENTIFY_DATA)((PSENDCMDOUTPARAMS)Command)->bBuffer;
for (int iIjk = 0; iIjk < 256; iIjk++)
dwDiskData[iIjk] = punIdSector[iIjk];
PrintIdeInfo(iDrive, dwDiskData);
iDone = TRUE;
}
// Done
CloseHandle(hPhysicalDriveIOCTL);
free(Command);
}
}
}
return iDone;
}
char * MasterHardDiskSerial::flipAndCodeBytes(int iPos, int iFlip, const char * pcszStr, char * pcszBuf)
{
int iI;
int iJ = 0;
int iK = 0;
pcszBuf[0] = '\0';
if (iPos <= 0)
读取硬盘序列号 自定义数据类,windows下可用
需积分: 2 153 浏览量
2023-09-04
14:12:39
上传
评论
收藏 8KB ZIP 举报
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
一切由态度决定
- 粉丝: 0
- 资源: 4
最新资源
- ParaStor300S 对象存储服务接口参考V1.0(V4.0.X).pdf
- ParaStor300S产品FAQ.doc
- SBM模型、超效率SBM模型代码(原始面板数据、MATLAB)
- 基于汇川AM系列PLC做的CNC加工
- ParaStor300S技术培训PPT-面向售前-V1.4.pptx
- 在MATLAB中,你可以使用多种函数和工具来创建各种类型的图形 以下是一些基本的MATLAB绘图命令和示例:
- 1_网络2204班 30 王磊 open flow 协议分析实践.docx
- 探索 v-on 之奥秘二:事件及各类修饰符的深度剖析与案例展示
- k8s基础教程与相关面试
- TerraScan中文用户指南
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)