• c++获取硬盘序列号


    转载:https://www.cnblogs.com/qq76211822/p/4712033.html

      1 #include<iostream>
      2 #include <windows.h>  
      3 #include <winioctl.h>  
      4 #include<tchar.h>
      5 
      6 using namespace std;
      7 
      8 //  
      9 BOOL GetPhyDriveSerial(LPTSTR pModelNo, LPTSTR pSerialNo);
     10 void ToLittleEndian(PUSHORT pWords, int nFirstIndex, int nLastIndex, LPTSTR pBuf);
     11 void TrimStart(LPTSTR pBuf);
     12 
     13 //把WORD数组调整字节序为little-endian,并滤除字符串结尾的空格。  
     14 void ToLittleEndian(PUSHORT pWords, int nFirstIndex, int nLastIndex, LPTSTR pBuf)
     15 {
     16     int index;
     17     LPTSTR pDest = pBuf;
     18     for (index = nFirstIndex; index <= nLastIndex; ++index)
     19     {
     20         pDest[0] = pWords[index] >> 8;
     21         pDest[1] = pWords[index] & 0xFF;
     22         pDest += 2;
     23     }
     24     *pDest = 0;
     25 
     26     //trim space at the endof string; 0x20: _T(' ')  
     27     --pDest;
     28     while (*pDest == 0x20)
     29     {
     30         *pDest = 0;
     31         --pDest;
     32     }
     33 }
     34 
     35 BOOL GetPhyDriveSerial(LPTSTR pModelNo, LPTSTR pSerialNo)
     36 {
     37     //-1是因为 SENDCMDOUTPARAMS 的结尾是 BYTE bBuffer[1];  
     38     BYTE IdentifyResult[sizeof(SENDCMDOUTPARAMS)+IDENTIFY_BUFFER_SIZE - 1];
     39     DWORD dwBytesReturned;
     40     GETVERSIONINPARAMS get_version;
     41     SENDCMDINPARAMS send_cmd = { 0 };
     42 
     43     HANDLE hFile = CreateFile(_T("\\.\PHYSICALDRIVE0"), GENERIC_READ | GENERIC_WRITE,
     44         FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
     45     if (hFile == INVALID_HANDLE_VALUE)
     46         return FALSE;
     47 
     48     //get version  
     49     DeviceIoControl(hFile, SMART_GET_VERSION, NULL, 0,
     50         &get_version, sizeof(get_version), &dwBytesReturned, NULL);
     51 
     52     //identify device  
     53     send_cmd.irDriveRegs.bCommandReg = (get_version.bIDEDeviceMap & 0x10) ? ATAPI_ID_CMD : ID_CMD;
     54     DeviceIoControl(hFile, SMART_RCV_DRIVE_DATA, &send_cmd, sizeof(SENDCMDINPARAMS)-1,
     55         IdentifyResult, sizeof(IdentifyResult), &dwBytesReturned, NULL);
     56     CloseHandle(hFile);
     57 
     58     //adjust the byte order  
     59     PUSHORT pWords = (USHORT*)(((SENDCMDOUTPARAMS*)IdentifyResult)->bBuffer);
     60     ToLittleEndian(pWords, 27, 46, pModelNo);
     61     ToLittleEndian(pWords, 10, 19, pSerialNo);
     62     return TRUE;
     63 }
     64 
     65 //滤除字符串起始位置的空格  
     66 void TrimStart(LPTSTR pBuf)
     67 {
     68     if (*pBuf != 0x20)
     69         return;
     70 
     71     LPTSTR pDest = pBuf;
     72     LPTSTR pSrc = pBuf + 1;
     73     while (*pSrc == 0x20)
     74         ++pSrc;
     75 
     76     while (*pSrc)
     77     {
     78         *pDest = *pSrc;
     79         ++pDest;
     80         ++pSrc;
     81     }
     82     *pDest = 0;
     83 }
     84 
     85 int main()
     86 {
     87     TCHAR szModelNo[48], szSerialNo[24];
     88     if (GetPhyDriveSerial(szModelNo, szSerialNo))
     89     {
     90         _tprintf(_T("Model No: %s
    "), szModelNo);
     91         TrimStart(szSerialNo);
     92         _tprintf(_T("Serial No: %s
    "), szSerialNo);
     93     }
     94     else
     95     {
     96         _tprintf(_T("Failed.
    "));
     97     }
     98     system("pause");
     99     return 0;
    100 }
  • 相关阅读:
    spring前两天
    代理模式
    学习spring第三天
    Spring集成MyBatis配置文件
    学习spring第二天
    学习spring第一天
    Bind关闭递归查询
    织梦DedeCMS v5.7 实现导航条下拉菜单
    Debian下修改系统时间的方式
    js禁止页面复制功能禁用页面右键菜单示例代码
  • 原文地址:https://www.cnblogs.com/Toya/p/12361935.html
Copyright © 2020-2023  润新知