Win32 设备管理(1)

 

一.获得物理内存参数

使用GlobalMemoryStatus函数

ExpandedBlockStart.gif 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方法,使用位掩码方式获得驱动器

 

ExpandedBlockStart.gif 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:\,根据返回的值判别类型

ExpandedBlockStart.gif 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函数,即硬盘分区的名字(卷标)

 

ExpandedBlockStart.gif 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函数,需要管理员权限

 

ExpandedBlockStart.gif 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);

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值