?
Solved

Exiting or Terminating a Process with no window

Posted on 2003-02-26
12
Medium Priority
?
235 Views
Last Modified: 2010-04-07
I can use lHwnd = GetWindow(lHwnd, GW_HWNDNEXT) to get the handle to a process's window and then use that handle in either of the following lines of code and it works ok:

bRet = PostMessage(lHwnd, WM_CLOSE, 0&, 0&)
ExitProcess lHwnd
bRet = TerminateProcess(lHwnd, 0)

But how do I exit or terminate a process for which there is no window and I haven't started the process so I don't have any handle?

 
0
Comment
Question by:ncw
[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
12 Comments
 
LVL 2

Expert Comment

by:IeuanJ
ID: 8024935
if you havent started the process how can you terminate it ???
0
 
LVL 1

Author Comment

by:ncw
ID: 8025124
I am hoping to enumerate all processes running and get a handle on one of them depending on eg it's exe name.
0
 
LVL 2

Expert Comment

by:dankennedy
ID: 8026039
I think you're looking to use the EnumProcesses API. API-Guide actually has a VB example of almost exactly what you're looking for.
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 2

Expert Comment

by:IeuanJ
ID: 8026179
I knew I had something somewhere.  It's not exactly what your looking for, what this does is search through all current processes to find out if the named process is running and whether it is hung or not.  Not sure if this will meet your needs but I hope it does.

----------
Call
----------
'success[boolean] = (Process_Name[string],Me[Form])
success = TaskRunning(Process_Name, Me)

'TerminateTask = (Process_Name[string])
Call TerminateTask(Process_Name)

----------
Code
----------

Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Public Declare Function EnumWindows Lib "user32" (ByVal wndenmprc As Long, ByVal lParam As Long) As Long ' GetWindowText used above
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
Const GW_HWNDFIRST = 0
Const GW_HWNDNEXT = 2
Const WM_CLOSE = &H10
Const SMTO_ABORTIFHUNG = &H2
Const WM_NULL = &H0
Public msTarget As String
     
     
Public Function IsTaskHung(lCurrWnd As Long) As Boolean

    Dim lngResult As Long
    Dim lngRetVal As Long
   
    DoEvents
    lngRetVal = SendMessageTimeout(lCurrWnd, WM_NULL, 0&, _
         0&, SMTO_ABORTIFHUNG, 1000, lngResult)
    If lngRetVal Then
        IsTaskHung = False
    Else
        IsTaskHung = True
    End If
End Function

Public Function TaskRunning(sTitle As String, frmName As Form, Optional bTerminate As Boolean) As Integer
   Dim lCurrWnd As Long, lLength As Long, sTaskName As String, lParent As Long
   Dim returnInt As Integer
   returnInt = 0
   lCurrWnd = GetWindow(frmName.hwnd, GW_HWNDFIRST)
   While lCurrWnd <> 0
       lParent = GetParent(lCurrWnd)
       lLength = GetWindowTextLength(lCurrWnd)
       sTaskName = Space$(lLength + 1)
       lLength = GetWindowText(lCurrWnd, sTaskName, lLength + 1)
       sTaskName = Left$(sTaskName, Len(sTaskName) - 1)

       If lLength > 0 Then
           
           If InStr(1, UCase(sTaskName), UCase(sTitle)) <> 0 Then
               If IsTaskHung(lCurrWnd) = True Then
                   returnInt = 2
               Else
                   returnInt = 1
               End If
           End If
       End If
       lCurrWnd = GetWindow(lCurrWnd, GW_HWNDNEXT)
       DoEvents
   Wend
   TaskRunning = returnInt
End Function

Public Function EnumCallback(ByVal lAppHwnd As Long, ByVal lParam As Long) As Long
   Dim sBuf As String * 256, sTitle As String, lLength As Long
   lLength = GetWindowText(lAppHwnd, sBuf, Len(sBuf)) ' Get the window's title.
   sTitle = Left$(sBuf, lLength)
   If InStr(sTitle, msTarget) <> 0 Then SendMessage lAppHwnd, WM_CLOSE, 0, 0 ' if this is the target window, kill the window.
   EnumCallback = 1 ' Continue searching.
End Function

Public Sub TerminateTask(sAppName As String)
   msTarget = sAppName
   EnumWindows AddressOf EnumCallback, 0
End Sub
0
 
LVL 1

Author Comment

by:ncw
ID: 8032853
IeuanJ: I have similar code but it relies on having a window caption.

dankennedy: The AllApi Guide example for EnumProcesses unfortunately only includes code for NT and I'm using Win98. See http://www.allapi.net/apilist/EnumProcesses.shtml#

In the above example the following line seems to get the handle:
lngHwndProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lngProcessIDs(lngLoop))

Just needs to be adapted for Win98. If the code for NT is run on Win98 it fails at the following line:
lngReturn = EnumProcesses(lngProcessIDs(1), lngCBSize, lngCBSizeReturned)
0
 
LVL 1

Author Comment

by:ncw
ID: 8032936
EnumProcesses function requires the PSAPI.DLL library, I don't believe Win98 has this installed. I see that kernel32 library on Win98 contains the function OpenProcess.

Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long
0
 
LVL 2

Expert Comment

by:IeuanJ
ID: 8032991
you want to do it on 98 ???  Good luck, I've only tried it on NT/2000 so far.
0
 

Expert Comment

by:CleanupPing
ID: 8947121
ncw:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
0
 
LVL 1

Author Comment

by:ncw
ID: 8947978
Didn't find a solution to ending a process on Win98 so I propose that this question be deleted
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8972962
ncw, an EE Moderator will handle this for you.
Moderator, my recommended disposition is:

    Refund points and save as a 0-pt PAQ.

DanRollins -- EE database cleanup volunteer
0
 

Accepted Solution

by:
YensidMod earned 0 total points
ID: 8973230
Question is PAQ'd and points refunded.

YensidMod
EE Moderator
0

Featured Post

Independent Software Vendors: 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…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
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…
Suggested Courses
Course of the Month8 days, 18 hours left to enroll

764 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