The Kill statement will delete a file on disk, not exit a running process. What you can do is to call TerminateProcess, and you do need the Process handle to do that. Do you know which process you want to end?
WM_CLOSE will close a window, a process might have several windows or in fact no window at all (in which case you don't have anything to send the WM_CLOSE message to ).
yeh i dunno what happened..it was formatted when i posted it
Sorry, but I find that a bit hard to believe unless you posted it without the [vbcode][/vbcode] tags first and edited it afterward to add them . But you can always edit the post again and paste the formatted code.
I have the process handle.
how do I use TerminateProcess? how do I call it?
Do you have the process handle or the process ID, there is a difference. The process handle can be retrieved from the process ID by calling the OpenProcess API function to which you specify what kind of access you need for the handle, in your case you need the PROCESS_TERMINATE + the STANDARD_RIGHTS_REQUIRED access flag. When you have this handle you may call the TerminateProcess function.
However you should know that calling the TerminateProcess function is an abnormal way of killing a process. It's equal to clicking the End Process button on the Processes tab in Task Manager. The reason it's abnormal is because TerminateProcess does kill the process and all of its threads but DLLs that are attached to the process will not be notified that the process has been terminated. You should only use this during extreme circumstances since the state of the global data maintained by DLLs may be comprimised.
However if it's the Process ID you have you can use the following code:
VB Code:
Private Declare Function OpenProcess Lib "kernel32.dll" ( _
ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32.dll" ( _
ByVal hProcess As Long, _
ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" ( _
ByVal hObject As Long) As Long
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000&
Private Const PROCESS_TERMINATE As Long = (&H1)
Public Function KillProcess(ByVal nProcessID As Long) As Boolean
Dim hProcess As Long
Const RIGHTS_FLAGS = STANDARD_RIGHTS_REQUIRED Or PROCESS_TERMINATE
here try this its a small thing u can use for more than one process it stops the process from running
in a moduel putthis
VB Code:
Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As Any, ReturnLength As Any) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Declare Function GetCurrentProcess Lib "kernel32" () As Long
Declare Function GetVersion Lib "kernel32" () As Long
Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function TerminateProcess Lib "kernel32" (ByVal ApphProcess As Long, ByVal uExitCode As Long) As Long
Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const TOKEN_ADJUST_PRIVILEGES = &H20
Public Const TOKEN_QUERY = &H8
Public Const SE_PRIVILEGE_ENABLED = &H2
Public Const PROCESS_ALL_ACCESS = &H1F0FFF
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
Type LUID
lowpart As Long
highpart As Long
End Type
Type TOKEN_PRIVILEGES
PrivilegeCount As Long
LuidUDT As LUID
Attributes As Long
End Type
Public Function KillApp(myName As String) As Boolean
I have the process handle and process ID,
so what it the simplest way to kill it?
Hmmm... Did you even bothered to read this? If you have the process handle with the correct rights you may call the TerminateProcess. If you only have the ID you need to use that to open the running process with the correct rights flag, and then terminate it. Please let me know what it is you didn't understand.