Results 1 to 9 of 9

Thread: Stop running applications

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Sweden - land of the mooses
    Posts
    44

    Question Stop running applications

    Hi there!

    I want to check if an application (in this case winamp) is running, and then stop it. Is that possible?

    Either autoclose it, or detect the process and then bring up a dialog where I tell the user to quit winamp. I mean.

    Cheers!
    /Hoffe

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Yep, you just pass this function the windows class name and the app name and it does the rest for you. The example closes excel, but you can apply it to anything .

    Code:
    Option Explicit
    
    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
    
    Private Sub Command1_Click()
     Call CloseApplication("xlmain", "Excel") 'closes excel
    End Sub
    
    Public Function CloseApplication(ByVal sClassName$, ByVal sAppName$) As Boolean
     Dim happ As Long
     If FindWindow(sClassName, vbNullString) <> 0 Then
        If MsgBox("confirm you would like To quit " & sAppName, vbOKCancel) = vbOK Then
            Do
                happ = FindWindow(sClassName, vbNullString)
                If happ = 0 Then Exit Do
                PostMessage happ, WM_CLOSE, 0, 0
            Loop
        End If
     End If
    End Function

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Sweden - land of the mooses
    Posts
    44
    Ow, looks great! Thanx! But, how do I find out the classname for winamp? Ive tried winamp, Winamp and winamp.exe. But without result.

    Im, as you probable know by now, very fresh when it comes to Windows-programming.
    /Hoffe

  4. #4
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Unzip the attached and run it. Open winamp and put the mouse over the title bar. Get the windows class name and that's it.
    Attached Files Attached Files

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Sweden - land of the mooses
    Posts
    44
    Flowers and cudos (and a beer if you want to)! Thanx alot!
    /Hoffe

  6. #6
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    Nice to have someone so appreciative, makes it worthwhile.
    [IMG]d:\data\similies\roll&Smile.gif[/IMG]

  7. #7
    Addicted Member cyberwarpy's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    200

    Talking

    Code that goes into the FORM (You can find the location of Winamp via VB by using the Winamp Registry settings...):
    Code:
    Private Sub Form_Load()
    ''
    ''
       Call KillApp("C:\Program Files\Winamp\Winamp.exe")
    ''
    ''
    End Sub
    Code that goes into the module:

    Code:
    Private Declare Function ProcessFirst Lib "kernel32" _
    Alias "Process32First" (ByVal hSnapshot As Long, uProcess _
    As PROCESSENTRY32) As Long
    
    Private Declare Function ProcessNext Lib "kernel32" _
    Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As _
    PROCESSENTRY32) As Long
    
    Private Declare Function CreateToolhelpSnapshot Lib "kernel32" _
    Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
    lProcessID As Long) As Long
    
    Private 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
    
    Private Const MAX_PATH& = 260
    
    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
    Last edited by cyberwarpy; May 11th, 2001 at 05:32 AM.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2001
    Location
    Sweden - land of the mooses
    Posts
    44
    Thankya! I luuuuve this forum!

    /Hoffe - l33t VB-h4xx0r
    /Hoffe

  9. #9
    Addicted Member cyberwarpy's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne, Australia
    Posts
    200


    Originally posted by Hoffe
    Thankya! I luuuuve this forum!

    /Hoffe - l33t VB-h4xx0r

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