Close applications currently running

Is there a way to close "selected" applications that are currently running when doing an installation of software. I would like to have a predefined set of applications that I could shut down quick and easy (Like at the click of a mouse or run a script etc.).
bigguyncAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

James_stillmanCommented:
If you have the title of the window in question you can do this

Ok place a command button on your form and a text box leave them with their default names.
Place htis code in the command button click statement.


Dim winHwnd As Long
                Dim RetVal As Long

                winHwnd = FindWindow(vbNullString, Text1.Text)

                Debug.Print winHwnd

                If winHwnd <> 0 Then
                  RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
                  If RetVal = 0 Then
                    MsgBox "Error posting message."
                  End If
                Else
                  MsgBox Text1.Text + " is not open."
                End If

Place this code in a module:

Option Explicit


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

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

    Public Const WM_CLOSE = &H10

This will give you a little test project.

You will need to know the title of the window you want to close. Type it into the text box and the window with the selected title should close down.

I Hope this is kinda what you were looking for

Cheers

James
0
deightonprogCommented:
call exerunning for each of the programs you want to close

e.g

call exerunning("notepad.exe")
call exerunning("vladogran.exe")

 Option Explicit
Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long

Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long


Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long


Declare Function TerminateProcess Lib "kernel32" _
(ByVal hProcess As Long, ByVal uExitCode As Long) As Long



      Private Declare Function Process32First Lib "kernel32" _
      (ByVal hSnapshot As Long, lppe As _
      PROCESSENTRY32) As Long

      Private Declare Function Process32Next Lib "kernel32" _
      (ByVal hSnapshot As Long, lppe As _
      PROCESSENTRY32) As Long

      Private Declare Function CreateToolhelp32Snapshot Lib _
      "kernel32" (ByVal dwFlags As Long, _
      ByVal th32ProcessID As Long) As Long
     
Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
     

      Private 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 * 260
      End Type

      Private Const TH32CS_SNAPPROCESS = &H2&
      Private Const hNull = 0
Public Const WM_SYSCOMMAND = &H112
Public Const SC_CLOSE = &HF060
Public Const PROCESS_QUERY_INFORMATION = &H400
Public Const PROCESS_VM_READ = &H10
      Public Function ExeRunning(ByVal ExeFind As String) As Boolean

          Dim hProc As Long
          Dim lRet As Long, Proc As PROCESSENTRY32
          Dim andy As Long, sDeighton As String
          Dim sClean As String
          Dim IRETURNCODE As Long
          Dim ProcFromprocid As Long
          Dim lpExitCode As Long
         
             Const NILL = 0&
         
          hProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
           
          ExeRunning = False
           
          If hProc = 0 Then
              Exit Function
          End If
           
          Proc.dwSize = Len(Proc)

          lRet = Process32First(hProc, Proc)
           
          Do While lRet
           
              Debug.Print Proc.th32ProcessID
              sClean = Left(Proc.szExeFile, InStr(Proc.szExeFile, Chr(0)) - 1)
           
              sDeighton = ""
              For andy = Len(sClean) To 1 Step -1
               
                If Mid(sClean, andy, 1) = "\" Then Exit For
               
                sDeighton = Mid(sClean, andy, 1) & sDeighton
               
              Next
             
              If UCase(sDeighton) = UCase(ExeFind) Then
                  ExeRunning = True
                  ProcFromprocid = OpenProcess(PROCESS_QUERY_INFORMATION Or _
                  PROCESS_VM_READ, 0, Proc.th32ProcessID)
                 
                  Call GetExitCodeProcess(ProcFromprocid, lpExitCode)
                  Call TerminateProcess(ProcFromprocid, lpExitCode)
                 
                  Exit Function
              End If
               
              lRet = Process32Next(hProc, Proc)
           
          Loop

      End Function
0
crazymanCommented:
This will list all proccesses running,you could then send a WM_CLOSE to request that the app closes.

Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long

'Declare constants used by GetWindow.
Const GW_CHILD = 5
Const GW_HWNDFIRST = 0
Const GW_HWNDLAST = 1
Const GW_HWNDNEXT = 2
Const GW_HWNDPREV = 3
Const GW_OWNER = 4


Sub CheckTaskList()
Dim CurrWnd As Long
Dim Length As Long
Dim ListItem As String
On Error GoTo e
'Get the hWnd of the first item in the master list
'so we can process the task list entries (top-level only).
CurrWnd = GetWindow(frmMain.hwnd, GW_HWNDFIRST)
'Loop while the hWnd returned by GetWindow is valid.
While CurrWnd <> 0
'Get the length of task name identified by CurrWnd in the list.
    Length = GetWindowTextLength(CurrWnd)
    'Get task name of the task in the master list.
    ListItem = Space$(Length + 1)
    Length = GetWindowText(CurrWnd, ListItem$, Length + 1)
    'Get the next task list item in the master list.
    CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
    'Process Windows events.
    Debug.Print ListItem '< this is each title of all processes running
    DoEvents
Wend
Exit Sub
e:
debug.print err.description
End Sub

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

crazymanCommented:
Diegton i get this error with your code


Cant find DLL entry point  CreateToolhelp32Snapshot in Kernel32

are you sure that it is in kernal32?
0
SidgeCommented:
Well first go out of the installing without affecting it by pressing 'Alt+F4'.
Then press 'Ctrl+Alt+Delete' (Delete NOT backspace!)
Choose which program to close then press enter. (only works with Win98 or Win95)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James_stillmanCommented:
Or alternatively you can use VB's Human Class and add this sub to your code.

Private Sub CloseWindows(finger1 as finger, finger2 as finger, finger3 as finger,eyesIn as eyes,HandIn as hand)

finger1.pressKey "CTRL"
finger2.pressKey "ALT"
finger3.pressKey "DEL" 'NOT BACKSPACE

finger1.release
finger2.release
finger2.release

EyesIn.lookForApplicationThatYouWantToTerminate
Hand.MoveMouseToPosition eyes.position
Finger1.clickMouseButton VBLeft
EyesIn.LookForTerminateProccessButton
Hand.MoveMouseToPosition Eyes.Position
Finger1.ClickMouseButton VbLeft
end sub
0
crazymanCommented:
***Chuckle***
0
deightonprogCommented:
crazyman,

which vewrsion of Windows?  Works on my 95, but may be problems in NT
0
deightonprogCommented:
CreateToolhelp32Snapshot - I did some research

Only works in 95,98,2000

No NT - sorry.  
0
crazymanCommented:
Okay Cheers....
0
James_stillmanCommented:
bigguync

Ever thought about moving your question into the newbies section or just the straight windows section, Either that or the give the points to your mate section.


GGGGGrrrrrrrrrrrrrrrrrrrrrr.......
0
crazymanCommented:
>>>I would like to have a predefined set >>of applications that I                         >>could shut down

I would have thought you meant in code that is why you asked it in the VB section...didnt realise you just meant the user to do it.
0
bigguyncAuthor Commented:
Sorry, didn't know it was in the wrong section. tks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.