Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How would I check to see if another app is running? 1

Status
Not open for further replies.

2016nc

Programmer
Jun 3, 2002
31
US
This is what I want to do, make an application that:

-Launches an app (i.e. notepad.exe), at a certain time
-If that app is closed prematurely, relaunch it
-Close the app at a certain time

I can launch the app using sytem("notepad.exe") or whatever

but, I do not know where to go from there. I do not use MFC, (I just write what I need using some windows libraries) So, If that's what it takes pleas be explicit.

thanks in advance
 
Well here's a little proggi which you can hopefully develop further, it does all you want in a simple sort of way:


#include "stdafx.h"
#include <iostream>
using namespace std;

#define TA_FAILED 0
#define TA_SUCCESS_CLEAN 1
#define TA_SUCCESS_KILL 2
#define TA_SUCCESS_16 3

struct ProgData
{
HANDLE hProc;
DWORD PID;
char* prog;
};

// Stop prog with WM_CLOSE if main window(s) found
BOOL CALLBACK termProgEnum(HWND hwnd,LPARAM lParam)
{
DWORD PID;

GetWindowThreadProcessId(hwnd, &PID);
//cout << &quot;hWnd: &quot; << hwnd << &quot; PID: &quot; << PID << &quot; sought: &quot; << ((ProgData*)lParam)->PID << endl;
if (PID==((ProgData*)lParam)->PID)
{
CloseHandle(((ProgData*)lParam)->hProc);
((ProgData*)lParam)->hProc = NULL;
PostMessage(hwnd, WM_CLOSE, 0, 0);
}
return TRUE;
}

// Terminate the process with WM_CLOSE or TerminateProcess() after timeout interval
int WINAPI termProg(ProgData& pd,DWORD timeout)
{
DWORD ret;
EnumWindows((WNDENUMPROC)termProgEnum,(LPARAM)&pd);

if (WaitForSingleObject(pd.hProc,timeout)!=WAIT_OBJECT_0)
ret = (TerminateProcess(pd.hProc,0)?TA_SUCCESS_KILL:TA_FAILED);
else
ret = TA_SUCCESS_CLEAN;
SYSTEMTIME time;
GetLocalTime(&time);
cout << &quot;Prog ended at &quot; << time.wHour << &quot;:&quot; << time.wMinute << &quot; code: &quot; << ret << endl;

return ret ;
}

// Start process if not running
int startProg(ProgData& pd)
{
SECURITY_ATTRIBUTES sa;
STARTUPINFO si;
PROCESS_INFORMATION pi;
DWORD creationFlags = 0;

SYSTEMTIME time;
GetLocalTime(&time);

if ((!pd.hProc)||(WaitForSingleObject(pd.hProc,0)==WAIT_OBJECT_0))
{
// Not running, start the process
ZeroMemory(&sa,sizeof(sa));
sa.nLength = sizeof(sa);
ZeroMemory(&si,sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi,sizeof(pi));
int ret = CreateProcess(NULL,pd.prog,&sa,&sa,FALSE,creationFlags,NULL,NULL,&si,&pi);
if (ret)
{
cout << &quot;Prog started at &quot; << time.wHour << &quot;:&quot; << time.wMinute << endl;
pd.hProc = pi.hProcess;
pd.PID = pi.dwProcessId;
}
else
{
cout << &quot;Prog started failed at &quot; << time.wHour << &quot;:&quot; << time.wMinute << endl;
pd.hProc = NULL;
return -1;
}
}
else
cout << &quot;Prog is running at &quot; << time.wHour << &quot;:&quot; << time.wMinute << endl;

return 0;
}

// Check if prog running start or stop according to status and time
void checkRunning(int sh, int sm, int eh, int em, char* prog)
{
ProgData pd;
int ct;

pd.hProc = NULL;
pd.PID = 0;
pd.prog = prog;

SYSTEMTIME time;
while (true)
{
GetLocalTime(&time);
cout << &quot;Current time: &quot; << time.wHour << &quot;:&quot; << time.wMinute << endl;
cout << &quot;Start at: &quot; << sh << &quot;:&quot; << sm << endl;
cout << &quot;End at: &quot; << eh << &quot;:&quot; << em << endl;

ct = time.wHour*60 + time.wMinute;
if ((ct>= (sh*60+sm))&&(ct<(eh*60+em)))
{
//cout << &quot;Check running&quot; << endl;
if (startProg(pd)) return;
}
else
if (pd.hProc)
{
//cout << &quot;Stop program&quot; << endl;
termProg(pd,5000);
}
Sleep(30000); // Check at 30sec intervals
}
}

int main(int argc, char* argv[])
{
checkRunning(12,15,13,20,&quot;notepad.exe&quot;);
return 0;
}
:) Hope that this helped! ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top