代码改变世界

Win32 设备管理(1)

2011-04-02 13:56  Clingingboy  阅读(561)  评论(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);