🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Terminate a Process

#7618
From: Colin B Maharaj
Date: Mon, 17 Jan 2005 02:42
82 lines
3898 bytes
Hi Salvador,
Can't say I know much about VB, but I got me some code some time ago 
when I had to build an upgrade for a program I built. It closes my app 
then I can go through with the Upgrade process. Hope this helps....
You are using the last function
void TermAppByName(char *Name);.....
with the other two functions as helpers...


//------------------------------------------------------------------

    BOOL CALLBACK TerminateAppEnum( HWND hwnd, LPARAM lParam )
    {
       DWORD dwID ;

       GetWindowThreadProcessId(hwnd, &dwID) ;

       if(dwID == (DWORD)lParam)
       {
          //PostMessage(hwnd, WM_CLOSE, 0, 0) ;
          PostMessage(hwnd, WM_QUERYENDSESSION, 0, 0) ;
          PostMessage(hwnd, WM_ENDSESSION, 0, 0) ;
       }
       return TRUE ;
    }

/*----------------------------------------------------------------
   DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout )

   Purpose: Shut down a 32-Bit Process (or 16-bit process under Win95)
   Parameters: dwPID Process ID of the process to shut down.
    dwTimeout    Wait time in milliseconds before shutting down process.
    Return Value:
       TA_FAILED - If the shutdown failed.
       TA_SUCCESS_CLEAN - If the process was shutdown using WM_CLOSE.
       TA_SUCCESS_KILL - if the process was shut down with
          TerminateProcess().
       NOTE:  See header for these defines.
    ----------------------------------------------------------------*/

    DWORD WINAPI TerminateApp( DWORD dwPID, DWORD dwTimeout )
    {
       HANDLE   hProc ;
       DWORD   dwRet ;

       // If we can't open the process with PROCESS_TERMINATE rights,
       // then we give up immediately.
       hProc = OpenProcess(SYNCHRONIZE|PROCESS_TERMINATE, FALSE,
          dwPID);

       if(hProc == NULL)
       {
          return TA_FAILED ;
       }

       // TerminateAppEnum() posts WM_CLOSE to all windows whose PID
       // matches your process's.
       EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) dwPID) ;

       // Wait on the handle. If it signals, great. If it times out,
       // then you kill it.
       if(WaitForSingleObject(hProc, dwTimeout)!=WAIT_OBJECT_0)
          dwRet=(TerminateProcess(hProc,0)?TA_SUCCESS_KILL:TA_FAILED);
       else
          dwRet = TA_SUCCESS_CLEAN ;
       CloseHandle(hProc) ;
       return dwRet ;
    }

//--------------------------------------------------------------------
//e.g. TermAppByName("C++ Builder");
// The Name is what you see when you do the three finger salute. "CAD"
// in the applications tab.
//--------------------------------------------------------------------

    void TermAppByName(char *Name)
    {
     DWORD dwID ;
     HWND Application = FindWindow( NULL, Name);
     GetWindowThreadProcessId(Application, &dwID) ;
     TerminateApp(  dwID, 2000 );
    }
Technical Details
Message-ID: <41eb2625$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!solnet.ch!solnet.ch!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <41ea997d@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,c4e3da735366ab7e
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Date: Sun, 16 Jan 2005 22:42:55 -0400
From: Colin B Maharaj <noreply@myhost.com>
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8a4) Gecko/20040927
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Terminate a Process
References: <41ea997d@newsgroups.borland.com>
In-Reply-To: <41ea997d@newsgroups.borland.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 200.108.20.138
Message-ID: <41eb2625$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1105929765 200.108.20.138 (16 Jan 2005 18:42:45 -0800)
Lines: 82
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!solnet.ch!solnet.ch!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail