VB closing a running exe

Posted on 2004-08-19
Last Modified: 2008-01-09
Hello all is it possible through a exe made by visual basic to close down a running exe for instantce closing aim?
Question by:Fwankyb
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
LVL 52

Accepted Solution

Ryan Chong earned 38 total points
ID: 11849079
Take a look on:

List All Active Processes and Allow Addition/Removal

End a Program, Given only its Window Caption

End All Instances of a Running Process

Hope this helps


Expert Comment

ID: 11849162
use the psapi.dll calls.

Author Comment

ID: 11849244
where can i get psapi.dll?
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.


Author Comment

ID: 11849252
actually i can be more specific now I want to be able to close novel or outlook express or any email application once it opens it will close and i want to be able to turn it on and off? any body have anyideas
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 37 total points
ID: 11849300
The following link will closed a process based on the Exe name:

All you would need to do is place a timer on a form and call the function for each application you want closed by passing the name of the Exe to the KillAppByEXEName() function in the timer event.

Let me know if you need help with this.


Author Comment

ID: 11856762
I will probally need help with this but im going to try it and let u know

Author Comment

ID: 11856829
Ok really what i need is that looks like it will work but what i really want it to do is not be on a timer i just want as soon as say somebody open outlook when this program is on it will close it
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11856999
As far as I know there is no way to do this without some kind of timer.   You can use the APIs to watch for a file change...but there is no API to watch for a process spawned from a executable.


Author Comment

ID: 11857196
well is there a way to stop a program before it even run say u click it and u want it to not run when u click it?
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11857219
Not unless you want to resort to such things as renaming the file so it doesn't have a .EXE, .COM, or .BAT extension.


Author Comment

ID: 11860423
hmm well ok then help me with the timer please to get a certain program to close after a certain amout of time passes right?
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11860517

The amount of time that passes before the "offending" application closes will be measured in mere milliseconds.  Be aware that if someone renames the EXE this approach will not work.  

In that case you have to get the ClassName of the app and poll all open windows to see if one is a match.



Author Comment

ID: 11862561
ok i did what u had at the other link here is the code and what i did

this is my module

Option Explicit

Public Declare Function TerminateProcess Lib "kernel32" ( _
    ByVal hProcess As Long, ByVal uExitCode As Long) As Long

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

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

Public Declare Function CloseHandle Lib "Kernel32.dll" _
    (ByVal Handle As Long) As Long

Public Declare Function OpenProcess Lib "Kernel32.dll" _
    (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, _
    ByVal dwProcId As Long) As Long

Public Declare Function EnumProcesses Lib "psapi.dll" _
    (ByRef lpidProcess As Long, ByVal cb As Long, _
    ByRef cbNeeded As Long) As Long

Public Declare Function GetModuleFileNameExA Lib "psapi.dll" _
    (ByVal hProcess As Long, ByVal hModule As Long, _
    ByVal ModuleName As String, ByVal nSize As Long) As Long

Public Declare Function EnumProcessModules Lib "psapi.dll" _
    (ByVal hProcess As Long, ByRef lphModule As Long, _
    ByVal cb As Long, ByRef cbNeeded As Long) As Long

Public Declare Function CreateToolhelp32Snapshot Lib "kernel32" ( _
    ByVal dwflags As Long, ByVal th32ProcessID As Long) As Long

Public Declare Function GetVersionExA Lib "kernel32" _
    (lpVersionInformation As OSVERSIONINFO) As Integer

    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long           ' This process
    th32DefaultHeapID As Long
    th32ModuleID As Long            ' Associated exe
    cntThreads As Long
    th32ParentProcessID As Long     ' This process's parent process
    pcPriClassBase As Long          ' Base priority of process threads
    dwflags As Long
    szExeFile As String * 260       ' MAX_PATH
End Type

    dwOSVersionInfoSize As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long           '1 = Windows 95   2 = Windows NT
    szCSDVersion As String * 128
