@KitaYama and
@cheekybuddha, I get the impression that you completely skipped an important step:
How do you know that Microsoft.Photo.exe is actually the application the user has configured for viewing these types of files?
I would rather use
ShellExecuteEx to invoke the verb ("open") and then use process id of the created process to close the application later.
I really wonder why nobody pursued this approach, which, in theory, would be simple and reliable. - Maybe you were so much more informed than I and knew in advance about the following problem...
The
ShellExecuteEx-approach works perfectly reliable to run, identify, and close the picture viewer. - But only once.
The problem is, that Photos.exe doesn't operate like a "normal" application that will create just one process for opening a file. Instead, Photos.exe will create multiple processes. If you close the window displaying your picture, either with code or manually, this will only terminate one of the multiple Photos.exe processes.
If there is at least one Photos.exe running when you use
ShellExecuteEx to open an image file, the existing Photos.exe process will spin off
more than one other Photos.exe processes. One of those new processes will then show the image while the others may or may not terminate again automatically. The ProcessId retrieved via ShellExecuteEx will be the one belonging to the very first process create which most likely terminates itself immediately. It will
not be the process displaying the picture.
I tried to take and analyze a snapshot of the process list to figure out the heritage of the running processes and trace their lineage back to the MsAccess process. - This would probably even work, but only if MsAccess exclusively would create new processes of the picture viewer. As the user could also run the picture viewer manually, this approach cannot be relied upon.
As a side product my code can be used to detect the viewer application's .exe-name and that name can be used to terminate all processes of this application. - I don't think this is an acceptable solution!
Nonetheless, I publish my code as attachment for review and potential enhancement. The attached module is in the state of being abandoned work in progress and is
not meant to be used in production without further revision!