Jump to content

does any1 know how to kill a process from a list in a text box?...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
15 replies to this topic

#1
MXTECH

MXTECH

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
does any1 know how to kill a process from a list in a text box? like:
if the textfile contains the name "word"
then is proc running =true then
proc.kill
else
endif

or somethign like that??
plz help


thanks peeps

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
What version of VB are you using? It seems like you want to create a program similar to TaskManager in Windows, right? Well, you need to do a few things:
1) Use a ListBox instead of a TextBox to enable the user to select which process they want to kill.
2) Declare the WinAPI functions you need. Basically what you need to do is A) enumerate all running processes using EnumProcesses, B) open each process using OpenProcess, C) get the executable name for each process using QueryFullProcessImageName, D) put all the names in a list and let the user choose which ones to kill, E) kill the process using TerminateThread.


REM    For Visual Basic .NET ONLY

Declare Function EnumProcesses Lib "kernel32" Alias "EnumProcessesA" _

(ByRef ProcessIDList As Int32, _

ByVal ListSize As Int32, _

ByRef BytesReturned As Int32) As Boolean


Declare Function OpenProcess Lib "kernel32" Alias "OpenProcessA" _

(ByVal DesiredAccessFlags As Int32, _

ByVal InheritHandle As Boolean, _

ByRef ProcessID As Int32) As IntPtr


Declare Function QueryFullProcessImageName Lib "kernel32" _

Alias "QueryFullProcessImageNameA" (ByVal ProcessHandle As IntPtr, _

ByVal ProcessFlags As Int32, _

ByRef EXEName As String, _

ByRef BufferLength As Int32) As Boolean


Declare Function TerminateThread Lib "kernel32" _

Alias "TerminateThreadA" _

(ByVal ProcessHandle As IntPtr, _

ByVal ExitCode As Int32) As Boolean


Declare Function CloseHandle Lib "kernel32" Alias "CloseHandleA" _

(ByVal Handle As IntPtr) As Boolean


REM   Define the following constants:

#const PROCESS_QUERY_LIMITED_INFORMATION = &H1000

#const PROCESS_TERMINATE = &H0001

I can give you the VB6 declarations if necessary. If you're putting these in a form module instead of a code module, you need to make the declarations begin with Private Declare Function. Here's an example of how to do what you want:


Dim ProcessIDList(512) As Int32 'holds the process IDs

Dim ProcessHandle As IntPtr 'holds handle to an open process

Dim ProcessIDString As String 'holds the string of the name of the process

Dim BytesRead As Int32 'not really needed in Visual Basic, but we have to have it


EnumProcesses(ProcessIDList,512,BytesRead)

'we have the process list now, so we just open one one at a time and get the string

Dim i As Integer

For i = 1 To 512

    ProcessIDString = ""

    ProcessHandle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,False,ProcessIDList(i))

    QueryFullProcessImageName(ProcessHandle,0,ProcessIDString,BytesRead)

    myListBox.AddItem(ProcessIDString)

    'since we can only have a few handles open at once, we'll close this

    CloseHandle(ProcessHandle)

Next i


'all processes in the listbox. get the index of the one the user wants to kill

'the following assumes that i contains the index


ProcessHandle = OpenProcess(PROCESS_TERMINATE,False,ProcessIDList(i))

If TerminateThread(ProcessHandle) = False Then

    MsgBox "Error killing process."

End If

'close handle, and we're done!

CloseHandle(ProcessHandle)


If you need more help, just let me know. Note: I might've messsed up the aliases on some of these functions; let me know if it doesn't work at runtime.

Edited by dargueta, 11 July 2008 - 09:21 PM.
Fixed bug


#3
MXTECH

MXTECH

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
    EnumProcesses(ProcessIDList,512,BytesRead)
    'we have the process list now, so we just open one one at a time and get the string
    Dim i As Integer
    For i = 1 To 512
    ProcessIDString = ""
    ProcessHandle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION,False,ProcessIDList(i))
    QueryFullProcessImageName(ProcessHandle,0,ProcessIDString,BytesRead)
    myListBox.AddItem(ProcessIDString)
    'since we can only have a few handles open at once, we'll close this
    CloseHandle(ProcessHandle)
