![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
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. |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Mar 2006
Posts: 15
Rep Power: 0
![]() |
Isn't the part that it writes to a txt file unnecessary? You could just strcmp() the array of process names you get one by one inside the app and then call terminateprocess() to end it. Once you have the name/PID, grab the handle with OpenProcess() and pass it to TerminateProcess()
|
|
|
|
|
|
#3 |
|
Expert Programmer
Join Date: Jun 2005
Posts: 825
Rep Power: 4
![]() |
if (szProcessName == "dtelnet.exe" | szProcessName == "adwin.exe") You can't compare c strings like this. You need to use strcmp. Also you should use the logical or (||), rather than the bitwise or (|) if (strcmp(szProcessName, "dtelnet.exe") == 0 || strcmp(szProcessName, "adwin.exe") == 0) |
|
|
|
|
|
#4 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
A little searching never hurt anyone lol: http://www.programmingforums.org/for...ead.php?t=8525
|
|
|
|
|
|
#5 | |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Quote:
![]()
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
|
#6 | |||
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
Quote:
... I was asking for a more specific example on how to get it to work, or how mine could be corrected.Quote:
Quote:
__________________
Learning to use C++ and loving every minute of it. |
|||
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
You did not even read this. Which was already in the application Cache showed you. It works after you apply that.
I also highly recommend using the C++ library, and lastly opening and closing a file 3 times is not very effecient.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
First off, how can you tell me that I did not read anything. Are you here sitting next to me watching what I am or am not doing? As a matter of fact, I did read that. I may have not understood everything fully, but read it non the less. After trying serveral things to make it work, I could not so I figured I would ask here since the people here have been helpful in the past. Now I am not so sure how helpful this forum really is...
I also could care less about he efficiency of this program as it is just to test functionality of a few things. This is nothing that will be "copied and pasted" into any other code. Strictly a test program, as I have stated before, to get a grasp on this and the write to file method. And actually, it is opening and closing the file more along the lines of 45 times (once for each process). It still takes well under a second to run, but that is irrelevent.
__________________
Learning to use C++ and loving every minute of it. |
|
|
|
|
|
#9 | |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 4
![]() |
Go back to the example I showed you in your other post. Note the access right I open the process with. It's the bit in bold lol. Then, again, see the following link:
http://msdn.microsoft.com/library/de...ateprocess.asp It states quite clearly that: Quote:
|
|
|
|
|
|
|
#10 | |||
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
Quote:
Quote:
![]()
__________________
Learning to use C++ and loving every minute of it. |
|||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|