自用电脑温度报警

 

描述:

  最近发现电脑风扇有时候不转!散热口那会烫的厉害!拆开后盖后可能是风扇接线接触不良!写个程序监控一下CPU的温度!

1. windows控制台程序。 每2分钟采一次温度数据,如果温度高于60,70,80,90(不大可能)时分别响警报声2,4,6,8次;温度高于60,70时采集频率加倍,控制台打印字体显黄色;变成1分钟采一次,高于80的时候30秒采一次,控制台打印字体显示红色;

2. 加入windows自启动。

 

代码:

  新建temptest.cpp。加入如下代码。

  报警声用cout<<"\a";也可以换成自己想要的铃声。

// temptest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#define _WIN32_DCOM
#include <iostream>
using namespace std;
#include <comdef.h>
#include <Wbemidl.h>

# pragma comment(lib, "wbemuuid.lib")

int main(int argc, char **argv)
{
    int SLEEPTIME = 120;

    //设置控制台字体颜色
    HANDLE hOut;
    hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hOut, FOREGROUND_GREEN|FOREGROUND_INTENSITY);

    CoInitializeEx(0,COINIT_MULTITHREADED);
    try 
    {
        if(SUCCEEDED(CoInitializeSecurity(NULL,-1,NULL,NULL,RPC_C_AUTHN_LEVEL_DEFAULT,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE,NULL))) 
        {
            IWbemLocator *pLoc=NULL;
            if(SUCCEEDED(CoCreateInstance(CLSID_WbemLocator,0,CLSCTX_INPROC_SERVER,IID_IWbemLocator,(LPVOID *)&pLoc)))
            {
                IWbemServices *pSvc=NULL;
                if(SUCCEEDED(pLoc->ConnectServer(_bstr_t(L"ROOT\\WMI"),NULL,NULL,0,NULL,0,0,&pSvc)))
                {
                    if(SUCCEEDED(CoSetProxyBlanket(pSvc,RPC_C_AUTHN_WINNT,RPC_C_AUTHZ_NONE,NULL,RPC_C_AUTHN_LEVEL_CALL,RPC_C_IMP_LEVEL_IMPERSONATE,NULL,EOAC_NONE)))
                    {
                        while (true){

                            SLEEPTIME = 120;

                            SetConsoleTextAttribute(hOut, FOREGROUND_GREEN|FOREGROUND_INTENSITY);

                            IEnumWbemClassObject* pEnumerator=NULL;
                            if(SUCCEEDED(pSvc->ExecQuery(bstr_t("WQL"),bstr_t("SELECT * FROM MSAcpi_ThermalZoneTemperature"),WBEM_FLAG_FORWARD_ONLY|WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumerator)))
                            {
                                IWbemClassObject *pclsObj;
                                ULONG uReturn=0;
                                while(pEnumerator)
                                {
                                    pEnumerator->Next(WBEM_INFINITE, 1, &pclsObj, &uReturn); 
                                    if(0==uReturn)
                                        break;
                                    VARIANT vtProp;
                                    VariantInit(&vtProp);
                                    pclsObj->Get(L"CurrentTemperature", 0, &vtProp, 0, 0);
                                    double CT = (vtProp.intVal - 2732)/10.0;

                                    int n = -1;
                                    switch(int(CT)/10)
                                    {
                                    case 9:n = 4;break;
                                    case 8:n = 3;break;
                                    case 7:n = 2;break;
                                    case 6:n = 1;break;
                                    default:n = 0;break;
                                    }
                                    for (int i = 0; i < 2*n; i++)
                                        cout<<"\a";
                    
                                    if (n == 2 || n == 1)
                                    {
                                        SetConsoleTextAttribute(hOut, FOREGROUND_GREEN|FOREGROUND_RED|FOREGROUND_INTENSITY);
                                        SLEEPTIME = 60;
                                    }
                                    else if(n == 3 || n == 4)
                                    {
                                        SetConsoleTextAttribute(hOut, FOREGROUND_RED|FOREGROUND_INTENSITY);
                                        SLEEPTIME = 30;
                                    }
                                    cout << "Current CPU Temperature : " << (vtProp.intVal - 2732)/10.0 << endl;
                                    
                                    VariantClear(&vtProp);
                                    pclsObj->Release();
                                }
                            }
                        Sleep(SLEEPTIME*1000);
                        }
                    }
                    pSvc->Release();
                }
                pLoc->Release();
            }
        }
    }
    catch (_com_error err)
    {
    }
    CoUninitialize();
    return 0; 
}

 加入开机启动项,将程序拖到C:\Users\zhu\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup文件中,zhu换成自己的用户名

或写成bat放入。

@echo off
temptest.exe

 

posted @ 2014-05-09 16:44  凤舞十天  阅读(472)  评论(0编辑  收藏  举报