Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can I shutdown applications using a vb application

Status
Not open for further replies.

bigracefan

Programmer
Apr 2, 2002
304
US
I'm using the following code to start up applications that have shut down:

Private Sub Verify_Running()
Dim i As Integer
Dim AppName(20) As String
Dim MyAppInstances As Integer


For i = 0 To lstRequired.ListCount - 1

AppName(i) = ReportArray2(i)
piAppCount = 0
MyAppInstances = AppInstances(Get_Title(UCase(AppName(i))), False)
If MyAppInstances = 0 Then
Run_Required (AppName(i))
End If
Next

End Sub
Public Function AppInstances(AppNamePart As String, _
Optional ExactMatchOnly As Boolean) As Integer

'PURPOSE: Counts the Number of Instances of a Given Application
'PARAMETERS:
'AppNamePart = Any Part of the WindowTitle for the App
'ExactMatchOnly (Optional): If you want to
'count only exact matches for AppNamePart,
'set this parameter to true

'RETURNS: Number of Running Instances
'EXAMPLE:
'dim iIEInstances as integer
'piAppCount = 0
'iIEInstances = AppInstances("Microsoft Internet Explorer")

Dim lRet As Long

psAppString = AppNamePart
pbExact = ExactMatchOnly

lRet = EnumWindows(AddressOf CheckForInstance, 0)
AppInstances = piAppCount
End Function

Private Function CheckForInstance(ByVal lhWnd As Long, ByVal _
lParam As Long) As Long

Dim sTitle As String
Dim lRet As Long
Dim iNew As Integer


sTitle = Space(255)
lRet = GetWindowText(lhWnd, sTitle, 255)

sTitle = StripNull(sTitle)

If sTitle <> "" Then
If pbExact Then
If sTitle = psAppString Then piAppCount = piAppCount + 1
Else
If InStr(sTitle, psAppString) > 0 Then _
piAppCount = piAppCount + 1
End If
End If

CheckForInstance = True

End Function

Private Function StripNull(ByVal InString As String) As String

'Input: String containing null terminator (Chr(0))
'Returns: all character before the null terminator

Dim iNull As Integer
If Len(InString) > 0 Then
iNull = InStr(InString, vbNullChar)
Select Case iNull
Case 0
StripNull = InString
Case 1
StripNull = ""
Case Else
StripNull = Left$(InString, iNull - 1)
End Select
End If

End Function



This application has a listbox of the applications that need to be running. I would now like to shut down the appliations. Is there a way to do this using lhWnd?

Or, is there a way to shut down the applications that I have in my listbox.

Thanks.
 

I use the following,


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 Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias _
"CreateToolhelp32Snapshot" (ByVal lFlags As Long, _
ByVal lProcessId As Long) As Long

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 Const PROCESS_TERMINATE = &H1
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const TH32CS_SNAPPROCESS As Long = 2&
Private Const MAX_PATH As Integer = 260
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long 'The processID
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * MAX_PATH 'The exe name of the process
End Type


Public Function FindAndKillProcess(strProcessName) As Boolean

Dim lhSnapShot As Long
Dim pe32 As PROCESSENTRY32
Dim lRet As Long
Dim lProcID As Long
Dim strProcName As String
Dim lhProcess As Long

'Get a picture of All the process
lhSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)

If lhSnapShot = 0 Then
'Unable to get a snapshot of processes"
FindAndKillProcess = False
Else
pe32.dwSize = Len(pe32)
lRet = ProcessFirst(lhSnapShot, pe32)
Do While lRet
strProcName = StripProcName(pe32.szExeFile)
'App.LogEvent "'" & CStr(pe32.th32ProcessID) & "'-'" & strProcName & "'"
If InStr(1, strProcName, strProcessName) > 0 Then

'kill the process
App.LogEvent "killing process: " & CStr(pe32.th32ProcessID) & _
"-" & strProcName

lhProcess = OpenProcess(PROCESS_TERMINATE, 0, pe32.th32ProcessID)

TerminateProcess lhProcess, 0
CloseHandle lhProcess

End If
lRet = ProcessNext(lhSnapShot, pe32)
Loop
CloseHandle lhSnapShot
FindAndKillProcess = True
End If

End Function

Private Function StripProcName(strProcName As String) As String

Dim strStrip As String

strStrip = Trim(strProcName)
While (Asc(Right(strStrip, 1)) < 32)
strStrip = Left(strStrip, (Len(strStrip) - 1))
Wend
StripProcName = strStrip

End Function
 
I've had some bad experiences with TerminateProcess (due to messaging, record locking, threads, whatever). Nowadays, I just declare:

Const WM_CLOSE = &H10

& use that in a PostMessage statement:

Result = PostMessage(hWnd, WM_CLOSE, Ø&, Ø&)

Since it's the moral equivalent of the Close command, it's not such a scorched-earth type of operation & doesn't come back to bite me in the tuckus.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top