PDA

Visualizza la versione completa : C++ Programma per calcolare il tempo


ovidiupop
30-11-2008, 10:30
Salve,
sto cercando di fare un Programma per calcolare quanto tempo resta aperto un altro programma.

Per esempio: io sto creando il programma A che appena il programma B si apre, il programma A fa partire il tempo, e quando il programma B si chiude, il tempo si ferma.

Ho provato vari metodi...con i threads...ma non ci sono ancora riuscito....

Ecco qui il codice
C++

#include <irrlicht.h>
#include <iostream>
#include <windows.h>
#include <time.h>
#include <tlhelp32.h>
#include <ctime>
#include <stdio.h>
#include <stdlib.h>

using namespace irr;
using namespace std;

#pragma comment(lib, "Irrlicht.lib")
#pragma once


int main()
{

DWORD FirstGTC = GetTickCount();

bool bFlag;

bFlag = false;

HWND hWnd;
DWORD dwID;
HANDLE ProcHandle;

hWnd = FindWindow(NULL, "Euro Truck Simulator");

GetWindowThreadProcessId(hWnd, &dwID);

ProcHandle = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, NULL, dwID);

if (ProcHandle) {
FirstGTC = GetTickCount();
MyThread = CreateThread(&ThreadProc);
}

while (!bFlag) {
Sleep(100);
}

printf("The process was running for %u milliseconds.\n",TimeElapsedInMilliseconds);

return 0;

}

ThreadProc {
while (ProcHandle) {
Sleep(100);
}

TimeElapsedInMilliseconds = (GetTickCount() - FirstGTC);
bFlag = true;
}

MItaly
30-11-2008, 14:51
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
DWORD FirstGTC;
HWND hWnd;
DWORD dwID=0;
HANDLE ProcHandle;

hWnd = FindWindow(NULL, "Euro Truck Simulator");
if(!hWnd)
{
cerr<<"Cannot find the window."<<endl;
return 1;
}
GetWindowThreadProcessId(hWnd, &dwID);
ProcHandle = OpenProcess(SYNCHRONIZE, NULL, dwID);
if(!ProcHandle)
{
cerr<<"Cannot open the process."<<endl;
return 2;
}
FirstGTC = GetTickCount();
WaitForSingleObject(ProcHandle,INFINITE);
cout<<"The process was running for "<<(GetTickCount()-FirstGTC)<<" milliseconds."<<endl;
return 0;
}

MacApp
30-11-2008, 15:44
Occhio che la GetTickCount:
http://msdn.microsoft.com/en-us/library/ms724408(VS.85).aspx


The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days. To avoid this problem, use GetTickCount64. Otherwise, check for an overflow condition when comparing times.

ovidiupop
30-11-2008, 15:44
grazie raga...ho risolto con una dll.

MItaly
30-11-2008, 15:54
Giusto... ma si risolve facilmente.


#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
DWORD lastGTC;
ULONGLONG totTime=0;
HWND hWnd;
DWORD dwID=0;
HANDLE ProcHandle;

hWnd = FindWindow(NULL, "Euro Truck Simulator");
if(!hWnd)
{
cerr<<"Cannot find the window."<<endl;
return 1;
}
GetWindowThreadProcessId(hWnd, &dwID);
ProcHandle = OpenProcess(SYNCHRONIZE, NULL, dwID);
if(!ProcHandle)
{
cerr<<"Cannot open the process."<<endl;
return 2;
}
lastGTC = GetTickCount();
while(WaitForSingleObject(ProcHandle,60000)==WAIT_ TIMEOUT)
{
totTime+=GetTickCount()-lastGTC;
lastGTC = GetTickCount();
};
cout<<"The process was running for "<<totTime+(GetTickCount()-lastGTC)<<" milliseconds."<<endl;
return 0;
}


grazie raga...ho risolto con una dll.
Perché tirare in ballo una dipendenza aggiuntiva quando il problema è di così facile soluzione?

Loading