Solved

How can I code for ending a process?

Posted on 2002-06-03
11
136 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
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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Extract Data and filename Macro 20 99
Help in WHSCRIPT 9 41
MsgBox 2 38
MS Access Search and Replace Using VBA 6 42
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now