Next i

    'all processes in the listbox. get the index of the one the user wants to kill
    'the following assumes that i contains the index

ProcessHandle = OpenProcess(PROCESS_TERMINATE,False,ProcessIDList(i))
If TerminateThread(ProcessHandle) = False Then
    MsgBox "Error killing process."
End If
    'close handle, and we're done!
CloseHandle(ProcessHandle)
This part is says it has declaration issues
i use Visual Studio 2008 and running Framework 3.5 so a bit of stuff is changed like
for example Listbox.additem() isn't the case anymore its listbox.items.add()
its wierd i kno haha

so do i have to make a seprate code module and just make declarations to it?

#4
MXTECH

MXTECH

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
O and what i'm trying to do is make an app for my school cos kiddies play games and i want it to run in the backgroud and kill the programs when the kids run them, and if a new game is released i want to be able to open the program interface and add the process name to the listbox with all the banned objects in it,
is that possible to do like i think i have to have a
timer that runs calls every like 500 millioseconds and then update process list and then
compare the 2 and if entry from processlist =true as well as bannedobject list=true then end process in processlist?
:confused:

#5
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
What error does it give you exactly? The solution depends on the error message.

I suggest you create a file that contains a list of banned games (one per line) and put it on the school server; that way, the program running on the computer can check the server file (open as read only to avoid access violations) and kill any unwanted processes. As for checking every 500 milliseconds, I know that there's a Timer object in Visual Basic, but I've never used it. I found a tutorial here. There's some stuff you'll want to skip over, as their example is somewhat specific, but it works for your purposes.

I strongly suggest that you don't have the program read from the banned list file every 500ms as it'll slow it down a lot. Instead, use another timer to have the program update its list of banned processes every hour or so.

Edited by dargueta, 12 July 2008 - 09:55 AM.
fixed typo


#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
Here are the correct declarations


REM    For Visual Basic .NET ONLY

Declare Function EnumProcesses Lib "kernel32" Alias "EnumProcesses" _

(ByRef ProcessIDList As Int32, _

ByVal ListSize As Int32, _

ByRef BytesReturned As Int32) As Boolean


Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" _

(ByVal DesiredAccessFlags As Int32, _

ByVal InheritHandle As Boolean, _

ByRef ProcessID As Int32) As IntPtr


Declare Function QueryFullProcessImageName Lib "kernel32" _

Alias "QueryFullProcessImageNameA" (ByVal ProcessHandle As IntPtr, _

ByVal ProcessFlags As Int32, _

ByRef EXEName As String, _

ByRef BufferLength As Int32) As Boolean


Declare Function TerminateThread Lib "kernel32" _

Alias "TerminateThread" _

(ByVal ProcessHandle As IntPtr, _

ByVal ExitCode As Int32) As Boolean


Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" _

(ByVal Handle As IntPtr) As Boolean



I messed up the aliases. Sorry about that.

#7
MXTECH

MXTECH

    Learning Programmer

  • Members
  • PipPipPip
  • 47 posts
aw yeah i have the same thing running for my STINGER app, it checks every 300seconds cos my boss was worried about excess network traffic. thanks dude i shall check the code out soon

cheers:D

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
If the QueryFullProcessImageName function gives you a file path, just strip off the executable name by going in reverse through the string until you hit a backslash.

Edited by dargueta, 12 July 2008 - 08:11 PM.
Fixed typo


#9
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
Again, great help! But now it won't let me give you more rep, I already gave you in another thread.

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
What exactly does rep do? I don't get the difference between the green and grey boxes either.

#11
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
what grey boxes?
rep is just reputation.. you can reach guru status etc... I think there is a thread about it in the announcements forum.

#12
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
I have green little rectangles and one or two grey ones in my rep thingy on the CP.