🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #3212

Re: How do I detect if a windows program is running

#3212
From: 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
Technical Details
Message-ID: <44cc9084$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <44cc30ce$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,8821223f4f1a60c
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "JD" <nospam@nospam.com>
Sender: "JD" <nospam@nospam.com>
Reply-To: "JD" <nospam@nospam.com>
Subject: Re: How do I detect if a windows program is running
Newsgroups: borland.public.cppbuilder.nativeapi
X-User-Info: 205.188.116.136     
X-Forwarded-For: 172.133.179.179
References:  <44cc30ce$1@newsgroups.borland.com>
NNTP-Posting-Host: 207.105.83.66
Message-ID: <44cc9084$1@newsgroups.borland.com>
Date: 30 Jul 2006 03:57:08 -0700
X-Trace: newsgroups.borland.com 1154257028 207.105.83.66 (30 Jul 2006 03:57:08 -0700)
Lines: 65
Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail