Hi Guys, I happen to need the similar function so that I can run a
windows application in command line and having the console not returned
until the program finishes. I have gone through you guys postings, and
found no true solution there. Then I have to develop my own solution
according to following logic and it proves to be working fine with me:
Logics for the solution:
1. Console application need a main thread to determine if the
application terminates or not.
2. However, non-console windows application does not have a main thread.
Its simply spawn a child process to Windows system through CWinApp
class. So when you start windows applicaton on command line, it simply
initialize the CWinApp derived class and then handle the instance to
Window system, and at the same time the console is returned.
3. To have a non-console application to run on command line without
return until it finishes, we just simply need to incept the CWinApp
thread and let it to attach to a console application instead of the
default window's system.
With these in mind, I developed a batch mode class that will call the
non-console window's application using Win32 API's CreateProcess() to
call the window's application, so that the window's application will
become a child process of my batch mode caller application, then using
inter-process management funciton: WaitForSingleObject() to force the
child process not to return until it finishes. The batch mode caller
itself has to be a console application of course.
Now you can start the window's application with this batch mode caller,
and the console will not return until the window's application
disappears. This resolves the issue and the window's application does
not require any modification to work with the batch mode caller. Of
course, if you have the source code of the window's application, and
want to make it truly batch mode console application, you could put all
the batch processing functions under InitInstance() and do not let the
Window application even start with the GUI. That way, you turn the
Window Application be able to run in dual modes: One is the batch mode
without gui, and another is the normal window application mode with GUI.
I developed this in .NET 2003 C++ compiler. But it shall applies to
early compilers too.
I have provided enough information in above for any knowledgable
programmer to develop such batch mode caller by him/herself. But if
anyone is really interested in my code, please contact me via email and
it can be purchased from me with a modest fee (That should be paid by
your company anyway). Good lucks!