wyq521003
驱动小牛
驱动小牛
  • 注册日期2005-05-28
  • 最后登录2008-11-19
  • 粉丝0
  • 关注0
  • 积分406分
  • 威望53点
  • 贡献值10点
  • 好评度26点
  • 原创分0分
  • 专家分0分
阅读:4320回复:4

怎么可以得到硬盘物理序列号?

楼主#
更多 发布于:2005-07-21 16:14
  从网上搜了很久,搜来搜去的也就是一个实例,而且也是不能执行的,我是新手,本来就看不懂,不能只行就更看不懂了。所以希望大侠能不吝赐教。 [p:3][p:3]
付出多少就会收获多少
wyq521003
驱动小牛
驱动小牛
  • 注册日期2005-05-28
  • 最后登录2008-11-19
  • 粉丝0
  • 关注0
  • 积分406分
  • 威望53点
  • 贡献值10点
  • 好评度26点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2005-07-21 16:15
这是什么呀,狂笑的动作怎么是这样的呀,我现在可不敢笑傲江湖呀。千万不要误会。我不知道狂笑是这个意思
付出多少就会收获多少
wowocock
VIP专家组
VIP专家组
  • 注册日期2002-04-08
  • 最后登录2016-01-09
  • 粉丝16
  • 关注2
  • 积分601分
  • 威望1651点
  • 贡献值1点
  • 好评度1227点
  • 原创分1分
  • 专家分0分
板凳#
发布于:2005-07-25 09:21
直接从RING3获取硬盘序列号
作者:陆麟
转载请征得作者同意.
2000.11.3
PATCHED 11.19


--------------------------------------------------------------------------------

通常情况下,我们通过0XEC命令对IDE端口进行监测.获取硬盘信息.
一般情况下,我们就写个VXD或者DRIVER来完成.但是现在,通过MS的S.M.A.R.T.接口,我们可以直接从RING3调用API DeviceIoControl()来获取硬盘信息.下面乃是我的例程:
另外,也有编译好的版本供大家平时使用.欢迎下载.
*注:在WIN98SE,WINDOWS ME中,S.M.A.R.T并不缺省安装.请将SMARTVSD.VXD拷贝到%SYSTEM%\IOSUBSYS目录下.
在WINDOWS2000下,由于非ADMINISTRATORS组的用户对硬盘连GENERIC_READ的权限也没有,所以请以ADMINISTRATOR登录后使用.
/*+++
HDID.CPP
Written by Lu Lin
http://lu0.126.com
2000.11.3
---*/
#include <windows.h>
#include <iostream.h>
#include <stdio.h>

#define DFP_GET_VERSION 0x00074080
#define DFP_SEND_DRIVE_COMMAND 0x0007c084
#define DFP_RECEIVE_DRIVE_DATA 0x0007c088

#pragma pack(1)
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;

/*+++
Global vars
---*/
GETVERSIONOUTPARAMS vers;
SENDCMDINPARAMS in;
SENDCMDOUTPARAMS out;
HANDLE h;
DWORD i;
BYTE j;

void CopyRight(){
 cerr<<endl<<"HDD identifier v1.0 for WIN95/98/Me/NT/2000. written by Lu Lin"<<endl;
 cerr<<"For more information, please visit Inside Programming: http://lu0.126.com"<<endl;
 cerr<<"2000.11.3"<<endl<<endl;
}
VOID ChangeByteOrder(PCHAR szString, USHORT uscStrSize)
{

USHORT i;
CHAR temp;

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

void DetectIDE(BYTE bIDEDeviceMap){
 if (bIDEDeviceMap&1){
  if (bIDEDeviceMap&16){
   cout<<"ATAPI device is attached to primary controller, drive 0."<<endl;
  }else{
   cout<<"IDE device is attached to primary controller, drive 0."<<endl;
  }
 }
 if (bIDEDeviceMap&2){
  if (bIDEDeviceMap&32){
   cout<<"ATAPI device is attached to primary controller, drive 1."<<endl;
  }else{
   cout<<"IDE device is attached to primary controller, drive 1."<<endl;
  }
 }
 if (bIDEDeviceMap&4){
  if (bIDEDeviceMap&64){
   cout<<"ATAPI device is attached to secondary controller, drive 0."<<endl;
  }else{
   cout<<"IDE device is attached to secondary controller, drive 0."<<endl;
  }
 }
 if (bIDEDeviceMap&8){
  if (bIDEDeviceMap&128){
   cout<<"ATAPI device is attached to secondary controller, drive 1."<<endl;
  }else{
   cout<<"IDE device is attached to secondary controller, drive 1."<<endl;
  }
 }
}

void hdid9x(){
 ZeroMemory(&vers,sizeof(vers));
 //We start in 95/98/Me
 h=CreateFile("\\\\.\\Smartvsd",0,0,0,CREATE_NEW,0,0);
 if (!h){
  cout<<"open smartvsd.vxd failed"<<endl;
  exit(0);
 }

 if (!DeviceIoControl(h,DFP_GET_VERSION,0,0,&vers,sizeof(vers),&i,0)){
  cout<<"DeviceIoControl failed:DFP_GET_VERSION"<<endl;
  CloseHandle(h);
  return;
 }
 //If IDE identify command not supported, fails
 if (!(vers.fCapabilities&1)){
  cout<<"Error: IDE identify command not supported.";
  CloseHandle(h);
  return;
 }
 //Display IDE drive number detected
 DetectIDE(vers.bIDEDeviceMap);
 //Identify the IDE drives
 for (j=0;j<4;j++){
  PIDSECTOR phdinfo;
  char s[41];

  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)){
   //We don't detect a ATAPI device.
   cout<<"Drive "<<(int)(j+1)<<" is a ATAPI device, we don't detect it"<<endl;
   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)){
   cout<<"DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"<<endl;
   CloseHandle(h);
   return;
  }
  phdinfo=(PIDSECTOR)out.bBuffer;
  memcpy(s,phdinfo->sModelNumber,40);
  s[40]=0;
  ChangeByteOrder(s,40);
  cout<<endl<<"Module Number:"<<s<<endl;
  memcpy(s,phdinfo->sFirmwareRev,8);
  s[8]=0;
  ChangeByteOrder(s,8);
  cout<<"\tFirmware rev:"<<s<<endl;
  memcpy(s,phdinfo->sSerialNumber,20);
  s[20]=0;
  ChangeByteOrder(s,20);
  cout<<"\tSerial Number:"<<s<<endl;
  cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
 }

 //Close handle before quit
 CloseHandle(h);
 CopyRight();

}

