CPU-Auslastungsanzeige von Konsole in Borland Builder Form wandeln
-
Moin moin,
ich habe mit Hilfe eines fremden Quelltextes zum Ermitteln der CPU-Auslastung ein kleines Konsolenprogramm erstellt, dass die Auslastung in einer kleinen "Fortschritts"-Anzeige darstellt. Soweit, so gut, doch nun möchte ich das ganze gerne als richtige Windows-Anwendung mit dem Borland C++ Builder machen (sieht einfach schöner aus).
Doch wenn ich den Quelltext der Konsolenversion in den eines solchen, neuen Formulars reinkopiere, bekomme ich Fehler in der "dde.h" in der Zeile 19 mit "extern "C" {", nämlich "Linkage specification not allowed" und "Declaration terminated incorrectly". Dabei habe ich an der Datei garnichts gemacht..?Wo liegt da der Fehler, was muss ich ändern? Oder gibt es sonst eine andere Möglichkeit, den Wert der Auslastung (hier iProzent) in einem Formular zu nutzen?
Hier die Codes:
CpuLoad.h//***************************************************************************** //* //* //* CpuLoad.h //* //* //***************************************************************************** #ifndef __CPULOAD_H__ #define __CPULOAD_H__ int CpuInit(); // Initialisierung int CpuExit(); // Deinitialisierung int CpuCount(); // CPU-Anzahl abfragen int CpuUpdate(); // CPU-Auslastungsdaten hollen int CpuLoad(unsigned uCpu); // CPU-Auslastungs für eine CPU abfragen #endifund der eigentliche Code Projekt.cpp
//--------------------------------------------------------------------------- #include <vcl\vcl.h> #pragma hdrstop #include [iostream.h] #include [conio.h] #include "Unit2.h" //--------------------------------------------------------------------------- #pragma resource "*.dfm" TDataModule2 *DataModule2; //--------------------------------------------------------------------------- __fastcall TDataModule2::TDataModule2(TComponent* Owner) : TDataModule(Owner) { //***************************************************************************** //* //* //* CpuLoad.cpp //* //* //***************************************************************************** #include <windows.h> #include "CpuLoad.h" #define PROCESSOR_IDX 238 #define CPU_USAGE_IDX 6 static HKEY hWin9xKey = 0; static BOOL bIsWinNt = 0; static DWORD dwDataSize = 0; static DWORD dwWin9xUsage = 0; static __int64 *pCounterAct = 0; static __int64 *pCounterPre = 0; static __int64 iSysTimeAct = 1; static __int64 iSysTimePre = 0; static unsigned uCpuCount = 0; static union { PERF_DATA_BLOCK *o; BYTE *b; }pData; //***************************************************************************** //* //* CpuUpdate //* //***************************************************************************** // Hollt die neuesten Daten für die CPU-Auslastung // Ergibt 1 wenn ok sonst 0 int CpuUpdate() { DWORD dwSize,dwType; int iError; unsigned u; union { PERF_OBJECT_TYPE *o; BYTE *b; }pType; union { PERF_COUNTER_DEFINITION *o; BYTE *b; }pCounter; union { PERF_INSTANCE_DEFINITION*o; BYTE *b; }pInstance; union { PERF_COUNTER_BLOCK *o; BYTE *b; }pBlock; //******************** Win 98 ************************************************* if(!bIsWinNt) { if(!hWin9xKey) { HKEY hKey; iError=RegOpenKeyEx(HKEY_DYN_DATA,"PerfStats\\StartStat",0,KEY_ALL_ACCESS,&hKey); if(iError!=ERROR_SUCCESS){hWin9xKey=0;return 0;} dwSize=sizeof(DWORD); RegQueryValueEx(hKey,"KERNEL\\CPUUsage",0,&dwType,(LPBYTE)&dwWin9xUsage, &dwSize); RegCloseKey(hKey); iError=RegOpenKeyEx( HKEY_DYN_DATA,"PerfStats\\StatData",0,KEY_READ,&hWin9xKey); if(iError!=ERROR_SUCCESS){hWin9xKey=0;return 0;} if(!uCpuCount)uCpuCount=1; } dwSize=sizeof(DWORD); RegQueryValueEx(hWin9xKey,"KERNEL\\CPUUsage",0,&dwType,(LPBYTE)&dwWin9xUsage,&dwSize); return 1; } //******************** Win NT ************************************************* while(1) { dwSize=dwDataSize; iError=RegQueryValueEx( HKEY_PERFORMANCE_DATA, "238", 0, 0,pData.b,&dwSize); if(iError!=ERROR_MORE_DATA)break; dwDataSize+=256; if(pData.o)delete pData.o; pData.b=new BYTE[dwDataSize]; } if(iError!=ERROR_SUCCESS || !pData.o) { if(pData.o)delete pData.o; return 0; } pType.b = ((BYTE*)pData.o)+pData.o->HeaderLength; for(u=0;u<pData.o->NumObjectTypes;u++) { if(pType.o->ObjectNameTitleIndex==PROCESSOR_IDX)break; pType.o += pType.o->TotalByteLength; } if(u>=pData.o->NumObjectTypes) { delete pData.o; return 0; } if(!uCpuCount && pType.o->NumInstances>(int)uCpuCount) { uCpuCount = pType.o->NumInstances; pCounterAct = new __int64[uCpuCount*2]; pCounterPre = pCounterAct+uCpuCount; } pCounter.b = pType.b + pType.o->HeaderLength; for(u=1;u<pType.o->NumCounters;u++) { if(pCounter.o->CounterNameTitleIndex == CPU_USAGE_IDX)break; pCounter.b += pCounter.o->ByteLength; } if(u>=pType.o->NumCounters) { delete pData.o; return 0; } pInstance.b = pType.b + pType.o->DefinitionLength; for(u = 0; (int)u<pType.o->NumInstances ; u++) { pBlock.b = pInstance.b + pInstance.o->ByteLength; pCounterPre[u]=pCounterAct[u]; pCounterAct [u]=*(__int64*)(pBlock.b + pCounter.o->CounterOffset); pInstance.b = pBlock.b + pBlock.o->ByteLength; } iSysTimePre = iSysTimeAct; SystemTimeToFileTime(&pData.o->SystemTime,(LPFILETIME)&iSysTimeAct); if(iSysTimePre == iSysTimeAct)iSysTimeAct++; return 1; } //***************************************************************************** //* //* CpuInit //* //***************************************************************************** // Initialisiert die CPU-Auslastungsberechnung // Ergibt 1 wenn ok sonst 0 int CpuInit() { DWORD dwVersion; dwVersion=GetVersion(); dwVersion=((dwVersion&0xFF00)>>8) | ((dwVersion&0x00FF)<<8); if(dwVersion>=0x0500 || dwVersion==0x0400 || dwVersion==0x0300)bIsWinNt=TRUE; if(!CpuUpdate())return 0; return 1; } //***************************************************************************** //* //* CpuExit //* //***************************************************************************** // Gibt alle Speicherbereiche wieder frei // Ergibt 1 wenn ok sonst 0 int CpuExit() { dwDataSize = 0; uCpuCount = 0; if(pCounterAct){delete pCounterAct;pCounterAct = 0;pCounterPre = 0;} if(pData.b ){delete pData.b ;pData.b = 0;} if(hWin9xKey ) { int iError; HKEY hKey; DWORD dwSize,dwType; RegCloseKey(hWin9xKey); hWin9xKey = 0; iError=RegOpenKeyEx(HKEY_DYN_DATA,"PerfStats\\StopStat",0,KEY_ALL_ACCESS,&hKey); if(iError!=ERROR_SUCCESS)return 0; dwSize=sizeof(DWORD); RegQueryValueEx(hKey,"KERNEL\\CPUUsage",0,&dwType,(LPBYTE)&dwWin9xUsage,&dwSize); RegCloseKey(hKey); } return 1; } //***************************************************************************** //* //* CpuLoad //* //***************************************************************************** // Ergibt die aktuelle CPU-Auslastung (0 bis 1024) // // uCpu : Ist die Nummer der CPU // // Danmit das funktioniert muss zuvor eine Zeit gewartet werden // und CpuUpdate aufgerufen werden // z.B. Sleep(1000); // CpuUpdate(); // CpuLoad(0); // CpuLoad(1); // ... int CpuLoad(unsigned uCpu) { __int64 iValue; int iLoad; if(uCpu>=uCpuCount)return -1; if(!bIsWinNt) // Windows 95/98 { if(!hWin9xKey)return -1; iLoad = (dwWin9xUsage<<10) / 100; } else{ // Windows NT/2000/XP iValue = pCounterAct[uCpu]-pCounterPre[uCpu]; iValue <<= 10; iValue /= iSysTimeAct-iSysTimePre; iLoad = 1024-(int)iValue; } if(iLoad< 0)iLoad=0; if(iLoad>1024)iLoad=1024; return iLoad; } //***************************************************************************** //* //* CpuCount //* //***************************************************************************** // Gibt die Anzah aller CPU's int CpuCount() { SYSTEM_INFO sInfo; if(!uCpuCount)CpuInit(); GetSystemInfo(&sInfo); if(uCpuCount<sInfo.dwNumberOfProcessors)return uCpuCount; return sInfo.dwNumberOfProcessors; } //------------------------------- //------------------------------- //------------------------------- } int main() { int i,iProzent,iCpuCount; CpuInit(); iCpuCount=CpuCount(); while(1) { char auslast[10]; auslast[0]=32; auslast[1]=32; auslast[2]=32; auslast[3]=32; auslast[4]=32; auslast[5]=32; auslast[6]=32; auslast[7]=32; auslast[8]=32; auslast[9]=32; for(i=0;i<iCpuCount;i++) // CPU Daten ausgeben { clrscr(); CpuUpdate(); iProzent=(CpuLoad(i)*100)>>10; if (iProzent>-1)auslast[0]=79; else (auslast[0]=32); if (iProzent>10)auslast[1]=79; else (auslast[1]=32); if (iProzent>20)auslast[2]=79; else (auslast[2]=32); if (iProzent>30)auslast[3]=79; else (auslast[3]=32); if (iProzent>40)auslast[4]=79; else (auslast[4]=32); if (iProzent>50)auslast[5]=79; else (auslast[5]=32); if (iProzent>60)auslast[6]=79; else (auslast[6]=32); if (iProzent>70)auslast[7]=79; else (auslast[7]=32); if (iProzent>80)auslast[8]=79; else (auslast[8]=32); if (iProzent>90)auslast[9]=79; else (auslast[9]=32); cout<<" -------------------" <<endl; cout<<"|";cout<<auslast[0];cout<<"|"; cout<<auslast[1];cout<<"|"; cout<<auslast[2];cout<<"|"; cout<<auslast[3];cout<<"|"; cout<<auslast[4];cout<<"|"; cout<<auslast[5];cout<<"|"; cout<<auslast[6];cout<<"|"; cout<<auslast[7];cout<<"|"; cout<<auslast[8];cout<<"|"; cout<<auslast[9];cout<<"|";cout<<endl; cout<<" -------------------" <<endl; Sleep(150); } } CpuExit(); return 0; }Es wär super, wenn mir da jemand die passende Antwort geben könnte!
Vielen Dank und mfG,
Morinho
-
Frag mal lieber im (WinApi) oder VCL Forum nach!!!
Da musst du dann mit Controlls (Buttons, Eingabefelder ...) arbeiten.
-
Dieser Thread wurde von Moderator/in HumeSikkins aus dem Forum C++ in das Forum VCL/CLX (Borland C++ Builder) verschoben.
Im Zweifelsfall bitte auch folgende Hinweise beachten:
C/C++ Forum :: FAQ - Sonstiges :: Wohin mit meiner Frage?Dieses Posting wurde automatisch erzeugt.
-
Hallo
Doch wenn ich den Quelltext der Konsolenversion in den eines solchen, neuen Formulars reinkopiere, bekomme ich Fehler in der "dde.h" in der Zeile 19 mit "extern "C" {", nämlich "Linkage specification not allowed" und "Declaration terminated incorrectly". Dabei habe ich an der Datei garnichts gemacht..?
Konsolenprogramm haben einen fundamental anderes Konzept als (VCL-) GUI-Anwendungen. Villeicht fängst du erstmal an, dich mit der VCL zu beschäftigen.
Mir fällt auf dem ersten Blick schonb mal das auf
#include [iostream.h] #include [conio.h]das muß auf jeden fall so aussehen
#include <iostream> #include <conio.h>bis bald
akari