Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Task Manager in VB?
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HazySmoke)345




PostPosted: Sat Dec 31, 2005 9:59 pm   Post subject: Task Manager in VB?

Since a virus busted the task manager in my Windows 2000. I now have no defence against the annoying spywares that's ticking in my computer. So, one day, this weird idea came to me and I want to make a task manager myself. The API function TerminateProcess should do the job on ending the processes, but how do you find all the processes that's running? And, also, the syntax for Terminate Process looks like this:
VisualBASIC:
Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Where am I supposed to get the "hProcess" value?
Sponsor
Sponsor
Sponsor
sponsor
Brightguy




PostPosted: Wed Jan 04, 2006 10:33 pm   Post subject: Re: Task Manager in VB?

You can use the EnumProcesses function to get a list of all the process identifiers, and you can easily find example code on the net (although I've never used it).

Here's a small example on how to get a pHandle. It'll search for a window with the name "Calculator", and terminate it if found.

VisualBASIC:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Const PROCESS_ALL_ACCESS = &H1F0FFF

Private Sub Command1_Click()
    Dim hwnd As Long, pId As Long, pHandle As Long
   
    hwnd = FindWindow(vbNullString, "Calculator")
    GetWindowThreadProcessId CLng(hwnd), pId
    pHandle = OpenProcess(PROCESS_ALL_ACCESS, False, pId)
    TerminateProcess pHandle, 0
    CloseHandle pHandle
End Sub
HazySmoke)345




PostPosted: Thu Jan 05, 2006 12:46 pm   Post subject: (No subject)

Thanks so much. +11 bits. Very Happy
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: