windows获取内存和硬盘使用百分比

// 获得系统CPU使用率  
// http://blog.youkuaiyun.com/morewindows/article/details/8678359  
// By MoreWindows( http://blog.youkuaiyun.com/MoreWindows )  
// 先调用Initialize(),然后while(true){Sleep(1000);GetCPUUseRate();}就能获得CPU使用率。  
// 经过测试,可以在WinXP及Win7下使用。  
class CCPUUseRate  
{  
public:  
    BOOL Initialize()   
    {  
        FILETIME ftIdle, ftKernel, ftUser;  
        BOOL flag = FALSE;  
        if (flag = GetSystemTimes(&ftIdle, &ftKernel, &ftUser))  
        {  
            m_fOldCPUIdleTime = FileTimeToDouble(ftIdle);  
            m_fOldCPUKernelTime = FileTimeToDouble(ftKernel);  
            m_fOldCPUUserTime = FileTimeToDouble(ftUser);  
  
        }  
        return flag;  
    }  
    //调用Initialize后要等待1秒再调用此函数  
    int GetCPUUseRate()  
    {  
        int nCPUUseRate = -1;  
        FILETIME ftIdle, ftKernel, ftUser;  
        if (GetSystemTimes(&ftIdle, &ftKernel, &ftUser))  
        {  
            double fCPUIdleTime = FileTimeToDouble(ftIdle);  
            double fCPUKernelTime = FileTimeToDouble(ftKernel);  
            double fCPUUserTime = FileTimeToDouble(ftUser);  
            nCPUUseRate= (int)(100.0 - (fCPUIdleTime - m_fOldCPUIdleTime)   
                / (fCPUKernelTime - m_fOldCPUKernelTime + fCPUUserTime - m_fOldCPUUserTime)   
                *100.0);  
            m_fOldCPUIdleTime = fCPUIdleTime;  
            m_fOldCPUKernelTime = fCPUKernelTime;  
            m_fOldCPUUserTime = fCPUUserTime;  
        }  
        return nCPUUseRate;  
    }  
private:  
    double FileTimeToDouble(FILETIME &filetime)  
    {  
        return (double)(filetime.dwHighDateTime * 4.294967296E9) + (double)filetime.dwLowDateTime;  
    }  
private:  
    double m_fOldCPUIdleTime;  
    double m_fOldCPUKernelTime;  
    double m_fOldCPUUserTime;  
}; 

CCPUUseRate cpuUseRate;  
    if (!cpuUseRate.Initialize())  
    {  
        printf("Error! %d\n", GetLastError());  
        getch();  
        return -1;  
    }  
    else  
    { 
            Sleep(1000);  
            printf("\r当前CPU使用率为:%4d%%", cpuUseRate.GetCPUUseRate());  
    } 



DWORD getWin_Mem_Usage()

{
        MEMORYSTATUSEX msEx;
        msEx.dwLength = sizeof(msEx);
        ::GlobalMemoryStatusEx(&msEx);
        DWORD dAP = (DWORD)(msEx.ullAvailPhys/(1024*1024));
        return msEx.dwMemoryLoad;

}


////////////////////////////////////////////////////////////////////////////////////////////////////////


int getWin_Disk_Size(char*& pDiskUsageInfoBuf)
{
    int DiskCount = 0;
    DWORD DiskInfo = GetLogicalDrives();
    //获取逻辑驱动器数量
    while (DiskInfo)//循环,查看每一位数据是否为1,如果是1则磁盘为真,如果为0则磁盘不存在 
    {
        if (DiskInfo & 1)//通过位运算的逻辑与操作,判断是否为1
        {
            ++DiskCount;
        }
        DiskInfo = DiskInfo >> 1;//位运算右移操作,查看下一个
        //DiskInfo = DiskInfo/2;   
    }
    printf("Logical Disk Number:%d \n", DiskCount);
    //-----------------------------------------------------------------------------------------
    int DSLength = GetLogicalDriveStrings(0, NULL);
    //获取所有驱动器字符串信息长度
    char* DStr = new char[DSLength]; //用获取的长度创建字符串数组
    GetLogicalDriveStrings(DSLength, (LPTSTR)DStr);
    //将字符串信息复制到数组中  
 
    int DType;
    int si = 0;
    BOOL fResult;
    unsigned _int64 i64FreeBytesToCaller;
    unsigned _int64 i64TotalBytes;
    unsigned _int64 i64FreeBytes;
 
    for (int i = 0; i<DSLength / 4; ++i)//A:\NULLB:\NULLC:\NULL,每个占4位,所以DSLength/4可的驱动器数目
    {
        char dir[4] = { DStr[si], ':', '\\' };
        printf("%s \n",dir);
        DType = GetDriveType(DStr + i * 4);
        //GetDriveType获得驱动器类型
        if (DType == DRIVE_FIXED)
        {
            printf("Hard Disk");
        }
        else if (DType == DRIVE_CDROM)
        {
            printf("CD-ROM");
        }
        else if (DType == DRIVE_REMOVABLE)
        {
            printf("Removable Disk");
        }
        else if (DType == DRIVE_REMOTE)
        {
            printf("Network Disk");
        }
        else if (DType == DRIVE_RAMDISK)
        {
            printf("Virtual RAM Disk");
        }
        else if (DType == DRIVE_UNKNOWN)
        {
            printf("Unknown Device");
        }
 
        fResult = GetDiskFreeSpaceEx(
            dir,
            (PULARGE_INTEGER)&i64FreeBytesToCaller,
            (PULARGE_INTEGER)&i64TotalBytes,
            (PULARGE_INTEGER)&i64FreeBytes);
        //GetDiskFreeSpaceEx 可获取驱动器磁盘的空间姿态
        if (fResult)// 返回值标识驱动器是否在工作姿态
        {
            printf(" totalspace: %f MB" ,(float)i64TotalBytes / 1024 / 1024 );// 磁盘总容量
            printf(" freespace: %f MB", (float)i64FreeBytesToCaller / 1024 / 1024 );// 磁盘剩余容量
        }
        else
        {
            printf(" 设备未准备好 “);
        }
        printf("\n");
        si += 4;
    }
 
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值