Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Feb 22nd, 2006, 1:23 PM   #1
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
Search for and close open windows

I am looking to modify a program that I finished some time ago. It searches for and deletes place holder files for a terminal emulator. Although for it to work properly, all open terminal windows need to be closed. As of right now the end user is being prompted by a message box to make sure they close any open windows before continuing. I was wondering if there is a way to have the program look when it is first started to see if there are any open before allowing the rest of the program to run. I would like it to detect the windows, prompt the user about them, and close the windows if the user clicks Continue, or let them do it manually and close my program if they click cancel. If so, what functions/methods should I be researching to do this?

Any help is greatly appreciated
-BB98

**Edit: Guess I should include that this is for a Windows based system.
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Feb 22nd, 2006, 2:19 PM   #2
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
You might find these links useful:

Enumerating all processes:
http://msdn.microsoft.com/library/de..._processes.asp

TerminateProcess function:
http://msdn.microsoft.com/library/de...ateprocess.asp
Cache is offline   Reply With Quote
Old Feb 22nd, 2006, 4:26 PM   #3
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
guess I am jumping into the deep end on this one... When it is talking about the processID, would that be the same as its displayed name in the TaskManager Process List? I guess I am quite confused as to how to get the handle of the process.:o Also, for each emulator window that is open, it adds another entry to the Running Process List with identical names. So if there are 4 windows open, there are four identical listings in the process list. Does this complicate things?

Thanks for the help thus far.
-BB98
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Feb 22nd, 2006, 9:34 PM   #4
Cache
Hobbyist
 
Join Date: Sep 2005
Posts: 266
Rep Power: 4 Cache is on a distinguished road
What you call the "displayed name" is the image name, and can be the same for multiple instances of the same executable. The PID, however, is unique to each instance. You could get the PID of any process that has the image name you are looking for, and take it from there.

The example in the link I gave shows how to get a handle to a process.
Cache is offline   Reply With Quote
Old Feb 22nd, 2006, 10:00 PM   #5
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
There's a heck of a difference between enumerating windows and enumerating processes as well as between terminating processes and closing windows. MSDN has a wealth of info on the API calls needed. Sending a close message to the windows you want to close is much more graceful. Terminating the process is like the "End Process" button in the task manager you mentioned. Sending a close message is like clicking the X in the control box or pressing Alt+F4
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270
Dameon is offline   Reply With Quote
Old Feb 23rd, 2006, 10:55 AM   #6
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
This is the sample code off of MSDN for Enumerating all processes... Would someone mind explaining where in the code the ProcessID is being obtained, or does it show this?

 #include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include "psapi.h"
 
void PrintProcessNameAndID( DWORD processID )
{
	TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
 
	// 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.
 
	_tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID );
 
	CloseHandle( hProcess );
}
 
void main( )
{
	// Get the list of process identifiers.
 
	DWORD aProcesses[1024], cbNeeded, cProcesses;
	unsigned int i;
 
	if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
		return;
 
	// 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++ )
		PrintProcessNameAndID( aProcesses[i] );
}

Thanks for the info
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Feb 23rd, 2006, 1:34 PM   #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
Is this really not clear?
void PrintProcessNameAndID( DWORD processID )
and
PrintProcessNameAndID( aProcesses[i] );
What then do you think the processID is?
__________________
"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 Feb 24th, 2006, 9:49 AM   #8
badbasser98
Hobbyist Programmer
 
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4 badbasser98 is on a distinguished road
OK, I understand it now. Although I cannot get the code to compile, is there a piece of syntax missing? This is the error I am getting.
Quote:
Originally Posted by Dev-C++ 4.9.9.2
[LINKER ERROR] unidentified reference to 'EnumProcessModules@16'
[LINKER ERROR] unidentified reference to 'GetModuleBaseNameA@16'
[LINKER ERROR] unidentified reference to 'EnumProcesses@12'
Id returned 1 exit status
These are new expressions to me and I am flying blind trying to get them to work.

Is there a DLL or other file that must be included?
Thanks,
-BB98
__________________
Learning to use C++ and loving every minute of it.
badbasser98 is offline   Reply With Quote
Old Feb 24th, 2006, 10:09 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Check the documentation for libraries required, acquire them if you don't have them, and add them to the path the linker searches or to the command line or to the makefile (depending on how you are building the program).
__________________
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
Old Feb 24th, 2006, 10:35 AM   #10
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 have to link to Psapi.lib, I'm not sure what that is named in Dev-C++.
__________________
"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
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 12:46 PM.

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