![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#2 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 3
![]() |
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 |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
Hobbyist
Join Date: Sep 2005
Posts: 261
Rep Power: 3
![]() |
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. |
|
|
|
|
|
#5 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2005
Location: United States
Posts: 124
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#7 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Is this really not clear?
void PrintProcessNameAndID( DWORD processID ) PrintProcessNameAndID( aProcesses[i] );
__________________
"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
![]() |
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:
Is there a DLL or other file that must be included? Thanks, -BB98
__________________
Learning to use C++ and loving every minute of it. |
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#10 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|