void hdidnt(){
 char hd[80];
 PIDSECTOR phdinfo;
 char s[41];

 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,
   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 IDE identify command not supported, fails
  if (!(vers.fCapabilities&1)){
   cout<<"Error: IDE identify command not supported.";
   CloseHandle(h);
   return;
  }
  //Identify the IDE drives
  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)){
   //We don't detect a ATAPI device.
   cout<<"Drive "<<(int)(j+1)<<" is a ATAPI device, we don't detect it"<<endl;
   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)){
   cout<<"DeviceIoControl failed:DFP_RECEIVE_DRIVE_DATA"<<endl;
   CloseHandle(h);
   return;
  }
  phdinfo=(PIDSECTOR)out.bBuffer;
  memcpy(s,phdinfo->sModelNumber,40);
  s[40]=0;
  ChangeByteOrder(s,40);
  cout<<endl<<"Module Number:"<<s<<endl;
  memcpy(s,phdinfo->sFirmwareRev,8);
  s[8]=0;
  ChangeByteOrder(s,8);
  cout<<"\tFirmware rev:"<<s<<endl;
  memcpy(s,phdinfo->sSerialNumber,20);
  s[20]=0;
  ChangeByteOrder(s,20);
  cout<<"\tSerial Number:"<<s<<endl;
  cout<<"\tCapacity:"<<phdinfo->ulTotalAddressableSectors/2/1024<<"M"<<endl<<endl;
  CloseHandle(h);
 }
 CopyRight();
}

void main(){
 OSVERSIONINFO VersionInfo;

 ZeroMemory(&VersionInfo,sizeof(VersionInfo));
 VersionInfo.dwOSVersionInfoSize=sizeof(VersionInfo);
 GetVersionEx(&VersionInfo);

 switch (VersionInfo.dwPlatformId){
 case VER_PLATFORM_WIN32s:
  cout<<"Win32s is not supported by this programm."<<endl;
  return;
 case VER_PLATFORM_WIN32_WINDOWS:
  hdid9x();
  return;
 case VER_PLATFORM_WIN32_NT:
  hdidnt();
  return;
 }
}
花开了,然后又会凋零,星星是璀璨的,可那光芒也会消失。在这样 一瞬间,人降生了,笑者,哭着,战斗,伤害,喜悦,悲伤憎恶,爱。一切都只是刹那间的邂逅,而最后都要归入死亡的永眠
wyq521003
驱动小牛
驱动小牛
  • 注册日期2005-05-28
  • 最后登录2008-11-19
  • 粉丝0
  • 关注0
  • 积分406分
  • 威望53点
  • 贡献值10点
  • 好评度26点
  • 原创分0分
  • 专家分0分
地板#
发布于:2005-07-25 14:56
谢谢你的支持
付出多少就会收获多少
qiuwenqiao
驱动牛犊
驱动牛犊
  • 注册日期2005-07-15
  • 最后登录2016-01-09
  • 粉丝2
  • 关注0
  • 积分4分
  • 威望29点
  • 贡献值0点
  • 好评度12点
  • 原创分0分
  • 专家分0分
地下室#
发布于:2007-02-02 13:00
可惜此代码无法在Users权限下工作
游客

返回顶部