温度检测,在上面已经说过cpu和硬盘温度的检测,这里主要是说Nvidia GPU的检测。废话不多讲了,贴上代码吧。其实很简单,都是对nvapi的封装而已。 /* Desc:This is a wrap of nvidia api,which provide almost functions that supported by nvidia. Author:tommy tommy.wong2008@yahoo.com Date:2010/17/15 */ #pragma once #include "windows.h" #include "nvapi.h" #ifdef WIN32 #pragma comment(lib,"../NVAPI_May2010/x86/nvapi.lib") #else #pragma comment(lib,"../NVAPI_May2010/amd64/nvapi64.lib") #endif class CNvidia { public: virtual ~CNvidia(void); public: static CNvidia* GetInstance(); public: // //get describing the version of the NvAPI library // bool GetInterfaceVersionString(LPSTR strVer); // //This function returns a struct that describes aspects of the display driver build. // bool GetDisplayDriverVersion(); // //Get physical gpu count // int GetPhyGpuCount(); // // // bool CNvidia::GetGpuTemperature(int nIndex,int& nMinTemp,int& nCurrentTemp,int& nMaxTemp); // //Get physical full name by index //return -1:buffer is too small,0 failed // int GetGpuName(int nIndex,LPSTR szName,int nBuffSize); // //the GPU type (integrated or discrete) // //NV_SYSTEM_TYPE_GPU_UNKNOWN 0 //NV_SYSTEM_TYPE_IGPU 1 Integrated GPU. //NV_SYSTEM_TYPE_DGPU 2 Discrete GPU. // int GetGpuType(int nIndex); // //Get system type(notebook GPU or a desktop GPU) // //NV_SYSTEM_TYPE_UNKNOWN 0 //NV_SYSTEM_TYPE_LAPTOP 1 //NV_SYSTEM_TYPE_DESKTOP 2 int GetSysType(int nIndex); // //the virtual size of framebuffer in KB. This includes the physical RAM plus any system RAM that has been dedicated for use by the GPU. // int GetVirtualFrameBufferSize(int nIndex); // //the physical size of framebuffer in KB. This does NOT include any system RAM that may be dedicated for use by the GPU // int GetPhysicalFrameBufferSize(int nIndex); // //the AGP aperture in megabytes. // int GetAGPAperture(int nIndex); // //the current AGP Rate (0 = AGP not present, 1 = 1x, 2 = 2x, etc.) // int GetCurrentAGPRate(int nIndex); // //Returns the ID of the bus associated with this GPU // int GetBusId(int nIndex); // // Returns the ID of the bus slot associated with this GPU. // int GetBusSlotId(int nIndex); // //returns the type of bus associated with this GPU. // //NVAPI_GPU_BUS_TYPE_UNDEFINED //NVAPI_GPU_BUS_TYPE_PCI // NVAPI_GPU_BUS_TYPE_AGP // NVAPI_GPU_BUS_TYPE_PCI_EXPRESS // NVAPI_GPU_BUS_TYPE_FPCI // int GetBusType(int nIndex); // //PCI identifiers associated with this GPU. // bool GetPCIIdentifiers(int nIndex,ULONG32&DeviceId,ULONG32 &SubSystemId,ULONG32 &RevisionId,ULONG32&ExtDeviceId); // //the fan speed tachometer reading for the specified physical GPU // int GetTachReading(int nIndex); private: void Init_THERMAL_SETTINGS(NV_GPU_THERMAL_SETTINGS& ThermalSettings,NvU32 Version); private: CNvidia(void); static CNvidia* g_pInstance; private: bool m_InitSucceed; NvPhysicalGpuHandle physicalGPUs[NVAPI_MAX_PHYSICAL_GPUS]; NvU32 physicalGPUCount; }; #include "StdAfx.h" #include "Nvidia.h" #include <assert.h> CNvidia* CNvidia::g_pInstance = NULL; CNvidia::CNvidia(void):m_InitSucceed(false) { //初始化,nvapi.dll只能动态加载 m_InitSucceed = NvAPI_Initialize() == NVAPI_OK ? true:false; } CNvidia::~CNvidia(void) { if (m_InitSucceed) { NvAPI_Unload(); } } CNvidia* CNvidia::GetInstance() { if (!g_pInstance) { g_pInstance = new CNvidia(); } return g_pInstance; } bool CNvidia::GetInterfaceVersionString(LPSTR strVer) { assert(m_InitSucceed); return NvAPI_GetInterfaceVersionString(strVer)==NVAPI_OK?true:false; } bool CNvidia::GetDisplayDriverVersion() { NvAPI_Status status; NV_DISPLAY_DRIVER_VERSION version = {0}; version.version = NV_DISPLAY_DRIVER_VERSION_VER; status = NvAPI_GetDisplayDriverVersion(NVAPI_DEFAULT_HANDLE, &version); if (status != NVAPI_OK) { NvAPI_ShortString string; NvAPI_GetErrorMessage(status, string); return false; } return true; } int CNvidia::GetPhyGpuCount() { NvAPI_Status status; status = NvAPI_EnumPhysicalGPUs(physicalGPUs, &physicalGPUCount); if (status != NVAPI_OK) { return -1; } return physicalGPUCount; } void CNvidia::Init_THERMAL_SETTINGS(NV_GPU_THERMAL_SETTINGS& ThermalSettings,NvU32 Version) { ZeroMemory(&ThermalSettings,sizeof(NV_GPU_THERMAL_SETTINGS)); ThermalSettings.version=Version; ThermalSettings.count=NVAPI_MAX_THERMAL_SENSORS_PER_GPU; // ThermalSettings.sensor[NVAPI_MAX_THERMAL_SENSORS_PER_GPU].controller = NVAPI_THERMAL_CONTROLLER_UNKNOWN; // ThermalSettings.sensor[NVAPI_MAX_THERMAL_SENSORS_PER_GPU].target = NVAPI_THERMAL_TARGET_GPU; } bool CNvidia::GetGpuTemperature(int nIndex,int& nMinTemp,int& nCurrentTemp,int& nMaxTemp) { NvAPI_Status status; NvU32 temp = 0; NV_GPU_THERMAL_SETTINGS ThermalSettings={0}; Init_THERMAL_SETTINGS(ThermalSettings,NV_GPU_THERMAL_SETTINGS_VER); status=NvAPI_GPU_GetThermalSettings(physicalGPUs[nIndex], //nvGPUHandle, //NVAPI_THERMAL_TARGET_ALL, 0, &ThermalSettings); if (NVAPI_INCOMPATIBLE_STRUCT_VERSION == status) { Init_THERMAL_SETTINGS(ThermalSettings,NV_GPU_THERMAL_SETTINGS_VER_1); status=NvAPI_GPU_GetThermalSettings(physicalGPUs[nIndex], //nvGPUHandle, //NVAPI_THERMAL_TARGET_ALL, 0, &ThermalSettings); if (status != NVAPI_OK) { NvAPI_ShortString string; NvAPI_GetErrorMessage(status, string); printf("NVAPI NvAPI_GetThermalSettings %s/n", string); NVAPI_INCOMPATIBLE_STRUCT_VERSION return false; } nCurrentTemp = ThermalSettings.sensor[0].currentTemp; nMinTemp = ThermalSettings.sensor[0].defaultMinTemp; nMaxTemp = ThermalSettings.sensor[0].defaultMaxTemp; return true; } else if (status == NVAPI_OK) { nCurrentTemp = ThermalSettings.sensor[0].currentTemp; nMinTemp = ThermalSettings.sensor[0].defaultMinTemp; nMaxTemp = ThermalSettings.sensor[0].defaultMaxTemp; return true ; } return false; } // //Get physical full name by index // int CNvidia::GetGpuName(int nIndex,LPSTR szName,int nBuffSize) { NvAPI_Status status; // //获取显卡名称,"NVIDIA" // NvAPI_ShortString Name; status = NvAPI_GPU_GetFullName(physicalGPUs[nIndex],Name); if (status == NVAPI_OK) { if(nBuffSize < strlen(Name)) return -1; strcpy(szName,Name); return 1; } return 0; } // //the GPU type (integrated or discrete) // int CNvidia::GetGpuType(int nIndex) { NvAPI_Status status; NV_GPU_TYPE type; status = NvAPI_GPU_GetGPUType(physicalGPUs[nIndex],&type); if (status == NVAPI_OK) { return type; } return NV_SYSTEM_TYPE_GPU_UNKNOWN; } // //Get system type(notebook GPU or a desktop GPU) // int CNvidia::GetSysType(int nIndex) { NvAPI_Status status; NV_SYSTEM_TYPE type; status = NvAPI_GPU_GetSystemType(physicalGPUs[nIndex],&type); if (status == NVAPI_OK) { return type; } return NV_SYSTEM_TYPE_UNKNOWN ; } // //the virtual size of framebuffer in KB. This includes the physical RAM plus any system RAM that has been dedicated for use by the GPU. // int CNvidia::GetVirtualFrameBufferSize(int nIndex) { NvAPI_Status status; NvU32 Size = -1; status = NvAPI_GPU_GetVirtualFrameBufferSize(physicalGPUs[nIndex],&Size); if (status == NVAPI_OK) { return Size; } return -1; } // //the physical size of framebuffer in KB. This does NOT include any system RAM that may be dedicated for use by the GPU // int CNvidia::GetPhysicalFrameBufferSize(int nIndex) { NvAPI_Status status; NvU32 Size = -1; status = NvAPI_GPU_GetPhysicalFrameBufferSize(physicalGPUs[nIndex],&Size); if (status == NVAPI_OK) { return Size; } return -1; } // //the AGP aperture in megabytes. // int CNvidia::GetAGPAperture(int nIndex) { NvAPI_Status status; NvU32 Size = -1; status = NvAPI_GPU_GetAGPAperture(physicalGPUs[nIndex],&Size); if (status == NVAPI_OK) { return Size; } return -1; } // //the current AGP Rate (0 = AGP not present, 1 = 1x, 2 = 2x, etc.) // int CNvidia::GetCurrentAGPRate(int nIndex) { NvAPI_Status status; NvU32 Rate = -1; status = NvAPI_GPU_GetCurrentAGPRate(physicalGPUs[nIndex],&Rate); if (status == NVAPI_OK) { return Rate; } return -1; } // //Returns the ID of the bus associated with this GPU // int CNvidia::GetBusId(int nIndex) { NvAPI_Status status; NvU32 BusId = -1; status = NvAPI_GPU_GetBusId(physicalGPUs[nIndex],&BusId); if (status == NVAPI_OK) { return BusId; } return -1; } // // Returns the ID of the bus slot associated with this GPU. // int CNvidia::GetBusSlotId(int nIndex) { NvAPI_Status status; NvU32 BusSlotId = -1; status = NvAPI_GPU_GetBusSlotId(physicalGPUs[nIndex],&BusSlotId); if (status == NVAPI_OK) { return BusSlotId; } return -1; } // //returns the type of bus associated with this GPU. // int CNvidia::GetBusType(int nIndex) { NvAPI_Status status; NV_GPU_BUS_TYPE type; status = NvAPI_GPU_GetBusType(physicalGPUs[nIndex],&type); if (status == NVAPI_OK) { return type; } return NVAPI_GPU_BUS_TYPE_UNDEFINED; } // //PCI identifiers associated with this GPU. //NvU32 * pDeviceId, //NvU32 * pSubSystemId, //NvU32 * pRevisionId, //NvU32 * pExtDeviceId // bool CNvidia::GetPCIIdentifiers(int nIndex,ULONG32&DeviceId,ULONG32 &SubSystemId,ULONG32 &RevisionId,ULONG32&ExtDeviceId) { NvU32 Value = 0; NvAPI_Status status; status = NvAPI_GPU_GetPCIIdentifiers(physicalGPUs[nIndex],(NvU32 *)&DeviceId,(NvU32 *)&SubSystemId,(NvU32 *)&RevisionId,(NvU32 *)&ExtDeviceId); if (status == NVAPI_OK) { return true; } return false; } // //the fan speed tachometer reading for the specified physical GPU // int CNvidia::GetTachReading(int nIndex) { NvU32 Value = 0; NvAPI_Status status; status = NvAPI_GPU_GetTachReading(physicalGPUs[nIndex], &Value); if (status == NVAPI_OK) { return Value; } return -1; } #include "stdafx.h" #include "Nvidia.h" int _tmain(int argc, _TCHAR* argv[]) { CNvidia * pInstance = CNvidia::GetInstance(); char szName[512]={0}; int PhyCount = pInstance->GetPhyGpuCount(); for (int i = 0;i < PhyCount;i++) { //name pInstance->GetGpuName(i,szName,512); printf("%s/n",szName); //gpu type switch(pInstance->GetGpuType(i)) { case 0: printf("Unknown/n"); break; case 1: printf("Integrated/n"); break; case 2: printf("Discrete/n"); break; } //fan speed printf("fan speed:%d/n",pInstance->GetTachReading(i)); //computer type switch(pInstance->GetSysType(i)) { case 0: printf("Unknown/n"); break; case 1: printf("laptop/n"); break; case 2: printf("desktop/n"); break; } //bus type switch(pInstance->GetBusType(i)) { case 0: printf("Unknown/n"); break; case 1: printf("PCI/n"); break; case 2: printf("AGP/n"); break; case 3: printf("PCI_EXPRESS/n"); break; case 4: printf("FPCI /n"); break; } //agp rate printf("AGP %dx/n",pInstance->GetCurrentAGPRate(i)); // printf("AGP Aperture %d MB/n",pInstance->GetAGPAperture(i)); printf("Virtul Frame buffer %dKB/n",pInstance->GetVirtualFrameBufferSize(i)); printf("Phy Frame buffer %dKB/n",pInstance->GetPhysicalFrameBufferSize(i)); int nMinTemp=0,nCurretTemp=0,nMaxTemp=0; if(pInstance->GetGpuTemperature(i,nMinTemp,nCurretTemp,nMaxTemp)) { printf("Temperature Default:%d Current:%d Max:%d/n",nMinTemp,nCurretTemp,nMaxTemp); } } getchar(); return 0; }