关闭

C++读取硬盘的序列号

标签: c++bytestructidecommandstructure
1375人阅读 评论(1) 收藏 举报
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#define DFP_GET_VERSION 0x00074080
#define DFP_SEND_DRIVE_COMMAND 0x0007c084
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088
#pragma pack(1)

 

void ChangeByteOrder(PCHAR szString, USHORT uscStrSize);

typedef struct _GETVERSIONOUTPARAMS {
BYTE bVersion; // Binary driver version.
BYTE bRevision; // Binary driver revision.
BYTE bReserved; // Not used.
BYTE bIDEDeviceMap; // Bit map of IDE devices.
DWORD fCapabilities; // Bit mask of driver capabilities.
DWORD dwReserved[4]; // For future use.
} GETVERSIONOUTPARAMS, *PGETVERSIONOUTPARAMS, *LPGETVERSIONOUTPARAMS;


typedef struct _IDEREGS {
BYTE bFeaturesReg; // Used for specifying SMART "commands".
BYTE bSectorCountReg; // IDE sector count register
BYTE bSectorNumberReg; // IDE sector number register
BYTE bCylLowReg; // IDE low order cylinder value
BYTE bCylHighReg; // IDE high order cylinder value
BYTE bDriveHeadReg; // IDE drive/head register
BYTE bCommandReg; // Actual IDE command.
BYTE bReserved; // reserved for future use. Must be zero.
} IDEREGS, *PIDEREGS, *LPIDEREGS;


typedef struct _SENDCMDINPARAMS {
DWORD cBufferSize; // Buffer size in bytes
IDEREGS irDriveRegs; // Structure with drive register values.
BYTE bDriveNumber; // Physical drive number to send
// command to (0,1,2,3).
BYTE bReserved[3]; // Reserved for future expansion.
DWORD dwReserved[4]; // For future use.
//BYTE bBuffer[1]; // Input buffer.
} SENDCMDINPARAMS, *PSENDCMDINPARAMS, *LPSENDCMDINPARAMS;


typedef struct _DRIVERSTATUS {
BYTE bDriverError; // Error code from driver,
// or 0 if no error.
BYTE bIDEStatus; // Contents of IDE Error register.
// Only valid when bDriverError
// is SMART_IDE_ERROR.
BYTE bReserved[2]; // Reserved for future expansion.
DWORD dwReserved[2]; // Reserved for future expansion.
} DRIVERSTATUS, *PDRIVERSTATUS, *LPDRIVERSTATUS;


typedef struct _SENDCMDOUTPARAMS {
DWORD cBufferSize; // Size of bBuffer in bytes
DRIVERSTATUS DriverStatus; // Driver status structure.
BYTE bBuffer[512]; // Buffer of arbitrary length
// in which to store the data read from the drive.
} SENDCMDOUTPARAMS, *PSENDCMDOUTPARAMS, *LPSENDCMDOUTPARAMS;


typedef struct _IDSECTOR {
USHORT wGenConfig;
USHORT wNumCyls;
USHORT wReserved;
USHORT wNumHeads;
USHORT wBytesPerTrack;
USHORT wBytesPerSector;
USHORT wSectorsPerTrack;
USHORT wVendorUnique[3];
CHAR sSerialNumber[20];
USHORT wBufferType;
USHORT wBufferSize;
USHORT wECCSize;
CHAR sFirmwareRev[8];
CHAR sModelNumber[40];
USHORT wMoreVendorUnique;
USHORT wDoubleWordIO;
USHORT wCapabilities;
USHORT wReserved1;
USHORT wPIOTiming;
USHORT wDMATiming;
USHORT wBS;
USHORT wNumCurrentCyls;
USHORT wNumCurrentHeads;
USHORT wNumCurrentSectorsPerTrack;
ULONG ulCurrentSectorCapacity;
USHORT wMultSectorStuff;
ULONG ulTotalAddressableSectors;
USHORT wSingleWordDMA;
USHORT wMultiWordDMA;
BYTE bReserved[128];
} IDSECTOR, *PIDSECTOR;

 

 

 

 


