SRL Archive - C/C++ Help and Tutorials

Run exe only if cant locate a task


tom99

2010-03-04 08:50:54

Can somone give me a code witch will see if "myprocess.exe" is running and if it is then the exe will terminate but if it dont find it then it will run the code witch is winthin the c++ code, how can this be done?

x[Warrior]x3500

2010-03-04 23:16:08

do you run windows?

if so, do you want the exe to be running? or is that a bad thing?

if i were you, i would play around with ShellExecuteEx(); (http://msdn.microsoft.com/en-us/library/bb762154%28VS.85%29.aspx)

since it is a BOOL, you can do a lot with it (if/then statement wise)

i'm sry i could not provide better code. could not find any way of actually analyzing all the exes that are currently running.


EDIT: go to http://www.cpplc.net/forum/ if you need any help on cpp stuffs. (that is what i do)

tom99

2010-03-08 03:54:45

I have figured out if you put this at the end of the code then it will prevent it from running the code witch the c++ contain but the thing is i want it to not run if there is another program running, i cant figure it out :S


#ifndef LimitSingleInstance_H
#define LimitSingleInstance_H

#include <windows.h>

//This code is from Q243953 in case you lose the article and wonder
//where this code came from.
class CLimitSingleInstance
{
protected:
DWORD m_dwLastError;
HANDLE m_hMutex;

public:
CLimitSingleInstance(TCHAR *strMutexName)
{
//Make sure that you use a name that is unique for this application otherwise
//two apps may think they are the same if they are using same name for
//3rd parm to CreateMutex
m_hMutex = CreateMutex(NULL, FALSE, strMutexName); //do early
m_dwLastError = GetLastError(); //save for use later...
}

~CLimitSingleInstance()
{
if (m_hMutex) //Do not forget to close handles.
{
CloseHandle(m_hMutex); //Do as late as possible.
m_hMutex = NULL; //Good habit to be in.
}
}

BOOL IsAnotherInstanceRunning()
{
return (ERROR_ALREADY_EXISTS == m_dwLastError);
}
};
#endif

tom99

2010-03-08 06:08:58

but i could test to bind it to the program i woud only allow to run once, but then i woud need to know the source of a program witch is just like this at scar:

begin
getself.hide
end.

but at c++ if you get what i mean, then bind the mutex app to the program and then see if allow to be runned duplicated times, worth a try ;D

tom99

2010-03-08 07:38:05

it will work with a code that stop if found a certain form titlename, not windowname because this utility dont has a windowname (its hided from taskbar)

so if it can search for a form title name and then if that name is there then application will not start, but if it dont find it then it will.

woud this be possible atleast ?

silentwolf

2010-03-08 08:13:30

http://msdn.microsoft.com/en-us/library/ms682623%28VS.85%29.aspx