Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Killing a console application

Posted on 2004-10-29
Medium Priority
Last Modified: 2013-12-03
I have a console application that closes by capturing CTRL_CLOSE_EVENT message.
If I startup this application manually, and then close the DOS console window via the CLOSE [X] icon, the applications closes successfully.
I can verify that it closes successfully, because I can see the debug log file showing that it captured the CTRL_CLOSE_EVENT message.

Now here is where my problem is at.  I have a test application that is suppose to launch the above console application, and then after peformming the proper test, it suppose to signal to the console application to close.  However, I can't seem to get the console application to close programmatically.
Here's the code that is in the test application:

            STARTUPINFO si;
            char TargetExec[] = "..\\debug\\FileBridgeApp.exe";
            ZeroMemory( &si, sizeof(si) );
            si.cb = sizeof(si);
            ZeroMemory( &pi, sizeof(pi) );
            if (CreateProcess( NULL,      // No module name (use command line).
                        TargetExec,                  // Command line.
                        NULL,                        // Process handle not inheritable.
                        NULL,                        // Thread handle not inheritable.
                        FALSE,                        // Set handle inheritance to FALSE.
                        0,                              // No creation flags.
                        NULL,                        // Use parent's environment block.
                        NULL,                        // Use parent's starting directory.
                        &si,                        // Pointer to STARTUPINFO structure.
                        &pi )                        // Pointer to PROCESS_INFORMATION structure.
                  CreateFilesInMonitoringDirectorys(teststructure, QtyTestApp);
                  // Close process and thread handles.
                  CloseHandle( pi.hProcess ); // ******this does not close the application
                  CloseHandle( pi.hThread ); // ******this does not close the application

How can  correctly signal to the application to close.
Question by:Axter
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 48

Expert Comment

ID: 12444319
LVL 30

Author Comment

ID: 12444375
That link is about capturing CTRL_CLOSE_EVENT message.

As I stated in my question, I'm not having a problem with capturing the message.

My problem is with the test application sending the message.
LVL 86

Accepted Solution

jkr earned 2000 total points
ID: 12444394
>>CloseHandle( pi.hProcess ); // ******this does not close the application

Well, it only closes the process handle. See http://support.microsoft.com/kb/q178893/ ("How To Terminate an Application "Cleanly" in Win32").
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

LVL 30

Author Comment

ID: 12444483
Thanks jkr.

I replace CloseHandle lines with the following:
EnumWindows((WNDENUMPROC)TerminateAppEnum, (LPARAM) pi.dwProcessId) ;

And copied and pasted the TerminateAppEnum function to my test project, and that seem to have done the trick.
LVL 86

Expert Comment

ID: 12450268
Um, sorry to be nitpicking, but you still should call 'CloseHandle()' even after terminating the console process. The "costs" in user space (4 bytes for the handle) is not important, but...
LVL 30

Author Comment

ID: 12450299
>>you still should call 'CloseHandle()'

What is gain by that?
When the console application shutsdown, wouldn't it close the process and the threads?
LVL 86

Expert Comment

ID: 12450313
For the app you're terminating, that's it. But, the calling app is still responsible to free the control structures (IOW: What's 'behind' the handle). It's not that much of memory, but that might add up e.g. in a server app in the long run.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question