?
Solved

How can I code for ending a process?

Posted on 2002-06-03
11
Medium Priority
?
145 Views
Last Modified: 2010-05-02
I want to end a process that may or may not be running, much like I would do if I had to do it through task manager.  I do NOT want to end an application, but a process.  Example please...
0
Comment
Question by:jackjeckyl
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 38

Expert Comment

by:PaulHews
ID: 7051824
Determine if an Application has Stopped Responding
http://www.thescarms.com/VBasic/AppResponse.asp

Other good process oriented examples here:
http://www.thescarms.com/vbasic/VBProcessRelated.asp
0
 
LVL 1

Expert Comment

by:jdc0724
ID: 7051949
Use WinAPI call "ExitProcess"  The exact declaration looks like this:

Declare sub ExitProcess Lib "Kernal32" (Byval uExitCode as Long).  

The specs also recommend posting a "WM_CLOSE" message to the main window of the application you wish to close.  This approach is apparently more politically correct then the "ExitProcess" suggestion I gave you first.
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 7051972
>Use WinAPI call "ExitProcess"  The exact declaration looks like this:

That will exit the CALLING process, not a specified separate running/hung process as required.  It will also terminate your instance of VB if you execute it from the IDE.

>The specs also recommend posting a "WM_CLOSE" message

Yes, that is explained in the example I posted.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Expert Comment

by:jdc0724
ID: 7051985
Yes, that is correct.
0
 
LVL 1

Expert Comment

by:Toad224
ID: 7052512
perhaps try TerminateProcess if you want to kill another app...

Good luck!
0
 

Author Comment

by:jackjeckyl
ID: 7060096
I don't know API worth a crap.  All I know is the code runs fine, just doesn't work correctly.  It looks for a process ID, I'd like it to search the entire processes running by name (BAI_One.exe) and if it finds it, kills it.  Can it be done?  Code example descriptive please!
0
 

Author Comment

by:jackjeckyl
ID: 7060387
reposting question
0
 
LVL 4

Expert Comment

by:ravi_raj123
ID: 7167646
Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lpExitCode As Long) As Long
Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Sub EndTask(ProcID As Long)
   Dim lpExitCode As Long
   Dim lCode As Long
   On Error GoTo e
   lCode = OpenProcess(PROCESS_ALL_ACCESS, 0&, ProcID)
   If lCode = 0 Then
       'writeLog "Could not Aquire Process Context- Application may not be running"
       ''//Rjf Always start if not running
       'StartProc strFullKey, vbNormalFocus
       '//------v1.1.0
       'CleanUp ProcID, strFullKey ' Clean up
       'Exit Sub
   End If
   'writeLog "Got Process Context(" & lCode & ")- Retrieving Exit Code"
   GetExitCodeThread lCode, lpExitCode
   'writeLog "Terminating Process (" & lCode & ")- " & strFullKey
   If TerminateProcess(lCode, lpExitCode) Then
       'writeLog "Process Terminated"
       'StartProc strFullKey, vbNormalFocus
   Else
       'writeLog "Terminate Process Failed "
   End If
   'CleanUp ProcID, strFullKey
Exit Sub
e:
'writeLog Err.Description & " EndTask"
End Sub
0
 
LVL 16

Expert Comment

by:twalgrave
ID: 7897003
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
- PAQ'd and points refunded
Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

twalgrave
Cleanup Volunteer
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 7941766
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question