Solved

How can I code for ending a process?

Posted on 2002-06-03
11
137 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now