End Type

Public Const PROCESS_VM_READ = 16
Public Const MAX_PATH = 260
Public Const TH32CS_SNAPPROCESS = &H2&
Public Const hNull = 0

Public Function StrZToStr(s As String) As String
    StrZToStr = Left$(s, Len(s) - 1)
End Function

Public Function getVersion() As Long
    Dim osinfo As OSVERSIONINFO
    Dim retvalue As Integer
    osinfo.dwOSVersionInfoSize = 148
    osinfo.szCSDVersion = Space$(128)
    retvalue = GetVersionExA(osinfo)
    getVersion = osinfo.dwPlatformId
End Function

Public Function KillAppByEXEName(NameOfEXE As String) As Boolean
    NameOfEXE = UCase(NameOfEXE)
    KillAppByEXEName = False ' Assume the worst
    Select Case getVersion()
        Case 1 'Windows 95/98
            Dim f As Long, sname As String
            Dim hSnap As Long, Proc As PROCESSENTRY32
            Dim ProcFromprocid As Long
            hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
            If hSnap = hNull Then
                Exit Function
            End If
            Proc.dwSize = Len(Proc)
            ' Iterate through the processes
            f = Process32First(hSnap, Proc)
            Do While f
                sname = StrZToStr(Proc.szExeFile)
                sname = UCase(Trim(sname))
                If InStr(sname, NameOfEXE) > 0 Then
                    ProcFromprocid = OpenProcess(PROCESS_QUERY_INFORMATION Or _
                        PROCESS_VM_READ, 0, Proc.th32ProcessID) ' get
                    Call TerminateProcess(ProcFromprocid, 0)
                    KillAppByEXEName = True
                    Exit Function
                End If
                f = Process32Next(hSnap, Proc)

        Case 2 'Windows NT
            Dim cb As Long
            Dim cbNeeded As Long
            Dim NumElements As Long
            Dim ProcessIDs() As Long
            Dim cbNeeded2 As Long
            Dim NumElements2 As Long
            Dim Modules(1 To 200) As Long
            Dim lRet As Long
            Dim ModuleName As String
            Dim nSize As Long
            Dim hProcess As Long
            Dim i As Long
            'Get the array containing the process id's for each process object
            cb = 8
            cbNeeded = 96
            Do While cb <= cbNeeded
                cb = cb * 2
                ReDim ProcessIDs(cb / 4) As Long
                lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
            NumElements = cbNeeded / 4

            For i = 1 To NumElements
                'Get a handle to the Process
                hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
                    Or PROCESS_VM_READ, 0, ProcessIDs(i))
                'Got a Process handle
                If hProcess <> 0 Then
                    'Get an array of the module handles for the specified
                    lRet = EnumProcessModules(hProcess, Modules(1), 200, _
                    'If the Module Array is retrieved, Get the ModuleFileName
                    If lRet <> 0 Then
                        ModuleName = Space(MAX_PATH)
                        nSize = 500
                        lRet = GetModuleFileNameExA(hProcess, Modules(1), _
                            ModuleName, nSize)
                        ModuleName = UCase(Trim(ModuleName))
                        If InStr(ModuleName, NameOfEXE) > 0 Then
                            Call TerminateProcess(hProcess, 0)
                            KillAppByEXEName = True
                            Exit Function
                        End If
                    End If
                End If
                'Close the handle to the process
                lRet = CloseHandle(hProcess)
    End Select
End Function

now here is the actual code
Private Sub Text1_Click(KeyAscii As Integer)
Dim Appname As String
Appname = "aim.exe"
    If KeyAscii = 13 Then
        If KillAppByEXEName(Appname) Then
            MsgBox "Killed!"
            MsgBox "Unable to find and/or kill"
        End If
        KeyAscii = 0
    End If
End Sub

 no message box appears either and the program does not close


Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction While answering a recent question ( 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…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month5 days, 17 hours left to enroll

627 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