Link to home
Start Free TrialLog in
Avatar of default
default

asked on

how to find an application running

hi friends...i want to find whether a particular application is running in a stand-alone or not and to terminate that application.
for example...if iexplore.exe is running in a machine , i want from my VB5.0 application , to close that iexplore.exe window and terminate that application.
i want this to be done very urgently...
reply asap
vj
ASKER CERTIFIED SOLUTION
Avatar of setiawan
setiawan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of setiawan
setiawan

Option 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



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
Avatar of default

ASKER

dear satiawan....this answer is quite confusing for me ...
can u give me a explanation for this code part where i've to give the application name for terminating???
thanx
vj
OK, sorry for the second example code that not clear
to check the process run or not
use ExeRunning("iexplorer.exe")
will return tru if running vice versa

for terminating iexplorer,
Call SendMessage(glHandle, WM_CLOSE, 0&, 0&)
where glHandle is handle of the parent window handle of the process want to terminate


Avatar of default

ASKER

satiawan ...its not working .....its giving error in 'Send Message' function.....can u pls give me exact events where i should write all these code???
pls help me..
thanx
vj
Avatar of default

ASKER

satiawan ...its not working .....its giving error in 'Send Message' function.....can u pls give me exact events where i should write all these code???
pls help me..
thanx
vj
Have u declare SendMessage Function ?

Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Call SendMessage after you get all parent window handle
of process

If you still confuse, I'll make it running code to solve your problem, OK


Avatar of default

ASKER

dear setiawan....can u make it still more simple ???
i want a text box in my vb application to get the name of the application to be terminated....after giving a enter that application should get terminated.
thanx
vj

Avatar of default

ASKER

dear setiawan....can u make it still more simple ???
i want a text box in my vb application to get the name of the application to be terminated....after giving a enter that application should get terminated.
thanx
vj

Avatar of default

ASKER

dear setiawan....can u make it still more simple ???
i want a text box in my vb application to get the name of the application to be terminated....after giving a enter that application should get terminated.
thanx
vj

Avatar of default

ASKER

dear setiawan....can u make it still more simple ???
i want a text box in my vb application to get the name of the application to be terminated....after giving a enter that application should get terminated.
thanx
vj

Avatar of default

ASKER

dear setiawan....can u make it still more simple ???
i want a text box in my vb application to get the name of the application to be terminated....after giving a enter that application should get terminated.
thanx
vj

yes, here it is
paste into notepad save as StopProcess.bas
Attribute VB_Name = "Module1"
Option 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


paste into notepad save as form1.frm
VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command1
      Caption         =   "&Stop"
      Height          =   375
      Left            =   1560
      TabIndex        =   2
      Top             =   1560
      Width           =   1215
   End
   Begin VB.TextBox Text1
      Height          =   375
      Left            =   2520
      TabIndex        =   0
      Top             =   240
      Width           =   1935
   End
   Begin VB.Label Label1
      Caption         =   "Exe Filename (eg. iexplore.exe)"
      Height          =   375
      Left            =   120
      TabIndex        =   1
      Top             =   360
      Width           =   2415
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Private Declare Function Process32First Lib "kernel32" _
(ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

Private Declare Function Process32Next Lib "kernel32" _
(ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long

Private Declare Function CreateToolhelp32Snapshot Lib _
"kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long

Private Type PROCESSENTRY32
             dwSize As Long
             cntUsage As Long
             th32ProcessID As Long
             th32DefaultHeapID As Long
             th32ModuleID As Long
             cntThreads As Long
             th32ParentProcessID As Long
             pcPriClassBase As Long
             dwFlags As Long
             szExeFile As String * 260
End Type

Private Const TH32CS_SNAPPROCESS = &H2&
Private Const hNull = 0
Public Function ExeRunning(ByVal ExeFind As String) As Boolean
    Dim hProc As Long, ExeName As String
    Dim lRet As Long, Proc As PROCESSENTRY32
    Dim g As Long, sDeighton As String
    Dim sClean As String
    Dim sAtom As String
    hProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    ExeRunning = False
                                             
    If hProc = 0 Then
        Exit Function
    End If
                   
    Proc.dwSize = Len(Proc)
    lRet = Process32First(hProc, Proc)
                   
    Do While lRet
       ExeName = Left(Proc.szExeFile, Len(Proc.szExeFile) - 1)
       sClean = ""
       For g = 1 To Len(ExeName)
           sAtom = Mid(ExeName, g, 1)
           If sAtom = Chr(0) Then
                  Exit For
           Else
                  sClean = sClean & sAtom
           End If
       Next
       sDeighton = ""
       For g = Len(sClean) To 1 Step -1
           If Mid(sClean, g, 1) = "\" Then Exit For
              sDeighton = Mid(sClean, g, 1) & sDeighton
       Next
                                         
       If UCase(sDeighton) = UCase(ExeFind) Then
            glPid = Proc.th32ProcessID
            ExeRunning = True
            Exit Function
       End If
                       
       lRet = Process32Next(hProc, Proc)
    Loop
End Function
Private Sub Command1_Click()
Dim i As Long
   If ExeRunning(UCase(Text1.Text)) Then
'
' 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 If
End Sub




GREETINGS!

This question was awarded, but never cleared due to the JSP-500 errors of that time.  It was "stuck" against userID -1 versus the intended expert whom you awarded.  This corrects the problem and the expert will now receive these points; points verified.

Please click on your Member Profile and select "View Question History" to navigate through any open or locked questions you may have to update and finalize them.  If you are an EE Pro user, you can also choose Power Search to find all your open questions.

This is the Community Support link, if help is needed, along with the link to All Topics which reflects many TAs recently added.

https://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
https://www.experts-exchange.com/jsp/zonesAll.jsp
 
Thank you,
Moondancer
Moderator @ Experts Exchange