Solved

How to kill running .exe using vb6?

Posted on 2011-09-12
13
2,386 Views
Last Modified: 2012-05-12
I want to kill .exe running on my computer... I mannulaly end the process in Windows task Manager where the .exe is found. I need to do this with vb6. How?
0
Comment
Question by:Whing Dela Cruz
  • 6
  • 4
  • 3
13 Comments
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36525704
Did you try Kill C:\PathToMyApp\MyApp.Exe
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36525725
Sorry, ignore that. It kills the file and not the running app <blush>
0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36525727
0
 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36525749
I have an app that uses the FindWindowsA to rotate the focus applications but have never used the Terminate process - let me know if it does what you need :)
0
 

Author Comment

by:Whing Dela Cruz
ID: 36525787
Hi! Martinlisss, Its work... but sorry I've got a big mistake of my question. What i need is not to kill but to stop the running .exe. or to close the running .exe.
0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36525910
My suggestion was wrong. You should look at Brook's.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Whing Dela Cruz
ID: 36584055
Hi Brook1966, I need to stop or end process the running program using vb6. How to start? There is an .exe running in my pc and i need to stop it . What i did now.. is mannualy end the process tru windows tast manager.
0
 
LVL 45

Accepted Solution

by:
Martin Liss earned 400 total points
ID: 36584073
Option Explicit
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_CLOSE = &H10

Private Sub SomeSub()
    Dim WindowName As String

    Dim Window As Long
    
        WindowName = InputBox("What window do you want to close?", "What Window")

        Window = FindWindow(vbNullString, WindowName)
    
        SendMessage Window, WM_CLOSE, 0, vbNullString
    

End Sub 

Open in new window

0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36584078
BTW I didn't come up with that code and I haven't tested it.
0
 

Author Comment

by:Whing Dela Cruz
ID: 36587323
I tried MartinLiss but its not working. What shall I do?
0
 
LVL 14

Assisted Solution

by:Brook Braswell
Brook Braswell earned 100 total points
ID: 36587466
Erwin - do you have the source code for the running application that you wish to STOP ?

The solution I gave above is the same as using the TaskManager - it does not delete the application which is what Kill is associated with.  If you want the application to STOP ( continue running but stop processing ) then you would need to implement a stop switch inside that application which would require you having the source code to it.  If you do not want the application to run at all then this would do it.
Option Explicit
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Type LUID
   LowPart As Long
   HighPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
   pLuid As LUID
   Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
   PrivilegeCount As Long
   TheLuid As LUID
   Attributes As Long
End Type


Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'Purpose   :    Terminates a process given a process ID or a the handle to a form.
'Inputs    :    [lProcessID]          The process ID (or PID) to terminate.
'          [lHwndWindow]          Any window handle belonging to the application.
'Outputs   :    Returns True on success.
'Notes     :    In WIN NT, click the "Processes" tab in the "Task Manager"
'          to see the process ID (or PID) for an application.
'          Must specify either lHwndWindow or lProcessID.
'          Equivalent to pressing Alt+Ctrl+Del then "End Task"

Function ProcessTerminate(Optional lProcessID As Long, Optional lHwndWindow As Long) As Boolean
   Dim lhwndProcess As Long
   Dim lExitCode As Long
   Dim lRetVal As Long
   Dim lhThisProc As Long
   Dim lhTokenHandle As Long
   Dim tLuid As LUID
   Dim tTokenPriv As TOKEN_PRIVILEGES, tTokenPrivNew As TOKEN_PRIVILEGES
   Dim lBufferNeeded As Long
   
   Const PROCESS_ALL_ACCESS = &H1F0FFF, PROCESS_TERMINATE = &H1
   Const ANYSIZE_ARRAY = 1, TOKEN_ADJUST_PRIVILEGES = &H20
   Const TOKEN_QUERY = &H8, SE_DEBUG_NAME As String = "SeDebugPrivilege"
   Const SE_PRIVILEGE_ENABLED = &H2

   On Error Resume Next
   If lHwndWindow Then
       'Get the process ID from the window handle
       lRetVal = GetWindowThreadProcessId(lHwndWindow, lProcessID)
   End If
   
   If lProcessID Then
       'Give Kill permissions to this process
       lhThisProc = GetCurrentProcess
       
       OpenProcessToken lhThisProc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, lhTokenHandle
       LookupPrivilegeValue "", SE_DEBUG_NAME, tLuid
       'Set the number of privileges to be change
       tTokenPriv.PrivilegeCount = 1
       tTokenPriv.TheLuid = tLuid
       tTokenPriv.Attributes = SE_PRIVILEGE_ENABLED
       'Enable the kill privilege in the access token of this process
       AdjustTokenPrivileges lhTokenHandle, False, tTokenPriv, Len(tTokenPrivNew), tTokenPrivNew, lBufferNeeded

       'Open the process to kill
       lhwndProcess = OpenProcess(PROCESS_TERMINATE, 0, lProcessID)
   
       If lhwndProcess Then
         'Obtained process handle, kill the process
         ProcessTerminate = CBool(TerminateProcess(lhwndProcess, lExitCode))
         Call CloseHandle(lhwndProcess)
       End If
   End If
   On Error GoTo 0
End Function


'Example of how to close excel using VBA or VB referencing Excel type library
Sub TestVBA()
   Dim lHwnd As Long
   'Make Excel's caption unique, use in VBA
   Application.Caption = "TEST EXCEL"
   'VB CODE
   'Find Excel's window handle
   lHwnd = FindWindow("XLMAIN", Application.Caption)
   'Terminate the process
   ProcessTerminate , lHwnd
End Sub


'Example of how to close excel using VB
Sub TestVB()
   Dim lHwnd As Long
   'Find Excel's window handle
   lHwnd = FindWindow("XLMAIN", vbNullString)
   'Terminate the process
   ProcessTerminate , lHwnd
End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:Martin Liss
ID: 36587614
@ERWIN it works for me. You need to enter the caption from the form that is running.
0
 

Author Closing Comment

by:Whing Dela Cruz
ID: 36601476
Thanks a lot... its working
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now