Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 27th, 2006, 3:34 PM   #1
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
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.
badbasser98 is offline   Reply With Quote
Old Mar 27th, 2006, 3:41 PM   #2
bgeraghty
Newbie
 
Join Date: Mar 2006
Posts: 15
Rep Power: 0 bgeraghty is on a distinguished road
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()
bgeraghty is offline   Reply With Quote
Old Mar 27th, 2006, 5:50 PM   #3
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 816
Rep Power: 4 The Dark is on a distinguished road
	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)
The Dark is offline   Reply With Quote
Old Mar 27th, 2006, 7:14 PM   #4
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 259
Rep Power: 3 Cache is on a distinguished road
A little searching never hurt anyone lol: http://www.programmingforums.org/for...ead.php?t=8525
Cache is offline   Reply With Quote
Old Mar 28th, 2006, 1:29 AM   #5
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
Quote:
Originally Posted by Cache
A little searching never hurt anyone lol: http://www.programmingforums.org/for...ead.php?t=8525
Yeah I saw him looking at that yesterday, he must not have copied it rightly.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for."
-- Socrates
nnxion is offline   Reply With Quote
Old Mar 29th, 2006, 7:54 AM   #6
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
Quote:
Originally Posted by Cache
A little searching never hurt anyone lol: http://www.programmingforums.org/for...ead.php?t=8525
I know of that thread, since it is one that I started ... I was asking for a more specific example on how to get it to work, or how mine could be corrected.

Quote:
Originally Posted by bgeraghty
Isn't the part that it writes to a txt file unnecessary?
Yes, the only reason it is in there is becuase this is a test program that I was trying to do serveral things with and get them working before including them in the main code of a program I am creating.

Quote:
Originally Posted by The Dark
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)
That makes sence, however is still does not work. Thanks for trying.
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Mar 29th, 2006, 8:44 AM   #7
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Mar 29th, 2006, 9:03 AM   #8
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
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.
badbasser98 is offline   Reply With Quote
Old Mar 29th, 2006, 11:34 AM   #9
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 259
Rep Power: 3 Cache is on a distinguished road
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:
hProcess [in] Handle to the process to terminate.
The handle must have the PROCESS_TERMINATE access right. For more information, see Process Security and Access Rights.
Cache is offline   Reply With Quote
Old Mar 29th, 2006, 12:21 PM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Don't admit you read it, we'll think you can't comprehend .
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:33 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC