🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
3 messages
3 total messages Started by Robert E Mitchell, Sr. Sun, 30 Jul 2006 04:09
How do I detect if a windows program is running
#3209
Author: Robert E Mitchell, Sr.
Date: Sun, 30 Jul 2006 04:09
17 lines
1613 bytes
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 
Re: How do I detect if a windows program is running
#3212
Author: JD
Date: Sun, 30 Jul 2006 10:57
64 lines
2978 bytes
"Robert E Mitchell, Sr." <n2963p@earthlink.net> wrote:
>
> [...] 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
Re: How do I detect if a windows program is running
#3213
Author: Remy Lebeau (TeamB)
Date: Mon, 31 Jul 2006 00:29
17 lines
1464 bytes
"Robert E Mitchell, Sr." <n2963p@earthlink.net> wrote in message
news:44cc30ce$1@newsgroups.borland.com...

> 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

> 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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads