[VC]获取驱动器序列号

获取所有驱动器序列号并加入CComboBox控件中:

     CComboBox* Driver=(CComboBox*)GetDlgItem(IDC_DRIVER);< ?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

     DWORD dwNumBytesForDriveStrings;//实际存储驱动器号的字符串长度

     HANDLE hHeap;

     LPSTR lp;

     CString strLogdrive;

     //获得实际存储驱动器号的字符串长度

     dwNumBytesForDriveStrings=GetLogicalDriveStrings(0,NULL)*sizeof(TCHAR);

     //如果字符串不为空,则表示有正常的驱动器存在

     if (dwNumBytesForDriveStrings!=0) {

         //分配字符串空间

         hHeap=GetProcessHeap();

         lp=(LPSTR)HeapAlloc(hHeap,HEAP_ZERO_MEMORY,

              dwNumBytesForDriveStrings);

         //获得标明所有驱动器的字符串

         GetLogicalDriveStrings(HeapSize(hHeap,0,lp),lp);

         //将驱动器一个个放到下拉框中

         while (*lp!=0) {

              Driver->AddString(lp);

              lp=_tcschr(lp,0)+1;

         }

     }

     else

         AfxMessageBox("Can't Use The Function GetLogicalDriveStrings!");

获取指定驱动器的序列号

     //获得驱动器序列号

     LPCTSTR lpRootPathName = _T("C:\\");

     LPTSTR lpVolumeNameBuffer=new char[12];

     DWORD nVolumeNameSize=12;

     DWORD VolumeSerialNumber;

     DWORD MaximumComponentLength;

     DWORD FileSystemFlags;

     LPTSTR lpFileSystemNameBuffer=new char[10];

     DWORD nFileSystemNameSize=10;

    

     GetVolumeInformationA(lpRootPathName,

                   lpVolumeNameBuffer, nVolumeNameSize,

                   &VolumeSerialNumber,

                   &MaximumComponentLength,

                   &FileSystemFlags,

                   lpFileSystemNameBuffer, nFileSystemNameSize);

 

     //显示驱动器序列号

     CString str;

     str.Format("驱动器%s的序列号为%x",strRootPathName,VolumeSerialNumber);

     AfxMessageBox(str);
vs.net 2005下调试通过
(摘自VC实效编程第二版)


如果给你带来帮助,欢迎微信或支付宝扫一扫,赞一下。