Solved

How can I code for ending a process?

Posted on 2002-06-03
11
141 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +4
11 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
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!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 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