Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to find an application running

Posted on 1999-08-03
14
Medium Priority
?
170 Views
Last Modified: 2010-04-30
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
0
Comment
Question by:default
[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
  • 8
  • 5
14 Comments
 
LVL 6

Accepted Solution

by:
setiawan earned 600 total points
ID: 1529221
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
                        ExeRunning = True
                        Exit Function
                    End If
                       
                    lRet = Process32Next(hProc, Proc)
                   
                Loop

            End Function


0
 
LVL 6

Expert Comment

by:setiawan
ID: 1529222
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
0
 

Author Comment

by:default
ID: 1529223
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Expert Comment

by:setiawan
ID: 1529224
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


0
 

Author Comment

by:default
ID: 1529225
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
0
 

Author Comment

by:default
ID: 1529226
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
0
 
LVL 6

Expert Comment

by:setiawan
ID: 1529227
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


0
 

Author Comment

by:default
ID: 1529228
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

0
 

Author Comment

by:default
ID: 1529229
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

0
 

Author Comment

by:default
ID: 1529230
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

0
 

Author Comment

by:default
ID: 1529231
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

0
 

Author Comment

by:default
ID: 1529232
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

0
 
LVL 6

Expert Comment

by:setiawan
ID: 1529233
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




0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6872147
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.

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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

704 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