Archived

This topic is now archived and is closed to further replies.

doctorsixstring

.EXE test

Recommended Posts

Oops, should have clarified more. What I mean is that I want to test to see if a particular application is running from within my program. I want to know if there is a function that will return true/false or whatever, given a particular *.exe file. I''m using Visual C++ 6.0.

Share this post


Link to post
Share on other sites
Certainly. Use the toolhelp API for Win95+ and NT5+:

CreateToolhelp32Snapshot
Process32First
Process32Next

or PSAPI for NT4+:

EnumProcesses

Edit: forgot about NT4!

[edited by - IndirectX on April 29, 2002 5:00:32 PM]

Share this post


Link to post
Share on other sites
Thanks IndirectX, that helped a lot. I now have a great little function to test if programs are running. In case anybody else ever needs something like this, I''ll post it here:


  
/////////////////////

// bool IsAppRunning( char *filename ) - tests to see if the given filename is currently

// running

/////////////////////

bool IsAppRunning( char *filenameIn, bool bReturnMessage )
{
char szTemp[64];

HANDLE processes = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );

PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(PROCESSENTRY32);

Process32First( processes, &processEntry);

while( GetLastError()!=ERROR_NO_MORE_FILES )
{
//extract filename from path

char *filename = PathFindFileName(processEntry.szExeFile);

if( strcmp( _strlwr(filename), _strlwr( filenameIn ) ) == 0)
{
if(bReturnMessage)
{
sprintf(&szTemp[0], "%s is currently running", _strlwr( filenameIn ) );
MessageBox(NULL, szTemp, "Process Found", MB_OK);
}

return true;
}

Process32Next( processes, &processEntry);
}

CloseHandle(processes);

if(bReturnMessage)
{
sprintf(&szTemp[0], "%s is not running.", _strlwr( filenameIn ) );
MessageBox(NULL, szTemp, "Process Not Found", MB_OK);
}

return false;
}

Share this post


Link to post
Share on other sites