How To Get Info from another program?

i need to print info about another program in my program..
by another program i mean software..

like, the sofware is named "server.exe" ..

i need to print status about that software in my program.. but how can i check if server.exe is currently running?

- - - - -

if server.exe IsRunning==True cout<<"Online";
else cout<<"Offline";

if you know what i mean...
closed account (13bSLyTq)
Hi,

You can use following code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

DWORD getPid(string procName){
   HANDLE hsnap;
   PROCESSENTRY32 pt;
   hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
   pt.dwSize = sizeof(PROCESSENTRY32);
   do{
		  if(!strcmp(pt.szExeFile, procName.c_str())){
			 DWORD pid = pt.th32ProcessID;
			 CloseHandle(hsnap);
			 return pid;
		  }
   } while(Process32Next(hsnap, &pt));
   CloseHandle(hsnap);
   return 0;		  
}


Remarks:

If the return is NULL (0) the process does not exist, else you will get a non-zero value (the PID of the process).

OR

you could find a mutex it creates and compare the mutex against it.
if the window is visible and maximized, you can easily get it's handle like this

1
2
3
	string sWinName;
	getline(cin, sWinName);
	HWND hWnd = FindWindow(NULL, sWinName.c_str());


if the window is not visible and it's just under processes, then you can use something like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void KillProcess(string str)
{
	HANDLE hProcessSnap;
	HANDLE hProcess;
	PROCESSENTRY32 pe32;

	hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if(hProcessSnap == INVALID_HANDLE_VALUE)
		return;
	
	pe32.dwSize = sizeof(PROCESSENTRY32);

	if(!Process32First(hProcessSnap, &pe32))
	{
		CloseHandle(hProcessSnap);
		return;
	}

	do
	{
		if(pe32.szExeFile == str)
		{
			hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID);
			if(hProcess == NULL)
				return; // error
			else
				TerminateProcess(hProcess, 0); // we found process we wanted to find - close it
		}
	}
	while(Process32Next(hProcessSnap, &pe32));

	CloseHandle(hProcessSnap);
	CloseHandle(hProcess);
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <string>

using namespace std;


DWORD getPid(string procName){
   HANDLE hsnap;
   PROCESSENTRY32 pt;
   hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
   pt.dwSize = sizeof(PROCESSENTRY32);
   do{
		  if(!strcmp(pt.szExeFile, procName.c_str())){
			 DWORD pid = pt.th32ProcessID;
			 CloseHandle(hsnap);
			 return pid;
		  }
   } while(Process32Next(hsnap, &pt));
   CloseHandle(hsnap);
   return 0;		  
}

int main(){
	getPid("server.exe");

	return 0;
}


bold words are telling me that they are identifiers undefined .. do i need to include something else?
Last edited on
Yes, you need windows.h and TlHelp32.h.

As a note, that code works, but it could give you wrong results if there are multiple processes called "server.exe" that is running from different locations.
thanks
Topic archived. No new replies allowed.