代码改变世界

Win32 设备管理(1)

  Clingingboy  阅读(570)  评论(0编辑  收藏  举报

 

一.获得物理内存参数

使用GlobalMemoryStatus函数

复制代码
View Code
void CDemoDlg::OnTest() 
{
    CListBox
* pListBox = (CListBox*)GetDlgItem(IDC_LIST);
    pListBox
->ResetContent();

    
//获得物理内存参数
    MEMORYSTATUS MemoryStatus;
    GlobalMemoryStatus(
&MemoryStatus);

    CString strText 
= _T("");
    strText.Format(_T(
"物理内存使用率:%d%s"), 
        MemoryStatus.dwMemoryLoad, _T(
"%"));
    pListBox
->AddString(strText);
    strText.Format(_T(
"物理内存总数:  %dK"), 
        MemoryStatus.dwTotalPhys 
/ 1024);
    pListBox
->AddString(strText);
    strText.Format(_T(
"物理内存可用数:%dK"), 
        MemoryStatus.dwAvailPhys 
/ 1024);
    pListBox
->AddString(strText);
    strText.Format(_T(
"页文件总数:    %dK"), 
        MemoryStatus.dwTotalPageFile 
/ 1024);
    pListBox
->AddString(strText);
    strText.Format(_T(
"页文件用数:    %dK"), 
        MemoryStatus.dwAvailPageFile 
/ 1024);
    pListBox
->AddString(strText);
    strText.Format(_T(
"虚拟内存总数:  %dK"), 
        MemoryStatus.dwTotalVirtual 
/ 1024);
    pListBox
->AddString(strText);
    strText.Format(_T(
"虚拟内存可用数:%dK"), 
        MemoryStatus.dwAvailVirtual 
/ 1024);
    pListBox
->AddString(strText);
}
复制代码


二.获取驱动器

使用GetLogicalDrives方法,使用位掩码方式获得驱动器

 

复制代码
View Code
void CDemoDlg::OnTest() 
{
    CListCtrl
* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    pList
->DeleteAllItems();

    
//获得驱动器位掩码
    DWORD dwBitMask = ::GetLogicalDrives();
    
if (dwBitMask != 0)
    {
        
int n = 0;

        TCHAR ch 
= 'A';

        
while (dwBitMask > 0)
        {
            
if (dwBitMask % 2 == 1)
            {
                
//驱动器名
                CString strDiriveName = _T("");
                strDiriveName.Format(_T(
"%c:\\"), ch);
                pList
->InsertItem(n, strDiriveName);

                n
++;
            }

            dwBitMask 
/= 2;

            ch
++;
        }
    }
}
复制代码

 

三.获取驱动器类型

使用GetDriveType函数,输入参数如C:\,根据返回的值判别类型

复制代码
View Code
void CDemoDlg::OnTest() 
{
    CListCtrl
* pList = (CListCtrl*)GetDlgItem(IDC_LIST);
    pList
->DeleteAllItems();

    
//获得驱动器位掩码
    DWORD dwBitMask = ::GetLogicalDrives();
    
if (dwBitMask != 0)
    {
        
int n = 0;

        TCHAR ch 
= 'A';

        
while (dwBitMask > 0)
        {
            
if (dwBitMask % 2 == 1)
            {
                
//驱动器名
                CString strDiriveName = _T("");
                strDiriveName.Format(_T(
"%c:\\"), ch);
                pList
->InsertItem(n, strDiriveName);

                
//获得驱动器类型
                UINT nDriveType = GetDriveType(strDiriveName);
                CString strDiriveType 
= _T("");
                
if (nDriveType == DRIVE_UNKNOWN)
                {
                    strDiriveType 
= _T("未知");
                }
                
else if (nDriveType == DRIVE_NO_ROOT_DIR)
                {
                    strDiriveType 
= _T("无效路径");
                }
                
else if (nDriveType == DRIVE_REMOVABLE)
                {
                    strDiriveType 
= _T("可移动驱动器");
                }
                
else if (nDriveType == DRIVE_FIXED)
                {
                    strDiriveType 
= _T("固定驱动器");
                }
                
else if (nDriveType == DRIVE_REMOTE)
                {
                    strDiriveType 
= _T("远程(网络)驱动器");
                }
                
else if (nDriveType == DRIVE_CDROM)
                {
                    strDiriveType 
= _T("CDROM驱动器");
                }
                
else if (nDriveType == DRIVE_RAMDISK)
                {
                    strDiriveType 
= _T("RAM磁盘");
                }
                pList
->SetItemText(n, 1, strDiriveType);

                n
++;
            }

            dwBitMask 
/= 2;

            ch
++;
        }
    }
}
复制代码

 

四.获得驱动器卷标

使用GetVolumeInformation函数,即硬盘分区的名字(卷标)

 

复制代码
View Code
                //驱动器名
                CString strDiriveName = _T("");
                strDiriveName.Format(_T(
"%c:\\"), ch);
                pList
->InsertItem(n, strDiriveName);

                
//获得驱动器卷标
                CString strVolumeName = _T("");
                GetVolumeInformation(strDiriveName, strVolumeName.GetBuffer(
1024),
                    
1024, NULL, NULL, NULL, NULL, 0);
                strVolumeName.ReleaseBuffer();
                pList
->SetItemText(n, 1, strVolumeName);
复制代码

 

五.设置驱动器卷标

 使用SetVolumeLabel函数,需要管理员权限

 

View Code
    //设置驱动器卷标
    SetVolumeLabel(_T("C:\\"), _T("系统盘"));

 

 六.获得驱动器序列号

还是使用GetVolumeInformation函数,第4个参数为输出结果,第一个参数是必须的

 

                //获得驱动器序列号
                DWORD dwSerialNumber = 0;
                GetVolumeInformation(strDiriveName, NULL,
                    NULL, 
&dwSerialNumber, NULL, NULL, NULL, 0);

 

七.获得驱动器文件系统

用GetVolumeInformation函数,最后两个参数填充,即获取装载文件系统的名称(如FAT,NTFS以及其他)

 

                //获得驱动器文件系统
                CString strFileSystemName = _T("");
                GetVolumeInformation(strDiriveName, NULL, 
0, NULL, NULL, NULL, 
                    strFileSystemName.GetBuffer(
1024), 1024);
                strFileSystemName.ReleaseBuffer();
                pList
->SetItemText(n, 1, strFileSystemName);

 

八.获取驱动器磁盘空间信息

使用GetDiskFreeSpaceEx函数

 

复制代码
                //获得磁盘空间信息
                ULARGE_INTEGER FreeBytes;
                ULARGE_INTEGER TotalBytes;
                
if (!GetDiskFreeSpaceEx(strDiriveName, &FreeBytes,
                    
&TotalBytes, NULL))
                {
                    FreeBytes.QuadPart
=0;
                    TotalBytes.QuadPart
=0;
                }

                UINT nFreeSize 
= (UINT)(FreeBytes.QuadPart / 1024 / 1024);
                UINT nTotalSize 
= (UINT)(TotalBytes.QuadPart / 1024 / 1024);

                CString strText 
= _T("");
                strText.Format(_T(
"%d MB"), nFreeSize);
                pList
->SetItemText(n, 1, strText);
                strText.Format(_T(
"%d MB"),nTotalSize);
                pList
->SetItemText(n, 2, strText);
复制代码

 

 

 

编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2007-04-02 Discuz!NT v1.0 正式版发布
点击右上角即可分享
微信分享提示