Hi everyone, I made a program that will check if a process is running.. it is supposed to detect if a process is running and start a timer. If the process has been ended, the timer is paused.. when the process is restarted, the timer continues from where it last was. Until it reaches example 5 minutes.. or until the timer count reaches 1 hr.. etc..
The code I have is as follow:
#pragma comment(lib, "advapi32.lib")
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <tlhelp32.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
DWORD CountProcesses(CHAR *pProcessName)
{
DWORD dwCount = 0;
HANDLE hSnap = NULL;
PROCESSENTRY32 proc32;
if((hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)) == INVALID_HANDLE_VALUE)
return -1;
proc32.dwSize=sizeof(PROCESSENTRY32);
while((Process32Next(hSnap, &proc32)) == TRUE)
if(stricmp(proc32.szExeFile,pProcessName) == 0)
++dwCount;
CloseHandle(hSnap);
return dwCount;
}
int main(void)
{
DWORD dwReturn;
char cProcess[80] = "hl.exe"; //process to be checked..
dwReturn = CountProcesses(cProcess);
if(dwReturn != -1){
printf("There are %d %s processes running\n",dwReturn, cProcess); //check if process is running..
}
else{
printf("CreateToolhelp32Snapshot failed\n");
}
if(dwReturn != -1){
system("taskkill /F /IM hl.exe"); //Stop program from running..
//would love to change this something like system("taskkill /F /IM"<<cProcess<<"");
}
system("Pause");
return 0;
}
And the timer I found on the internet, I edited and and added a repeat function but Im not sure how to implement it.. Or is there a far better way of checking how long it was running?
#define REPEAT do{
#define UNTIL( condition ) }while(!(condition));
#include <iostream.h>
#include <conio.h>
#include <windows.h>
#include <time.h> // class needs this inclusion
//////////////////////////////////////////
// class declaration:
class timer {
public:
timer();
void start();
void stop();
void reset();
bool isRunning();
unsigned long getTime();
bool isOver(unsigned long seconds);
private:
bool resetted;
bool running;
unsigned long beg;
unsigned long end;
};
//////////////////////////////////////////
// class implementation:
timer::timer() {
resetted = true;
running = false;
beg = 0;
end = 0;
}
void timer::start() {
if(! running) {
if(resetted)
beg = (unsigned long) clock();
else
beg -= end - (unsigned long) clock();
running = true;
resetted = false;
}
}
void timer::stop() {
if(running) {
end = (unsigned long) clock();
running = false;
}
}
void timer::reset() {
bool wereRunning = running;
if(wereRunning)
stop();
resetted = true;
beg = 0;
end = 0;
if(wereRunning)
start();
}
bool timer::isRunning() {
return running;
}
unsigned long timer::getTime() {
if(running)
return ((unsigned long) clock() - beg) / CLOCKS_PER_SEC;
else
return (end - beg) / CLOCKS_PER_SEC;
}
bool timer::isOver(unsigned long seconds) {
return seconds >= getTime();
}
//////////////////////////////////////////
// class test program:
int main() {
bool quit = false;
char choice;
timer t;
while(! quit) {
{
t.start();
cout << "started" << endl;
int i = 0;
do
{
Sleep(1000);
cout <<t.getTime()/3600<<" hrs & "<< t.getTime() << " s" << endl;
i++;
}while(i <= 20);
quit = true;
}
}
cout << "------------------------------" << endl;
return 0;
}