|
-
October 10th, 1999, 10:10 AM
#1
Killing a process
Does anybody know how to kill a running process ??
-
October 11th, 1999, 01:40 AM
#2
Re: Killing a process
use the TerminateProcess API.
-
October 11th, 1999, 09:59 AM
#3
Re: Killing a process
How would I implement that ???
Can you give me an example....
-
October 11th, 1999, 10:03 AM
#4
Re: Killing a process
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.
-
October 11th, 1999, 01:18 PM
#5
Re: Killing a process
But what if the process was not started with CreateProcess ??
-
October 12th, 1999, 01:41 AM
#6
Re: Killing a process
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
-
October 12th, 1999, 05:23 AM
#7
Re: Killing a process
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...
-
October 12th, 1999, 03:23 PM
#8
Re: Killing a process
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)
-
February 19th, 2001, 10:34 AM
#9
Re: Killing a process
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;
}
-
February 19th, 2001, 10:39 AM
#10
Re: Killing a process
[ ... i ... ] make my code italic...
when you read aProcesses, you should add the index i : aProcesses [ i ]
ccode is not enough ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|