Wie kann ich die Systemwerte auslesen?
-
Hallo Leute!
Ich habe vor ein Programm zu schreiben, in dem eigentlich alle Werte meines Systems zu finden sind.
Und zwar an einem Fleck, und nicht ein Teil im Taskmanager, ein anderer nur per nTune, usw.Ich dachte an solche Werte wie CPU-Takt, RAM, Graka-RAM, DX-Version...
Quer durch den GemüsegartenIch weiß bloß nicht wo ich danach suchen soll. Für google gehen mir irgendwie die Suchbegriffe aus.
Ich hoffe mir kann jemand helfen, an irgendeinen Wert zu kommen.
Hoffentlich werdens mehr!Schonmal danke!
-
Dann ist hier schonmal der Anfang mit dem RAM.
int GetRAM(void) { MEMORYSTATUSEX sStatus; sStatus.dwLength=sizeof(MEMORYSTATUSEX); GlobalMemoryStatusEx(&sStatus); return (sStatus.ullTotalPhys / (1024 * 1024)); }
Das hier könnte ja ein Sammelthread werden
-
Sammelthread hört sich gut an...
Hier ne Klasse für die CPU-Auslastung (auch für Mehrkern-CPUs):
cpuusage.h:#if !defined(AFX_CPUUSAGE_H__A81CF652_6568_4BBB_91DA_4C1A40237D10__INCLUDED_) #define AFX_CPUUSAGE_H__A81CF652_6568_4BBB_91DA_4C1A40237D10__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 #include <afxwin.h> #include <winternl.h> #include <vector> //======================================================================================= // // INTERFACE: Class CCpuUsage // //======================================================================================= class CCpuUsage { public: CCpuUsage(); virtual ~CCpuUsage(); inline bool GetSupported() const; int GetCpuUsage( int cpu, int* pKernelPercent = NULL, int* pUserPercent = NULL ); void SetScale( int factor = 100 ); int GetScale() const { return m_scaleFactor; } int GetProcessorCount() const { return m_processorCount; } void Init() const; private: typedef NTSTATUS (WINAPI* P_NTQSI)( SYSTEM_INFORMATION_CLASS, PVOID, ULONG, PULONG ); mutable bool m_bInitialised; mutable P_NTQSI m_p_NtQuerySystemInformation; mutable int m_processorCount; mutable std::vector<unsigned __int64> m_lastIdleTime, m_lastUserTime, m_lastKernelTime; int m_scaleFactor; }; //--------------------------------------------------------------------------------------- // INLINE IMPLEMENTATION: Class CCpuUsage //--------------------------------------------------------------------------------------- bool CCpuUsage::GetSupported() const { if( ! m_bInitialised ) Init(); return m_p_NtQuerySystemInformation != NULL; } //--------------------------------------------------------------------------------------- #endif // !defined(AFX_CPUUSAGE_H__A81CF652_6568_4BBB_91DA_4C1A40237D10__INCLUDED_)
cpuusage.cpp:
#include "stdafx.h" #include "CpuUsage.h" using namespace std; #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif //from the "ntstatus.h" file: #define STATUS_SUCCESS ((NTSTATUS)0x00000000L) // ntsubauth //--------------------------------------------------------------------------------------- CCpuUsage::CCpuUsage() { m_bInitialised = false; m_processorCount = 0; m_p_NtQuerySystemInformation = NULL; for(int i=0;i!= 4;i++) { m_lastIdleTime.push_back(0); m_lastUserTime.push_back(0); m_lastKernelTime.push_back(0); } m_scaleFactor = 100; } //--------------------------------------------------------------------------------------- CCpuUsage::~CCpuUsage() { //unused } //--------------------------------------------------------------------------------------- void CCpuUsage::Init() const { m_bInitialised = true; //--- check if we have at least Win 2000 OSVERSIONINFO ovi = { sizeof(ovi) }; ::GetVersionEx( &ovi ); if( ovi.dwMajorVersion < 5 ) return; HMODULE hMod = ::GetModuleHandle( _T("ntdll.dll") ); if( hMod ) m_p_NtQuerySystemInformation = reinterpret_cast<P_NTQSI>( ::GetProcAddress( hMod, "NtQuerySystemInformation" ) ); if( m_p_NtQuerySystemInformation ) { //--- get number of processors SYSTEM_BASIC_INFORMATION sbi = { 0 }; NTSTATUS res = m_p_NtQuerySystemInformation( SystemBasicInformation, &sbi, sizeof(sbi), NULL ); if( res == STATUS_SUCCESS ) m_processorCount = sbi.NumberOfProcessors; } } //--------------------------------------------------------------------------------------- int CCpuUsage::GetCpuUsage(int cpu, int* pKernelPercent, int* pUserPercent) { if( ! m_bInitialised ) Init(); if(cpu > m_processorCount - 1) return -1; // default values will indicate error if( pKernelPercent ) *pKernelPercent = -1; if( pUserPercent ) *pUserPercent = -1; // return if the function is not supported --- if( ! m_p_NtQuerySystemInformation ) return -1; bool bSuccess = false; unsigned __int64 kernelTime = 0, userTime = 0, idleTime = 0; vector<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> sppi( m_processorCount ); NTSTATUS res = m_p_NtQuerySystemInformation( SystemProcessorPerformanceInformation, &sppi[0], sizeof(sppi[0]) * sppi.size(), NULL ); if( res == STATUS_SUCCESS ) { //--- sum CPU usage times for all processors in the system // for( int i = 0; i < m_processorCount; i++ ) //{ kernelTime += sppi[cpu].KernelTime.QuadPart; userTime += sppi[cpu].UserTime.QuadPart; idleTime += sppi[cpu].IdleTime.QuadPart; //} //--- calculate percentages of whatever scale the user of the class wants unsigned __int64 kernelTimeDiff = kernelTime - m_lastKernelTime[cpu]; unsigned __int64 userTimeDiff = userTime - m_lastUserTime[cpu]; unsigned __int64 idleTimeDiff = idleTime - m_lastIdleTime[cpu]; unsigned __int64 totalTimeDiff = kernelTimeDiff + userTimeDiff; if( totalTimeDiff == 0 ) totalTimeDiff = 1; int totalUsagePercent = (totalTimeDiff - idleTimeDiff) * m_scaleFactor / totalTimeDiff; if( pKernelPercent ) *pKernelPercent = (kernelTimeDiff - idleTimeDiff) * m_scaleFactor / totalTimeDiff; if( pUserPercent ) *pUserPercent = userTimeDiff * m_scaleFactor / totalTimeDiff; m_lastKernelTime[cpu] = kernelTime; m_lastUserTime[cpu] = userTime; m_lastIdleTime[cpu] = idleTime; return totalUsagePercent; } return -1; }
-
Guck' Dir mal WMI an, kannst Du an der Kommandozeile mit WMIC erforschen.
http://www.serverhowto.de/Teil-1-Wie-wird-WMIC-gestartet.171.0.html
-
Danke euch Drein!
@peterchen: Also mit WMI komm ich nicht klar, weiß nich wie ich das in ein Programm integrieren kann
@Machine: Krieg ich schlecht in die Konsole rein. Ich wollte eigentlich feste Werte haben, wie zb Prozessorname oder Prozessorleistung in MHz, trotzdem danke.
@Blaze: Genau das was ich brauche, solche Funktionen
Das mit dem Sammelthread ist wirklich ne gute Idee! Machen wir einen draus!
-
WMI hab' ich auch noch nicht vom C++ ausprobiert, hab nur neulich die Konsole entdeckt
ich weiß nur, daß die API "relativ häßlich" ist, aber eine Menge abdeckt - http://msdn.microsoft.com/en-us/library/aa389762(VS.85).aspx
-
Hier ist noch etwas für die CPU. Geht alles über Registry
Beispiel für Konsole:
void ShowCPU(void) { cout<<"#CPU"<<endl; DWORD dwLen=256; DWORD dwSizeOfDWORD= sizeof(DWORD); char acCPUName[256]=""; DWORD dwCPUMHz=0; HKEY hkBaseCPU; LONG lResult= RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0,KEY_READ,&hkBaseCPU); if(lResult != ERROR_SUCCESS) { cout<<"Fehler 1! RegistryKey konnte nicht geoeffnet werden."<<endl; return; } RegQueryValueEx(hkBaseCPU,"ProcessorNameString",0,0,acCPUName,&dwLen); RegQueryValueEx(hkBaseCPU,"~MHz",0,0,(BYTE*)&dwCPUMHz,&dwSizeOfDWORD); RegCloseKey(hkBaseCPU); cout<<" Name : "<<acCPUName<<endl; cout<<" Leistung : "<<dwCPUMHz<<" MHz"<<endl; }
-
Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum WinAPI verschoben.
Im Zweifelsfall bitte auch folgende Hinweise beachten:
C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?Dieses Posting wurde automatisch erzeugt.