Killing a console application

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.
LVL 30
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AxterAuthor Commented:
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.
>>CloseHandle( pi.hProcess ); // ******this does not close the application

Well, it only closes the process handle. See ("How To Terminate an Application "Cleanly" in Win32").

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

AxterAuthor Commented:
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.
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...
AxterAuthor Commented:
>>you still should call 'CloseHandle()'

What is gain by that?
When the console application shutsdown, wouldn't it close the process and the threads?
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.