View previous topic :: View next topic |
Author |
Message |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Apr 25, 2008 2:15 pm Post subject: process killer question |
|
|
I have a question about making a process killer. Basically, my idea was to use CreateToolHelp32Snapshot() to take a snapshot of all the processes. Then list them out into like a combo box using Process32First() and Process32Next() which both need the PROCESSENTRY32 structure. After that I was going to do something like OpenProcess(). But then after that I was going to do TerminateProcess(). But every time I try to figure out how to use TerminateProcess() I get pulled into all these other APIs required for it and I just can't make any sense of it. I was wondering if anyone could give me a snippet of using OpenProcess() and TerminateProcess() to kill a process.
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Fri Apr 25, 2008 2:25 pm Post subject: |
|
|
Just a quick snippet for a function that gets the handle on an inputted exe file for the parameter and terminates it.
Code: | #include <windows.h>
#include <tchar.h>
#include <tlhelp32.h>
BOOL FindProcessAndTerminate( TCHAR *tszExe )
{
HANDLE hProcess;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL );
Process32First( hSnapshot, &pe32 );
do
{
if ( _tcscmp( pe32.szExeFile, tszExe ) == 0 )
{
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
// Optional (for safety of exit):
// DWORD dwExitCode;
// GetExitCodeProcess( hProcess, &dwExitCode );
// TerminateProcess( hProcess, dwExitCode );
// Or just straight terminate with 0 return value:
TerminateProcess( hProcess, 0 );
CloseHandle( hProcess );
CloseHandle( hSnapshot );
// All went well, lets return true.
return TRUE;
}
}
while ( Process32Next( hSnapshot, &pe32 ) );
// we got through without it terminating anything... so it didnt find the process... so close the handle and return false
CloseHandle( hSnapshot );
return FALSE;
} |
_________________
|
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Apr 25, 2008 2:38 pm Post subject: |
|
|
lurc wrote: | Just a quick snippet for a function that gets the handle on an inputted exe file for the parameter and terminates it.
Code: | #include <windows.h>
#include <tchar.h>
#include <tlhelp32.h>
BOOL FindProcessAndTerminate( TCHAR *tszExe )
{
HANDLE hProcess;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL );
Process32First( hSnapshot, &pe32 );
do
{
if ( _tcscmp( pe32.szExeFile, tszExe ) == 0 )
{
hProcess = OpenProcess( PROCESS_ALL_ACCESS, FALSE, pe32.th32ProcessID );
// Optional (for safety of exit):
// DWORD dwExitCode;
// GetExitCodeProcess( hProcess, &dwExitCode );
// TerminateProcess( hProcess, dwExitCode );
// Or just straight terminate with 0 return value:
TerminateProcess( hProcess, 0 );
CloseHandle( hProcess );
CloseHandle( hSnapshot );
// All went well, lets return true.
return TRUE;
}
}
while ( Process32Next( hSnapshot, &pe32 ) );
// we got through without it terminating anything... so it didnt find the process... so close the handle and return false
CloseHandle( hSnapshot );
return FALSE;
} |
|
Ok. I get it. I just have a quick question. What does the _tcscmp macro (or so I assume) do? Btw, what is a TCHAR persay. I imagine it is a typedef of something as in Win32 APIs LPSTR is the same as char*.
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Fri Apr 25, 2008 2:45 pm Post subject: |
|
|
_tcscmp is a macro for string compare.
_tcscmp compares 2 TCHAR * pointers or string's (LPTSTR)
TCHAR is a definition for WCHAR (if using unicode, just CHAR if using ASCII) TCHAR* is a pointer to an array of characters.
_________________
|
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Apr 25, 2008 2:47 pm Post subject: |
|
|
lurc wrote: | _tcscmp is a macro for string compare.
_tcscmp compares 2 TCHAR * pointers or string's (LPTSTR)
TCHAR is a definition for WCHAR (if using unicode, just CHAR if using ASCII) TCHAR* is a pointer to an array of characters. |
Ok, I get it. So basically your comparing whether or not the user selected process, and the process that is currently being "selected" by my program are the same. So if the user decides to end firefox.exe and my program gets to firefox.exe it executes the code, if not it keeps on going.
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Fri Apr 25, 2008 2:52 pm Post subject: |
|
|
Yea, exactly, say you want to terminate Firefox.exe then you would simply have your program call that function like so:
Code: | FindProcessAndTerminate( _T("firefox.exe") ); |
if Firefox is running, it should terminate.
you could even do a check since i wrote the function as a bool and it returns TRUE or FALSE;
like this:
Code: | if ( FindProcessAndTerminate( _T("firefox.exe") ) )
MessageBox( NULL, _T("Firefox terminated"), _T("Terminated"), MB_OK );
else
MessageBox( NULL, _T("Firefox.exe coudn\'t be found"), _T("Failed"), MB_OK ); |
btw _T is a macro for UNICODE or ASCII depending on the charecter set your using.
_________________
|
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Apr 25, 2008 2:57 pm Post subject: |
|
|
Ok I get it. I'll go to msdn and try to find what _T does, just because I like to at least have a understanding of what I'm doing instead of putting something in there and knowing it will work. But what I think I'm going to find annoying is that I'm going to have to do, is that I'm going to have to list em all out. Btw, if I understand correctly, in your do while loop, your condition is set so that it keeps going as long as their is another process. So when I'm listing them out, I would want to do the same thing (obviously not the exact same coding) but use the same condition, so it knows when to stop.
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Fri Apr 25, 2008 3:06 pm Post subject: |
|
|
Yep, it loops until it gets to the last process.
If you're using a ListView class then you can send the message LB_ADDSTRING in the loop and the LPARAM would be (LPARAM)pe32.szExeFile And if your gonna be wanting to do something that allows you to select the item, then terminate, you can use the LB_GETCURSEL Message to get the selected index then send the message LB_GETTEXT where the wParam is the val LB_GETCURSEL returned and the lParam is a pointer to a LPTSTR that will get the text.
Finally you can call the function i gave u to terminate the exe file chosen.
btw if you just right click it and hit "See declaration" it will open tchar.h and show you what _T does. (at least with MSVC)
Code: | #define _T(x) __T(x) |
Code: |
#ifdef _UNICODE
...
#define __T(x) L ## x
|
L is the symbol that the compiler uses for UNICODE. ( ## just means combine whatever is before the #'s with the thing after, so if you inputted _T("hi"), it would change it to L"hi" )
_________________
|
|
Back to top |
|
 |
atom0s Moderator
Reputation: 205
Joined: 25 Jan 2006 Posts: 8587 Location: 127.0.0.1
|
Posted: Fri Apr 25, 2008 4:44 pm Post subject: |
|
|
Just a suggestion, when you use OpenProcess, don't pass PROCESS_ALL_ACCESS as it is the commonly used and blocked when calling the API. Instead, to kill the application, you only need to have PROCESS_TERMINATE access.
_________________
- Retired. |
|
Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Fri Apr 25, 2008 4:56 pm Post subject: |
|
|
Ok, thanks Wiccaan.
_________________
8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
Back to top |
|
 |
|