获取硬盘序列号代码

void CDrvSrlNmbrDlg::OnGetSrlNum() { //更新下拉框的驱动器号 UpdateData(TRUE);

//获得下拉框的指针对象
CComboBox* Driver=(CComboBox*)GetDlgItem(IDC_DRIVER);

//获得当前选择的驱动器
CString strRootPathName;
Driver->GetWindowText(strRootPathName);

//获得驱动器序列号
LPCTSTR lpRootPathName = strRootPathName;
LPTSTR lpVolumeNameBuffer=new char[12];
DWORD nVolumeNameSize=12;
DWORD VolumeSerialNumber;
DWORD MaximumComponentLength;
DWORD FileSystemFlags;
LPTSTR lpFileSystemNameBuffer=new char[10];
DWORD nFileSystemNameSize=10;
GetVolumeInformation(lpRootPathName,
            lpVolumeNameBuffer, nVolumeNameSize,
            &VolumeSerialNumber;,
            &MaximumComponentLength;,
            &FileSystemFlags;,
            lpFileSystemNameBuffer, nFileSystemNameSize);

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

}

void CDrvSrlNmbrDlg::FindAllDrivers() { CComboBox Driver=(CComboBox)GetDlgItem(IDC_DRIVER); 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!");

}

支持原创技术分享,据说打赏我的人,都找到了女朋友!