zoukankan      html  css  js  c++  java
  • Windows ,获取硬盘物理序列号(VC++)

    #include <windows.h>

    BOOL GetHDID(PCHAR pIDBufer)


        HANDLE hDevice=NULL;
        hDevice=::CreateFileA("\\.\PhysicalDrive0",GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
        if(!hDevice)

      {

           hDevice=::CreateFileA("\\.\Scsi0",GENERIC_READ |GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);

      }

        if(!hDevice)

      {

        return FALSE;

      }

        DWORD dwBytesReturned=0;
        GETVERSIONINPARAMS gVersionParsams;
        ZeroMemory(&gVersionParsams,sizeof(GETVERSIONINPARAMS));


        if(!DeviceIoControl(hDevice,SMART_GET_VERSION,NULL,NULL,&gVersionParsams,sizeof(GETVERSIONINPARAMS),&dwBytesReturned, NULL)
            || dwBytesReturned==0 || gVersionParsams.bIDEDeviceMap <= 0)
        {
            ::CloseHandle(hDevice);
            return FALSE;
        }
        
        SENDCMDINPARAMS scip;

      ZeroMemory( &scip,sizeof(SENDCMDINPARAMS)); 
        scip.cBufferSize=IDENTIFY_BUFFER_SIZE;
        scip.irDriveRegs.bSectorCountReg=1;
        scip.irDriveRegs.bSectorNumberReg=1;
        scip.irDriveRegs.bDriveHeadReg=0xA0;
        scip.irDriveRegs.bCommandReg=0xEC;

        BYTE btBuffer[1024]={ 0 };

        if(!DeviceIoControl(hDevice,SMART_RCV_DRIVE_DATA,&scip,sizeof(SENDCMDINPARAMS),
            btBuffer,1024,&dwBytesReturned,NULL))
        {
            ::CloseHandle(hDevice);
            return FALSE;
        }
        
        int nPos=0x24;            //序列号的开始位置,具体请参考SENDCMDOUTPARAMS与IDSECTOR结构
       
     while(btBuffer[nPos]<128)
        
    {

            *pIDBufer=btBuffer[nPos++];
            pIDBufer++;
        }
        
    *pIDBufer=00;
       
     return TRUE;
    }

    int _tmain(int argc, _TCHAR* argv[])
    {

       CHAR szHDID[256];
       if(GetHDID(szHDID))

       {

          printf("硬盘序列号:%s ",szHDID);

      }  

       else

       {

        printf("取硬盘序列号失败");

       }

       getch();

       return 0;
    }

  • 相关阅读:
    Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
    分布式架构2:Nginx下Session存储
    分布式架构3:MySQL集群(Mysql Cluster7.5.5)
    分布式架构1:Nginx实现负载均衡
    Oracle 触发器记录
    [Deprecation Notice] 本博客弃用, 迁移至新博客
    KM算法 O(n^3)最大权完美匹配
    Python 学习笔记(附 Pytorch)
    你一定看的懂的:vlan与交换机端口的三种模式access,trunk和hybrid
    Git代码回滚
  • 原文地址:https://www.cnblogs.com/luisfan/p/5641020.html
Copyright © 2011-2022 走看看