Wolfgang Adamec
Wolfgang Adamec

Reputation: 8534

ShellExecuteEx on windows 7 with default image / photo viewer

We have a desktop application running on Windows XP, Windows Vista, Windows 7 and so on (it is written in cobol, but I think in this case it is not relevant).

In our source code we call ShellExecuteEx with open.
After this - in order to wait for the termination of the called programm - we call WaitForSingleObject with infinite.

It is working without problems, only on windows-7-machines with images and the default image viewer we have problems.

If ShellExecuteEx is called there for a jpg or tif file and the machine has only the default windows image / photo viewer, an error occurs. WaitForSingeObject returns WAIT_FAILED and GetLastError() says: INVALID_HANDLE.

This DOES NOT HAPPEN on windows 7, when there is another image viewer, for example, Picasa Photo Viewer, or Evince windows version or JPEGView.

I do not know where the problem is with the default image viewer on windows 7 and ShellExecuteEx. ShellExecuteEx DOES NOT return an error code!

Thanks alot in advance.

Update: Thank you for your answers.

@David Heffernan: As you said, in the case where it does NOT work, ShellExecuteEx has an return code (hInstApp) of 42 (ok!) and hProcess is NULL! (The only new process started was dllhost.exe.)

But, I tested the whole thing on another windows 7 machine. In this case i did the following:
- On this machine Picasa was the default viewer.
- I switched back to the default (Windows Photo Viewer).
- It worked!
- Then I did NOT close photo viewer and pressed "show" in our application AGAIN. - It also worked (a new instance of the viewer popped up), even when the Windows Photo Viewer was already running!

Upvotes: 4

Views: 1190

Answers (1)

David Heffernan
David Heffernan

Reputation: 613432

The default handler for the open verb on images in modern Windows versions may not invoke a new process. It may well just show the image in an already running shell process. And when that happens, the process handle that is returned is NULL. That is what is happening here, and that is why the call to WaitForSingleObject fails in the way you describe.

The documentation for SHELLEXECUTEINFO covers this:

Even if fMask is set to SEE_MASK_NOCLOSEPROCESS, hProcess will be NULL if no process was launched. For example, if a document to be launched is a URL and an instance of Internet Explorer is already running, it will display the document. No new process is launched, and hProcess will be NULL.

What this all means is that the design of that part of your program is based on a flawed assumption. Namely the assumption that calling ShellExecuteEx will always yield a process handle on which you can wait for termination. You will need to find some other way to solve your problem.

Upvotes: 5

Related Questions