Does anybody know how to kill a running process ??
Printable View
Does anybody know how to kill a running process ??
use the TerminateProcess API.
How would I implement that ???
Can you give me an example....
the hard part is getting the process handle.
Let's say you called CreateProcess to start a process.
you would then call OpenProcess with the following params:
hProcess = OpenProcess(SYNCHRONIZE OR PROCESS_TERMINATE, 0, pi.dwProcessId)
(get all the constant declarations from the APIViewer add-in)
(pi is the process_information structure that is filled by CreatProcess.)
Now, you are ready to kill the process by means of:
TerminateProcess( hProcess, 0)
Be sure to check all return codes of all api calls.
But what if the process was not started with CreateProcess ??
AFAIK the Shell function returns a Process ID (although the Documentation talks about a "task id"). You can get the Process handle from the Process id via OpenProcess API
The thing is. I would like to have vb-source of the following:
a list wich shows the path's of running processes (dll's exe's etc.)
The option to select and kill any of these processes..
The thing is I did find source of apps revealing processes and various source with wich you could kill processes (usually the process needs to have a window (titlebar), wich is not what we are looking for here), but never a combination of the two...
Could you explain it to me in an example..
Let's say f.i. c:\windows\notepad.exe is running..
How do I find it and then kill it....(winthout using the caption from the titlebar or anything)
using psapi, this function list all process, check name and if the boolean is true check the filename too (use the longname (if>8 char) without the .exe), then for your sample just call GetProcessIDByName(L"notepad", true);
to find it.
#include <Psapi.h>
DWORD GetProcessIDByName(LPWSTR szName, bool bCheckFileName) {
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 0;
long l = wcslen(szName);
WCHAR szProcessToFind[MAX_PATH];
wcscpy(szProcessToFind, szName);
if(wcsicmp(&szName[l-4], L".exe")) {
wcscat(szProcessToFind, L".exe");
}
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the name and process identifier for each process.
for ( i = 0; i < cProcesses; i++ ) {
WCHAR szProcessName[MAX_PATH] = L"unknown";
// Get a handle to the process.
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, aProcesses[i]);
// Get the process name.
if(hProcess) {
HMODULE hMod;
DWORD cbNeeded;
if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
GetModuleBaseNameW(hProcess, hMod, szProcessName, sizeof(szProcessName));
if(wcsicmp(szProcessToFind, szProcessName) == 0) {
CloseHandle( hProcess );
return aProcesses[i];
} else if(bCheckFileName) {
WCHAR *p, szFileName[MAX_PATH] = L"unknown", szLongFileName[MAX_PATH] = L"unknown";
GetModuleFileNameExW(hProcess, hMod, szFileName, sizeof(szFileName));
p = wcsrchr(szFileName, '\\');
if(wcsicmp(szProcessToFind, p+1) == 0) {
CloseHandle( hProcess );
return aProcesses[i];
}
if(wcschr(szFileName, '~')) {
if(GetLongPathNameW(szFileName, szLongFileName, sizeof(szLongFileName))) {
p = wcsrchr(szLongFileName, '\\');
if(wcsicmp(szProcessToFind, p+1) == 0) {
CloseHandle( hProcess );
return aProcesses[i];
}
}
}
}
}
}
CloseHandle(hProcess);
}
return 0;
}
[ ... i ... ] make my code italic...
when you read aProcesses, you should add the index i : aProcesses [ i ]
ccode is not enough ...