|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 
|
working example
Hi all,
Would someone be kind enough to put together a working example of a program using the TerminateProcess() function? I have tried for the life of me to follow the examples found on MSDN to create a program that includes this, but cannot. Perferably a function that searches the current running processes and closes one (predetermined by programer), there could also be mutiple instances of this process with the same name.
This is modified code taken from the MSDN example I was using to try and get this to work (yes i know it could be improved and more 'modern' code could have been used, but I am simply trying to get it to work, not make it pretty just yet). As of right now, it also creates a list in a text file showing the currently running processes and their ID number and opens it upon finishing the search. During the process query it is supposed to terminate any process with the name dtelnet.exe or adwin.exe.
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include "psapi.h"
const char* filename;
FILE* fio;
char ret;
void PrintProcessNameAndID( DWORD processID )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
TCHAR szName[MAX_PATH] = TEXT("dtelnet.exe");
// Get a handle to the process.
HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
// Get the process name.
if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
}
// Print the process name and identifier in the text file.
if ((fio = fopen(filename, "a+")) != NULL)
{
fprintf( fio, "%s\t(%u)\n", szProcessName, processID);
}
fclose(fio);
if (szProcessName == "dtelnet.exe" | szProcessName == "adwin.exe")
{
TerminateProcess(hProcess, 1);
}
CloseHandle( hProcess );
}
int main( )
{
int NoOfProcesses;
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 0;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
NoOfProcesses = cProcesses;
//clear all previous data to show only current processes and prevent errors.
filename = "C:\\processes.txt";
fio = fopen(filename, "w");
fclose(fio);
if ((fio = fopen(filename, "a+")) != NULL)
{
fprintf( fio, "Process Name\t(PID)\n\n");
}
fclose(fio);
// Print the name and process identifier for each process in the text file.
for ( i = 0; i < cProcesses; i++ )
PrintProcessNameAndID( aProcesses[i] );
if ((fio = fopen(filename, "a+")) != NULL)
{
fprintf( fio, "\nTotal Processes Scanned: %i", NoOfProcesses);
}
fclose(fio);
printf("do not close this window, it will close automatically");
printf(" when notepad is closed.");
system("c:\\processes.txt");
return 0;
}
Thanks for the help,
-BB98
__________________
Learning to use C++ and loving every minute of it.
|