|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Enumerate Processes and Terminate SelectedI want to enumerate open processes in a listview and terminate the selected process. I found a code which can enumerate but termination doesnt work. any input is welcome, thanks! here is the code ' Enumerate open processes Private Sub EnumProcess(Optional ByVal sExeName As String = "") Dim lSnapShot As Long Dim lNextProcess As Long Dim tPE As PROCESSENTRY32 ' Create snapshot lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) If lSnapShot <> -1 Then ' Clear list lstProcess.Clear ' Length of the structure tPE.dwSize = Len(tPE) ' Find first process lNextProcess = Process32First(lSnapShot, tPE) Do While lNextProcess ' Found specified process If sExeName = Left$(tPE.szExeFile, Len(sExeName)) And Len(sExeName) > 0 Then Dim lProcess As Long Dim lExitCode As Long ' Open process lProcess = OpenProcess(0, False, tPE.th32ProcessID) ' Terminate process TerminateProcess lProcess, lExitCode ' Close handle CloseHandle lProcess Else ' Add exe to list lstProcess.AddItem tPE.szExeFile End If ' Get next process lNextProcess = Process32Next(lSnapShot, tPE) Loop ' Close handle CloseHandle (lSnapShot) Else lstProcess.AddItem "Cannot enumerate running process!" End If End Sub Look at Process Monitor examples on PSC (Planet Source Code).
http://www.planet-source-code.com/vb/default.asp?lngWId=1 They work. I have used some of that code in a working app. Show quoteHide quote "Abhishek" wrote: > Hello everyone, > > I want to enumerate open processes in a listview and terminate the selected > process. I found a code which can enumerate but termination doesnt work. > > any input is welcome, thanks! > > here is the code > ' Enumerate open processes > Private Sub EnumProcess(Optional ByVal sExeName As String = "") > Dim lSnapShot As Long > Dim lNextProcess As Long > Dim tPE As PROCESSENTRY32 > > ' Create snapshot > lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) > If lSnapShot <> -1 Then > ' Clear list > lstProcess.Clear > ' Length of the structure > tPE.dwSize = Len(tPE) > > ' Find first process > lNextProcess = Process32First(lSnapShot, tPE) > Do While lNextProcess > ' Found specified process > If sExeName = Left$(tPE.szExeFile, Len(sExeName)) And > Len(sExeName) > 0 Then > Dim lProcess As Long > Dim lExitCode As Long > ' Open process > lProcess = OpenProcess(0, False, tPE.th32ProcessID) > ' Terminate process > TerminateProcess lProcess, lExitCode > ' Close handle > CloseHandle lProcess > Else > ' Add exe to list > lstProcess.AddItem tPE.szExeFile > End If > ' Get next process > lNextProcess = Process32Next(lSnapShot, tPE) > Loop > > ' Close handle > CloseHandle (lSnapShot) > > Else > lstProcess.AddItem "Cannot enumerate running process!" > End If > End Sub > > > "Abhishek" <m*@server.com> wrote in message You didn't request any access when calling OpenProcess, so TerminateProcess news:u3sy6291JHA.4880@TK2MSFTNGP03.phx.gbl... > ' Open process > lProcess = OpenProcess(0, False, tPE.th32ProcessID) > ' Terminate process > TerminateProcess lProcess, lExitCode would have failed with error access denied. Use PROCESS_TERMINATE in the call to OpenProcess. Its working now,
here is the fix Const PROCESS_TERMINATE As Long = (&H1) 'Open process lProcess = OpenProcess(PROCESS_TERMINATE, False, tPE.th32ProcessID) Thanks a lot, god bless u Show quoteHide quote "Nobody" <nob***@nobody.com> wrote in message news:ugrUuK%231JHA.1716@TK2MSFTNGP03.phx.gbl... | "Abhishek" <m*@server.com> wrote in message | news:u3sy6291JHA.4880@TK2MSFTNGP03.phx.gbl... | > ' Open process | > lProcess = OpenProcess(0, False, tPE.th32ProcessID) | > ' Terminate process | > TerminateProcess lProcess, lExitCode | | You didn't request any access when calling OpenProcess, so TerminateProcess | would have failed with error access denied. Use PROCESS_TERMINATE in the | call to OpenProcess. | |
Control Focus
Help using SendMessage() function can the loading of a large number of records go as quick as in DOS Auto Spacing Labels in 2D Graph What is Sendkeys Chr(188)? 8 to 24 FMLabs FMTKit functions BigInteger and BigDecimal [equivalents] for VB.NET? Is using raw, uncooked Winsock so bad? NewEnum in Interface |
|||||||||||||||||||||||