I am using CreateProcess to run Adobe Acrobat Reader to display a PDF file. I would like to close that application. I am trying use TerminatePocess to close the application. If only one instance of the application is running, TerminateProcess works just fine. But if more than one instance is running, it will not close. If a PDF file is opened outside my program, and my program runs Acrobat Reader to open a different file, TerminateProcess will not close the reader that I opened.

2 Answers
2

CreateProcess() can only give you a HANDLE to the process that it directly created. Sometimes, an app might launch another process of its own to do its work, or it might pass information (like the passed filename) to another running process and then exit itself. Those kind of behaviors would account for the symptoms you are seeing, if that is what Adobe is doing internally.

Alright, you launch an instance of Adobe Reader, and user reads the PDF content. But who are you to decide when the Reader is supposed to close? Let the end-user decide when the said application is to close. Are you waiting for few seconds/minutes before application should close - that's a bad design. Are you providing user an option in your application (like some Close Adobe Reader button), when you application should close - not a good option!

For just launching application, you should just use ShellExecute or ShellExecuteEx to open the desired file. The OS (i.e. Shell) will determine how to open a particular file. How do you know where Adobe Reader is installed - in Program Files, or in Program Files (x86). Which version of Reader is installed? And what about any other application, say a word-processor, or a media player? Who are you to open a file using very-specific program to open a file, and you to decide when to terminate such process.

More than that, your application may spawn other process(es) - would you close all child processes (down the process-tree)?