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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find a Process not a window. 4

Status
Not open for further replies.

CasperTFG

Programmer
Nov 15, 2001
1,210
US
I can see whether or not a Service or a Windows program is runner through, the EnumServicesStatus, and FindWindow API's. However does anyone know of a way to find whether a process that is not a service and is not windowed is running.

The processes show up under the Task Managers Processes tab, an example of one would be. sqlservr.exe.

Here is the code that I use to test Window Programs. I imagine it would be similar for processes however I need something beside the FindWindow API to get the PID in order to use SendMessageTimeout.
[tt]
Public Function checkProgram(ByRef Program As String) As Long
On Error GoTo ErrorHandler
Dim myName As String
myName = "checkProgram(Program=" & Program & ")"

Dim hWindow As Long
Dim lngResult As Long
Dim lngReturnValue As Long

hWindow = FindWindow(vbNullString, Program)
If hWindow = 0 Then
checkProgram = 998
Exit Function
End If

lngReturnValue = SendMessageTimeout(hWindow, WM_NULL, 0&, _
0&, SMTO_ABORTIFHUNG And SMTO_BLOCK, 1000, lngResult)
If lngReturnValue = 0 Then
checkProgram = 11
Exit Function
End If

'-1 response
DoEvents
hWindow = FindWindow(vbNullString, Program)
If IsWindow(hWindow) = 1 Then
checkProgram = 0
Else
checkProgram = 998
End If

Exit Function
ErrorHandler:
checkProgram = 1000
Call GlobalErr(m_strSource & myName & ": ERR#" & str(Err) & ", Desc:" & Err.Description)

End Function
[/tt] Craig, mailto:sander@cogeco.ca

Si hoc legere scis, nimis eruditionis habes
 
Craig - here is a class module which takes a snapshot of the current processes

'==========================================

Option Explicit

'==========================================

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
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

'------------------------------------------

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

'==========================================

Public Function GetProcesses() As Boolean

Dim lhSnapShot As Long
Dim pe32 As PROCESSENTRY32
Dim lRet As Long
Dim lStr_ProcID As String
Dim lInt_Row As Integer

lhSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)

If (lhSnapShot = 0) Then
GetProcesses = False
Else
pe32.dwSize = Len(pe32)
lRet = ProcessFirst(lhSnapShot, pe32)
Do While lRet
lInt_Row = LineCount.grdProcs.rows - 1
LineCount.grdProcs.TextMatrix(lInt_Row, 0) = StripIt(pe32.szExeFile)
LineCount.grdProcs.TextMatrix(lInt_Row, 1) = CStr(pe32.th32ProcessID)
LineCount.grdProcs.TextMatrix(lInt_Row, 2) = CStr(pe32.th32DefaultHeapID)
LineCount.grdProcs.TextMatrix(lInt_Row, 3) = CStr(pe32.th32ModuleID)
LineCount.grdProcs.TextMatrix(lInt_Row, 4) = CStr(pe32.cntThreads)
LineCount.grdProcs.TextMatrix(lInt_Row, 5) = CStr(pe32.th32ParentProcessID)
LineCount.grdProcs.TextMatrix(lInt_Row, 6) = CStr(pe32.pcPriClassBase)
LineCount.grdProcs.TextMatrix(lInt_Row, 7) = CStr(pe32.cntUsage)
lRet = ProcessNext(lhSnapShot, pe32)
LineCount.grdProcs.rows = LineCount.grdProcs.rows + 1
Loop
LineCount.grdProcs.rows = LineCount.grdProcs.rows - 1

CloseHandle lhSnapShot
GetProcesses = True
End If

End Function

'-----------------------------------------

Private Function StripIt(rStr_ProcName As String) As String

Dim lStr_StripString As String

lStr_StripString = Trim(rStr_ProcName)
While (Asc(Right(lStr_StripString, 1)) < 32)
lStr_StripString = Left(lStr_StripString, (Len(lStr_StripString) - 1))
Wend
StripIt = lStr_StripString

End Function

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Thanks for the Help Cajun

What is LineCount, though?

Craig, mailto:sander@cogeco.ca

Si hoc legere scis, nimis eruditionis habes
 
Sorry - LineCount is a form which contains a Grid for displaying the results. Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Works Great... Thanks Cajun...

Do you know if I can use that PID to do a SendMessage?
Actually no worries, I ahve the structure already set up for that, I'll test it out and let you know.

Craig, mailto:sander@cogeco.ca

Si hoc legere scis, nimis eruditionis habes
 
You're welcome Craig - looking forward to hear about the PID for a SendMessage Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
Does anyone know how to kill a process that is returned with the code reference in this thread?
 
Have you tried using the TerminateProcess API using the th32ProcessID as the arguement? Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I had to use the OpenProcess with the PROCESS_ALL_ACCESS parameter and the th32ProcessID. I then used the return with the TerminateProcess API. It works! Here is part of my code:

hProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, th32ProcessID)
If (hProcess > 0) Then
TerminateProcess hProcess, 0
End If

Thanks for your help.
 
Cajun wouuld you please explain the results? im not sure what all of them are. Also 2, 3, and 7 listed below always come back with a &quot;0&quot; value, for me, is that right?

0 - process name
1 - process ID
2 - Default Heap ID
3 - Module ID
4 - CntThreads?
5 - th32ParentProcessID ?
6 - ClassBase ?
7 - CntUsage ? percent usage

thanks
 
Here I come almost a year later... Cajun, if you are still around, YOU'RE THE MAN! Thanks, the code is exactly what I was looking for!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top