|
-
May 4th, 2000, 01:40 AM
#1
Thread Starter
PowerPoster
I'm making a process viewer and it works fine, but there are apps I cant close.
Also I'd like to know how to find out if a apllication responds.
I tried this to close a process:
Code:
Public Sub KillProcess( iProcess As ProcessEntry32 )
Dim Temp As Long
Dim hWnd As Long
'Find process and exit
hWnd = GetProcessHandle(iProcess)
While hWnd <> 0
Temp = SendMessage( hWnd, WM_CLOSE, 0, 0& )
'If I want to close some apps Temp is always 0...
If Temp = 0 Then
hWnd = GetProcessHandle( iProcess )
Else
hWnd = 0
End If
Wend
End Sub
I tries to find out about response like this:
Code:
Temp = SendMessageTimeout(GetProcessHandle( Process(A) ), WM_NULL, 0, 0, SMTO_ABORTIFHUNG Or SMTO_BLOCK, 2000, Temp )
If Temp = 0 Then
Caption = "Process #" & A & " doesn't response"
End If
Thanks in advance!
[Edited by Fox on 05-04-2000 at 08:41 PM]
-
May 4th, 2000, 02:56 AM
#2
Here's an example of what you're trying to do:
In a Module:
Code:
Public 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 Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
Public Declare Function Process32First Lib "Kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "Kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
Public Declare Function CloseHandle Lib "Kernel32" (ByVal Handle As Long) As Long
Public Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccessas As Long, ByVal bInheritHandle As Long, ByVal dwProcId As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, ByVal hModule As Long, ByVal ModuleName As String, ByVal nSize As Long) As Long
Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
Public Declare Function CreateToolhelp32Snapshot Lib "Kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function GetVersionEx Lib "Kernel32" Alias "GetVersionExA" (lpVersionInformation As OSVERSIONINFO) As Long
Public Declare Function TerminateProcess Lib "Kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Public Const PROCESS_TERMINATE = &H1
Public Const VER_PLATFORM_WIN32_WINDOWS = 1
Public Const PROCESS_QUERY_INFORMATION = 1024
Public Const PROCESS_VM_READ = 16
Public Const TH32CS_SNAPPROCESS = &H2
Public Function CheckVersion() As Long
Dim tOS As OSVERSIONINFO
tOS.dwOSVersionInfoSize = Len(tOS)
Call GetVersionEx(tOS)
CheckVersion = tOS.dwPlatformId
End Function
In a Form with a ListBox
Code:
Private Sub Form_Load()
PopulateList
End Sub
Private Sub PopulateList()
Dim aPID() As Long
Dim lProcesses As Long
Dim lProcess As Long
Dim lModule As Long
Dim sName As String
Dim iIndex As Integer
Dim bCopied As Long
Dim lSnapShot As Long
Dim tPE As PROCESSENTRY32
List1.Clear
If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
'Windows 9x
'Create a SnapShot of the Currently Running Processes
lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
If lSnapShot < 0 Then Exit Sub
tPE.dwSize = Len(tPE)
'Buffer the First Processes Info..
bCopied = Process32First(lSnapShot, tPE)
While bCopied
'While there are Processes List them..
List1.AddItem _
Right$("000" & tPE.th32ProcessID, 3) & " - " & _
Left$(tPE.szExeFile, InStr(proc.szExeFile, Chr(0)) - 1)
bCopied = Process32Next(lSnapShot, tPE)
List1.ItemData(List1.NewIndex) = tPE.th32ProcessID
Wend
Else
'Windows NT
'The EnumProcesses Function doesn't indicate how many Process there are,
'so you need to pass a large array and trim off the empty elements
'as cbNeeded will return the no. of Processes copied.
ReDim aPID(255)
Call EnumProcesses(aPID(0), 1024, lProcesses)
lProcesses = lProcesses / 4
ReDim Preserve aPID(lProcesses)
For iIndex = 0 To lProcesses - 1
'Get the Process Handle, by Opening the Process
lProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aPID(iIndex))
If lProcess Then
'Just get the First Module, all we need is the Handle to get
'the Filename..
If EnumProcessModules(lProcess, lModule, 4, 0&) Then
sName = Space(260)
Call GetModuleFileNameExA(lProcess, lModule, sName, Len(sName))
While InStr(sName, "\")
sName = Mid$(sName, InStr(sName, "\") + 1)
Wend
List1.AddItem Right$("000" & aPID(iIndex), 3) & " - " & sName
List1.ItemData(List1.NewIndex) = aPID(iIndex)
End If
'Close the Process Handle
lRet = CloseHandle(lProcess)
End If
Next
End If
End Sub
Private Sub List1_DblClick()
Dim lProcess As Long
If MsgBox("Are you sure you want to Terminate the Process: " & vbCrLf & vbCrLf & _
List1 & vbCrLf, vbYesNoCancel + vbQuestion, "Terminate Process") = vbYes Then
'Close the Process..
lProcess = OpenProcess(PROCESS_TERMINATE, 0, List1.ItemData(List1.ListIndex))
Call TerminateProcess(lProcess, 0&)
Call CloseHandle(lProcess)
PopulateList
End If
End Sub
I think you're confusing WindowHandles with ProcessHandles.
To force a Process to Close, use the TerminateProcess() API.
Regards,
- Aaron.
-
May 4th, 2000, 04:42 AM
#3
Thread Starter
PowerPoster
Ah right 
Thanks Aaron
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|