Results 1 to 5 of 5

Thread: ***---How To Terminate Programs Running---***

  1. #1

    Thread Starter
    Lively Member mykg4orce's Avatar
    Join Date
    Oct 2000
    Location
    CANADA
    Posts
    92

    Exclamation

    hello, how can i write a vb program to terminate a application running in the computer's background. I suppose this is like hacking because, I have a computer running a security program in the taskbar and it is taking up ram so can I terminate it from a vb form.

  2. #2
    Junior Member
    Join Date
    Aug 1999
    Posts
    16

    Cool

    It's pretty simple. Take a look:

    First you need to declare this in the general declrations section of your program:
    Code:
    Private Declare Function M_GetActiveWindow Lib "user32" _
    Alias "GetActiveWindow" () As Long
    
    Private Declare Function M_GetClassName Lib "user32" _
    Alias "GetClassNameA" (ByVal lHwnd As Long, ByVal lpClassName _
    As String, ByVal lMax As Long) As Long
    
    Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    
    Const GW_HWNDFIRST = 0
    Const GW_HWNDNEXT = 2
    Next, use this code to actually shut down a program (I chose Notepad for the example). The name is the name you'd find in the titlebar, or in the CTRL+ALT+DEL list:

    Code:
    Dim winHwnd As Long
    Dim RetVal As Long
    If TasksRunning(0) = "" Then Exit Sub
    winHwnd = FindWindow(vbNullString, "Notepad")
    RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
    It merely posts the close command to the window that you selected. Enjoy =)

    ===================
    - Jason Egan
    Entry-level programmer, VB Lover

  3. #3
    Guest
    If you know the exe:

    Code:
    Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function OpenProcess Lib "kernel32" _
      (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _
       ByVal dwProcessId As Long) As Long
    Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
    ByVal uExitCode As Long) As Long
    
    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 * MAX_PATH
        End Type
    
    
    Public Function KillApp(myName As String) As Boolean
        Const PROCESS_ALL_ACCESS = 0
        Dim uProcess As PROCESSENTRY32
        Dim rProcessFound As Long
        Dim hSnapshot As Long
        Dim szExename As String
        Dim exitCode As Long
        Dim myProcess As Long
        Dim AppKill As Boolean
        Dim appCount As Integer
        Dim i As Integer
        On Local Error GoTo Finish
        appCount = 0
        
        Const TH32CS_SNAPPROCESS As Long = 2&
        
        uProcess.dwSize = Len(uProcess)
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        rProcessFound = ProcessFirst(hSnapshot, uProcess)
        
        Do While rProcessFound
            i = InStr(1, uProcess.szexeFile, Chr(0))
            szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            If Right$(szExename, Len(myName)) = LCase$(myName) Then
                KillApp = True
                appCount = appCount + 1
                myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                AppKill = TerminateProcess(myProcess, exitCode)
                Call CloseHandle(myProcess)
            End If
            rProcessFound = ProcessNext(hSnapshot, uProcess)
        Loop
    
        Call CloseHandle(hSnapshot)
    Finish:
    End Function
    
    
    Usage
    
    Call KillApp("C:\Security\Security.exe")

  4. #4
    Lively Member
    Join Date
    Feb 1999
    Location
    Austin,TX,USA
    Posts
    98

    What is still missing?

    OK Mr smartguy <g>! I pasted your code in my project & I get "Sub or Function not defined" here at the PostMessage point! I'm guessing because there's no PostMessage Sub?? btw I had to change "FindWindow" to "GetWindow" to get this far...
    RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&))

    So what SHOULD it be? I can't figure out what else PostMessage could be in your code. Please help! I feel like I'm ALMOST there!

    btw the code I'm using to LAUNCH the media player is as follows:

    Dim ItemFile As String
    ItemFile = "First.MPG"
    Call ShellExecute(hwnd, "Open", ItemFile, "", App.Path & "\Items\", 1)

    thomas

  5. #5
    Junior Member
    Join Date
    Aug 1999
    Posts
    16

    Question

    Did you put the first chunk of code in the main form's general declarations section?

    Double click the form and put the code at the very top.
    - Jason Egan
    Entry-level programmer, VB Lover
    VB6 Enterprise Edition SP3
    http://jeprod.cjb.net/

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