CString HDSNNT()
{
 
GETVERSIONOUTPARAMS vers;
SENDCMDINPARAMS in;
SENDCMDOUTPARAMS out;
HANDLE h;
DWORD i;
BYTE j;
char hd[80];
PIDSECTOR phdinfo;
char s[41];
int i_=0;


ZeroMemory(&vers,sizeof(vers));
//We start in NT/Win2000

for (j=0;j<4;j++)
{

//
sprintf(hd,"////.//PhysicalDrive%d",j);


h=CreateFile(hd,
    GENERIC_READ|GENERIC_WRITE,//&brvbar;&brvbar;
             FILE_SHARE_READ|FILE_SHARE_WRITE,
    0,
    OPEN_EXISTING,
    0,
    0);

if (!h)
{
continue;
}
//发送信号
if
(!DeviceIoControl(h,DFP_GET_VERSION,0,0,&vers,sizeof(vers),&i,0))
{
CloseHandle(h);
continue;
}
if (!(vers.fCapabilities&1))
{
CloseHandle(h);
}


ZeroMemory(&in,sizeof(in));
ZeroMemory(&out,sizeof(out));

if (j&1)
{
in.irDriveRegs.bDriveHeadReg=0xb0;
}
else
{
in.irDriveRegs.bDriveHeadReg=0xa0;
}

if (vers.fCapabilities&(16>>j))
{
continue;
}
else
{
in.irDriveRegs.bCommandReg=0xec;
}

in.bDriveNumber=j;
in.irDriveRegs.bSectorCountReg=1;
in.irDriveRegs.bSectorNumberReg=1;
in.cBufferSize=512;

if(!DeviceIoControl(h,
   DFP_RECEIVE_DRIVE_DATA,
   &in,
   sizeof(in),
   &out,
   sizeof(out),
   &i,
   0))
{
CloseHandle(h);
return "";
}
phdinfo=(PIDSECTOR)out.bBuffer;
memcpy(s,phdinfo->sSerialNumber,20);
s[20]=0;
ChangeByteOrder(s,20);
//显示硬盘序列号
}
CloseHandle(h);
return s;

}

void ChangeByteOrder(PCHAR szString, USHORT uscStrSize)
{

USHORT i;
CHAR temp;

for (i = 0; i < uscStrSize; i+=2)
{
temp = szString[i];
szString[i] = szString[i+1];
szString[i+1] = temp;
}
}

 

 

 

 

    CString sss=HDSNNT();
 sss+="07";
 sss.TrimLeft();
  CString code[16]={"ad","eh","im","np","ru","vy","zc","gk",
 "pt","xb","fj","ox","wa","ei","nr","qu"};
 CString reg,stred;
 stred=sss;
 int num;
 stred.MakeLower();
 for(int i=0;i<10;i++)
 {
  char p=stred.GetAt(i);
  if(p>=48&&p<=57)
   num=p-48;
  else if(p>=97&&p<=109)
   num=p-97;
  else
   num=p-110;
  CString tmp=code[num];
  reg+=tmp; 
 }
 reg.MakeUpper();
 CString key=reg;
 CString nkey,key1,key2,key3,key4;
 GetDlgItem(IDC_EDIT1)->GetWindowText(key1);
 GetDlgItem(IDC_EDIT2)->GetWindowText(key2);
 GetDlgItem(IDC_EDIT3)->GetWindowText(key3);
 GetDlgItem(IDC_EDIT4)->GetWindowText(key4);
 nkey=key1+key2+key3+key4;
 
 if(nkey==key)
 {
  
 }
 else
 {
  MessageBox("你输入的验证码有错,请从新在输入");
 }

 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:18212次
    • 积分:187
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:13篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论