BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How do I detect if a windows program is running

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API)
View previous topic :: View next topic  
Author Message
Robert E Mitchell, Sr.
Guest





PostPosted: Sun Jul 30, 2006 8:10 am    Post subject: How do I detect if a windows program is running Reply with quote



I sell a software package that starts several windows programs, but does not
always shut them down properly. If any of the programs are running, it
blocks the windows shutdown functionality. It also won't start the package
properly if any of the programs are already running. I get tired of going
out to my customers sites to go through the task manager to clean things up.
The manufacturer doesn't want to solve the problem, so I want to write a
program that will detect if any of the programs are already running, and if
they are, shut them down.

I need some advice on where to start finding if a program is running.

Any help would be greatly appreciated.

Thank you.

Robert E Mitchell, Sr.
Mill Creek, WA
Back to top
JD
Guest





PostPosted: Sun Jul 30, 2006 3:57 pm    Post subject: Re: How do I detect if a windows program is running Reply with quote



"Robert E Mitchell, Sr." <n2963p (AT) earthlink (DOT) net> wrote:
Quote:

[...] I need some advice on where to start finding if a
program is running.

The first thing that you'll need to do is build a list of the
offending applications and then translate that list into each
application's Window's ClassName.

To accomplish this, launch the offending application and use
the win32 API EnumWindows to enumerate all top level windows
and inspect their Window's ClassName. For example:

//-------------------------------------------------------------
BOOL CALLBACK EnumWindowsCallBack( HWND hWnd, LPARAM lParam )
{
char WindowClassName[256] = { 0 };
if( ::GetClassName(hWnd, WindowClassName, 255) )
{
ShowMessage( WindowClassName );
}
return TRUE;
}
//-------------------------------------------------------------
void __fastcall TForm1::Button1Click( TObject *Sender )
{
::EnumWindows( (WNDENUMPROC)EnumWindowsCallBack, NULL );
}
//-------------------------------------------------------------


Then, once you have the list built:
//-------------------------------------------------------------
BOOL CALLBACK EnumWindowsCallBack( HWND hWnd, LPARAM lParam )
{
char WindowClassName[256] = { 0 };
if( ::GetClassName(hWnd, WindowClassName, 255) )
{
if( stricmp(WindowClassName, (char*)lParam) == 0)
{
::SendMessage( hWnd, WM_QUIT, 0, 0 );
return FALSE;
}
}
return TRUE;
}
//-------------------------------------------------------------
void __fastcall TForm1::Button1Click( TObject *Sender )
{
int TotalOffenders = 2;
AnsiString ClassNames[ TotalOffenders ] = { "ThisOne", "ThatOne" };
for( int x = 0; x < TotalOffenders; ++x )
{
::EnumWindows( (WNDENUMPROC)EnumWindowsCallBack, (LPARAM)ClassNames[x].c_str() );
}
}
//-------------------------------------------------------------

Be aware that you may find one or 2 applications that don't
close because they don't have to if they don't want to. For
those, you'll need to use the win32 API TerminateProcess.

~ JD
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Mon Jul 31, 2006 5:29 am    Post subject: Re: How do I detect if a windows program is running Reply with quote



"Robert E Mitchell, Sr." <n2963p (AT) earthlink (DOT) net> wrote in message
news:44cc30ce$1 (AT) newsgroups (DOT) borland.com...

Quote:
I want to write a program that will detect if any of the
programs are already running

How To Enumerate Applications Using Win32 APIs
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q175030

Quote:
and if they are, shut them down.

Once you find the desired .exe process, you will have its process ID. You
can use OpenProcess() and TerminateProcess() to kill it.


Gambit
Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Native API) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.