Click here to Skip to main content
15,895,606 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is there a way to run a loop that would check to see if an application is running? I have this so far. It's in a windows console program. I'm using Visual Studio 2010. Its saying define _AFXDLL.

C++
#include "stdafx.h"
#include <afx.h>
int main()
{
HANDLE hd;
LPCWSTR mu = (LPCWSTR)"Notepad";
hd = CreateMutex( NULL, FALSE, mu);
if ( GetLastError() == ERROR_ALREADY_EXISTS )
{
//MessageBox::Show("The program is already running.");
return 1;
}
CloseHandle( hd );
Posted
Updated 8-Sep-11 20:20pm
v6
Comments
Sergey Alexandrovich Kryukov 8-Sep-11 21:27pm    
Why loop?
--SA
Member 7766180 8-Sep-11 21:29pm    
Ok no loop! Any suggestions?
Sergey Alexandrovich Kryukov 8-Sep-11 21:38pm    
Sure. See the article I referenced. Good luck.
--SA
Emilio Garavaglia 9-Sep-11 2:20am    
removed spurious html tags

Use this:

AM just hardcoded and comparing whether internet.exe is running!
like this you do it dynamically to determing any service running. also you may hav arguments passed from command line.
CSS




#include "afxwin.h"
# include <wtsapi32.h>
# include <iostream>

#pragma comment(lib,"wtsapi32.lib")

using namespace std;

DWORD pCount = 0; 
	PWTS_PROCESS_INFO ppProcessInfo,pProcess;
	WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE,0,1,&ppProcessInfo,&pCount);
	pProcess = ppProcessInfo;
        CString internet = "iexplore.exe";

	for(int i = 0; i < pCount; i++)
	{
		pProcess->pProcessName;
		if (internet.CompareNoCase ( pProcess->pProcessName) == 0)
		{
			cout<< "iexplore running";
		}
		pProcess++;
	}</iostream></wtsapi32.h>
 
Share this answer
 
You can use WMI to watch any process and get notification on system events related to processes like creation and termination. See this CodeProject article: Process Information and Notifications using WMI[^].

—SA
 
Share this answer
 
Comments
Member 7766180 9-Sep-11 1:02am    
Thank You.
Sergey Alexandrovich Kryukov 9-Sep-11 13:04pm    
You are welcome.
If you agree that it makes sense, please formally accept this answer (green button) -- thanks.
Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900