Results 1 to 7 of 7

Thread: shutting down a exe file from vb

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2001
    Posts
    14

    Question shutting down a exe file from vb

    pl any one help in this regard

    i want to close the exe whis has been opened using shell script.i want to close that exe from vb itself

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    This should do it for you:

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

    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    Private Const WM_CLOSE = &H10

    Dim CloseIt As Long
    CloseIt = FindWindow(vbNullString, "Caption Of Window To Be Closed")
    PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)

  3. #3
    Matthew Gates
    Guest
    The following code will close an application, just by stating it's exe location and exe name. But it does not seem to work for WinNT and I believe it does not work for Win2k.


    VB Code:
    1. Private Declare Function ProcessFirst Lib "kernel32" _
    2. Alias "Process32First" (ByVal hSnapshot As Long, uProcess As _
    3. PROCESSENTRY32) As Long
    4.  
    5. Private Declare Function ProcessNext Lib "kernel32" _
    6. Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As _
    7. PROCESSENTRY32) As Long
    8.  
    9. Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
    10. Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
    11. lProcessID As Long) As Long
    12.  
    13. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    14. hObject As Long) As Long
    15.  
    16. Private Declare Function OpenProcess Lib "kernel32" (ByVal _
    17. dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
    18. ByVal dwProcessId As Long) As Long
    19.  
    20. Private Declare Function TerminateProcess Lib "kernel32" (ByVal _
    21. hProcess As Long, ByVal uExitCode As Long) As Long
    22.  
    23. Private Const MAX_PATH = 260
    24.  
    25. Private Type PROCESSENTRY32
    26.     dwSize As Long
    27.     cntUsage As Long
    28.     th32ProcessID As Long
    29.     th32DefaultHeapID As Long
    30.     th32ModuleID As Long
    31.     cntThreads As Long
    32.     th32ParentProcessID As Long
    33.     pcPriClassBase As Long
    34.     dwFlags As Long
    35.     szexeFile As String * MAX_PATH
    36. End Type
    37.  
    38.  
    39.  
    40. Private Function KillApp(myName As String) As Boolean
    41.     Const PROCESS_ALL_ACCESS = 0
    42.     Dim uProcess As PROCESSENTRY32
    43.     Dim rProcessFound As Long
    44.     Dim hSnapshot As Long
    45.     Dim szExename As String
    46.     Dim exitCode As Long
    47.     Dim myProcess As Long
    48.     Dim AppKill As Boolean
    49.     Dim appCount As Integer
    50.     Dim i As Integer
    51.     On Local Error GoTo Finish
    52.     appCount = 0
    53.    
    54.     Const TH32CS_SNAPPROCESS As Long = 2&
    55.    
    56.     uProcess.dwSize = Len(uProcess)
    57.     hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    58.     rProcessFound = ProcessFirst(hSnapshot, uProcess)
    59.    
    60.     Do While rProcessFound
    61.         i = InStr(1, uProcess.szexeFile, Chr(0))
    62.         szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
    63.         If Right$(szExename, Len(myName)) = LCase$(myName) Then
    64.             KillApp = True
    65.             appCount = appCount + 1
    66.             myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
    67.             AppKill = TerminateProcess(myProcess, exitCode)
    68.             Call CloseHandle(myProcess)
    69.         End If
    70.         rProcessFound = ProcessNext(hSnapshot, uProcess)
    71.     Loop
    72.  
    73.     Call CloseHandle(hSnapshot)
    74. Finish:
    75. End Function

  4. #4
    Megatron
    Guest
    When using Hack's code, you'll sometimes have to send WM_QUIT as well.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Megatron is correct. My bad.

    Occassionally, WM_QUIT is necessary.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    My bad again (I need a vacation). If you are going to use WM_QUIT, you would need to declare it:

    Private Const WM_QUIT = &H12

  7. #7
    New Member
    Join Date
    Aug 2001
    Location
    Philippines
    Posts
    11

    Matthew Gates' code

    Your code works very well...I was just curious when I saw your code and I tried it...it really works well. But why does it not work with Win2K and WinNT??

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width