Author Topic: Kill process else run code how  (Read 808 times)

Offline tom2

  • Jr. Member
  • **
  • Posts: 79
  • Rep: 0
    • View Profile
Kill process else run code how
« on: March 04, 2010, 05:32:37 am »
i woud like my c++ code to run only if "myprocess.exe" is not running else if it is then terminate it and run the c++ code.

Freddy1990.com

Kill process else run code how
« on: March 04, 2010, 05:32:37 am »

tvirusx1

  • Guest
Re: Kill process else run code how
« Reply #1 on: March 04, 2010, 08:00:08 am »
Is it necessary for you to search for the Process?

Is the Window Hidden of that Process?

If not then use FindWindow.

Code: [Select]
#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    HWND hwnd = FindWindow(NULL,"The Windows name");
    if(!hwnd)
        cout << "Could not find window!" << endl;
    else
        cout << "Found that Window!" << endl;
    cin.get();
return 0;
}

I remember working with Processes... it's a pain lol.

EDIT:
Depending on your compiler you might have to add something in front of the "The Windows name", eg
Code: [Select]
FindWindow(NULL,L"The Windows name");

EDIT2:
Checking the Process:
Code: [Select]
bool isRunning(string pName)
{
unsigned long aProcesses[1024], cbNeeded, cProcesses;
if(!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
return;

cProcesses = cbNeeded / sizeof(unsigned long);
for(unsigned int i = 0; i < cProcesses; i++)
{
if(aProcesses[i] == 0)
continue;

HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, aProcesses[i]);
char buffer[50];
GetModuleBaseName(hProcess, 0, buffer, 50);
CloseHandle(hProcess);
if(pName == string(buffer))
return true;
}
return false;
}

It can be used like this:
Code: [Select]
#include <iostream>
#include <windows.h>
#include <string.h>
using namespace std;

bool isRunning(string pName)
{
unsigned long aProcesses[1024], cbNeeded, cProcesses;
if(!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded))
return;

cProcesses = cbNeeded / sizeof(unsigned long);
for(unsigned int i = 0; i < cProcesses; i++)
{
if(aProcesses[i] == 0)
continue;

HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, 0, aProcesses[i]);
char buffer[50];
GetModuleBaseName(hProcess, 0, buffer, 50);
CloseHandle(hProcess);
if(pName == string(buffer))
return true;
}
return false;
}

int main()
{
    if(isRunning("myprocess.exe")==true)
        cout << "w00t its running" << endl;
    else
        cout << "no its not...!" << endl;
    cin.get();
return 0;
}

I did not test them so do Post if you get a Compiler Error.
« Last Edit: March 04, 2010, 08:12:09 am by tvirusx1 »

Freddy1990.com

Re: Kill process else run code how
« Reply #1 on: March 04, 2010, 08:00:08 am »

Offline tom2

  • Jr. Member
  • **
  • Posts: 79
  • Rep: 0
    • View Profile
Re: Kill process else run code how
« Reply #2 on: March 08, 2010, 04:37:55 am »
Didnt work, here is the results of compiling:

[scar]
 
Compiling...

Mybasemeth2.cs(1,2): error CS1024: Preprocessor directive expected
Mybasemeth2.cs(1,10): error CS1025: Single-line comment or end-of-line expected
Mybasemeth2.cs(2,2): error CS1024: Preprocessor directive expected
Mybasemeth2.cs(2,10): error CS1025: Single-line comment or end-of-line expected
Mybasemeth2.cs(3,2): error CS1024: Preprocessor directive expected
Mybasemeth2.cs(3,10): error CS1025: Single-line comment or end-of-line expected
Mybasemeth2.cs(4,7): error CS1041: Identifier expected; 'namespace' is a keyword
Mybasemeth2.cs(4,20): error CS1514: { expected
Mybasemeth2.cs(8,27): error CS1001: Identifier expected
Mybasemeth2.cs(15,20): error CS1518: Expected class, delegate, enum, interface, or struct
Mybasemeth2.cs(18,94): error CS1518: Expected class, delegate, enum, interface, or struct
Mybasemeth2.cs(19,15): error CS1001: Identifier expected
Mybasemeth2.cs(25,2): error CS0116: A namespace does not directly contain members such as fields or methods
Mybasemeth2.cs(26,1): error CS1022: Type or namespace definition, or end-of-file expected

Done.
[/scar]
Posted on: March 08, 2010, 12:17:52 AM
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.

[scar]
#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
      [/scar]
Posted on: March 08, 2010, 12:56:45 AM
what woud be similar to this:

begin
getself.hide
end.


but in c++
i tought of an idea but i need to know the code for that first what is it ?
Posted on: March 08, 2010, 03:24:02 AM
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 ?

Freddy1990.com

Re: Kill process else run code how
« Reply #2 on: March 08, 2010, 04:37:55 am »