Solved

End Task

Posted on 2001-06-06
12
323 Views
Last Modified: 2010-05-02
When I load my application, I check to see if there is a previous instance already running.  If there is (and it's not responding), how do I End Task it (until I fix the error that caused the crash)?  I'm running NT4.
0
Comment
Question by:northeyk
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 2

Expert Comment

by:Microsoft
ID: 6160416
try blowing it up.



lol

sorry just messing.


i have no idea
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6160478
Do this to check:

Private Sub Form_Load()
If App.PrevInstance = True Then
Unload Me 'Unload if there's a previous instance if it
End If
End Sub

That's it!

glass cookie : )
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6160495
> Check for an instance of your crashed program (don't know if this is applicable).

View page for code:
http://161.58.186.98/tips/tip147.html

Description: Determining Which Tasks Are Running


Terminate a program

View page for code...
http://www.freevbcode.com/ShowCode.Asp?ID=2781

Description: End a Program, Given only its Window Caption

Tha'ts it!

glass cookie : )

0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 6160504
View page:
http://161.58.186.98/tips/tip2.html

Description: How can I close an application?

Please note that the code forgot this line (which is to be placed at the top of your code):

Public Const WM_CLOSE = &H10

That's all!

glass cookie : )
0
 
LVL 1

Expert Comment

by:dkaygee
ID: 6160510
This is an app I got from a user at this site. It is a VB app that has 1 form and 1 module:

This is the form code:

The form has 1 text box where you input the path of the app to kill and 3 cmd buttons. "Start Process", "End Process", and "Quit".

I was able to modify the code so that it worked with an existing application I was runing. I have not tested it on NT.

Option Explicit
Private Sub cmdStart_Click()
Dim sTemp As String
'
' Start the process.
'
sTemp = Trim$(txtProcess)
If sTemp = "" Then Exit Sub

glPid = Shell(sTemp, vbNormalFocus)
If glPid = 0 Then
    MsgBox "Could not start process", vbExclamation, "Error"
End If
End Sub
Private Sub cmdKill_Click()
Dim i As Long
'
' Enumerate all parent windows for the process.
'
Call fEnumWindows
'
' Send a close command to each parent window.
' The app may issue a close confirmation dialog
' depending on how it handles the WM_CLOSE message.
'
For i = 1 To colHandle.Count
    glHandle = colHandle.Item(i)
    Call SendMessage(glHandle, WM_CLOSE, 0&, 0&)
Next
End Sub
Private Sub cmdQuit_Click()
Unload Me
End Sub


This is the module code:
ption Explicit
Public glPid     As Long
Public glHandle  As Long
Public colHandle As New Collection

Public Const WM_CLOSE = &H10
Public Const WM_DESTROY = &H2

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long

Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long
Dim lParent    As Long
Dim lThreadId  As Long
Dim lProcessId As Long
'
' This callback function is called by Windows (from the EnumWindows
' API call) for EVERY top-level window that exists.  It populates a
' collection with the handles of all parent windows owned by the
' process that we started.
'
fEnumWindowsCallBack = 1
lThreadId = GetWindowThreadProcessId(hwnd, lProcessId)

If glPid = lProcessId Then
    lParent = GetParent(hwnd)
    If lParent = 0 Then
        colHandle.Add hwnd
    End If
End If
End Function



Public Function fEnumWindows() As Boolean
Dim hwnd As Long
'
' The EnumWindows function enumerates all top-level windows
' by passing the handle of each window, in turn, to an
' application-defined callback function. EnumWindows
' continues until the last top-level window is enumerated or
' the callback function returns FALSE.
'
Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd)
End Function

0
 
LVL 3

Expert Comment

by:jrspano
ID: 6160537
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:northeyk
ID: 6163561
Will not work in NT4.

Also, most of the sample code that has been referenced (ie. Planet Source Code) is all written for Win9x and/or 2000.  You get DLL errors when they are run in NT4.
0
 
LVL 3

Expert Comment

by:jrspano
ID: 6163677
both the terminateprocess and ExitProcess both work on win nt 3.1 and higher.  once you have the process id of the process they will kill it.  there is an example in the link above that shows you how to get the running processes on win 95/98/nt/2000, once you have it then terminate it.  be carefull though, the api stuff in msdn says that terminateprocess is dangerous, and can leave dll's hanging.
0
 

Author Comment

by:northeyk
ID: 6167347
jrspano.

Which download are your referring to on that page?  I assume it's "App List and Kill" (which references terminateprocess).  Running NT4, you get a Run-time Error '453' - Can't find DLL entry point RegisterServiceProcess in kernel32.

I tried four apps from the same page.  One runs (but just lists the exe's running), two of them give me the DLL error, and the other gives me Dr. Watson.
0
 
LVL 3

Expert Comment

by:jrspano
ID: 6167371
look at the list all running processes demo.  it will show you how to the the proccess on the machine.  then use exit or terminate process.  the RegisterServiceProcess
api he is using is unknown to me.  It isn't in msdn.  if you remove the lines it will list all running exe's though.  the kill doesn't work though. just look at these demo's to learn how to get the process id.  after that you just kill it.
0
 
LVL 3

Accepted Solution

by:
jrspano earned 200 total points
ID: 6167397
once you have the process handle you call
GetExitCodeProcess to get the exit code for the process

then call ExitProcess or TerminateProcess and pass in the exit code.  you have to have sufficient rights to the terminateprocess in  win nt and 2000 to do this

for getexitcodeprocess you have to have PROCESS_QUERY_INFORMATION rights and for terminate process you have to have PROCESS_TERMINATE rights on the process.  if your process is a vb one I'm not sure what rights are on it but I would think you should have the rights you need.  If you create the process yourself in say C++ you assign the rights when you open the process
0
 

Author Comment

by:northeyk
ID: 6168863
Works great.  Thanks.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
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…
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…

744 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

15 Experts available now in Live!

Get 1:1 Help Now