松鼠的博客

导航

统计

获取硬盘序列号代码

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!");
}

posted on   Xproer-松鼠  阅读(465)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示