• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3629
  • Last Modified:

VB closing a running exe

Hello all is it possible through a exe made by visual basic to close down a running exe for instantce closing aim?
2 Solutions
Ryan ChongCommented:
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

use the psapi.dll calls.
FwankybAuthor Commented:
where can i get psapi.dll?
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.

FwankybAuthor Commented:
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
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.

FwankybAuthor Commented:
I will probally need help with this but im going to try it and let u know
FwankybAuthor Commented:
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
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.

FwankybAuthor Commented:
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?
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not unless you want to resort to such things as renaming the file so it doesn't have a .EXE, .COM, or .BAT extension.

FwankybAuthor Commented:
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?
Mike TomlinsonMiddle School Assistant TeacherCommented:

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.


FwankybAuthor Commented:
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

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now