Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Problem with ShellExecute and Excel

michael_green
Beginner
956 Views

Hi All,

I use the following code to active Excel from within my application:

type (t_shellexecuteinfo) sei

...

sei%cbsize = sizeof(sei)

sei%fMask = SEE_MASK_NOCLOSEPROCESS

sei%hwnd = hwnd

sei%lpVerb = NULL

sei%lpfile = loc(fullname2)

sei%lpParameters = NULL

sei%lpDirectory = NULL

sei%nShow = SW_SHOWNORMAL

if(.not.ShellExecuteEx(sei))then

! ... Handle errors

return

else

iret = WaitForSingleObject(sei%hProcess,INFINITE)

iret = CloseHandle(sei%hProcess)

end if

It is essential for the code that follows that the WaitForSingleObject succeeds, and this is usually the case. But if the user already has an Excel instance open independently of this program, the wait does not occur and mayhem results.

Can anyone explain to me what's going on and perhaps a way to fix it?

With many thanks in advance,

Mike

0 Kudos
1 Reply
anthonyrichards
New Contributor III
956 Views

The attached code(it's CVF but should build easily using IVF) will builddialog that lets youShellExecuteEx instances of EXCEL at the press of a button andaddeach shelled process handleto a list box. You can then select a handle from the list and then useone command button to trigger a WAITFORSINGLEOBJECT on the process using your command, or use another button to terminate the processs using the TerminateProcess function (in the ShellExecuteEx section,you will need tochange the path to theEXCEL executable from the one I use)

I have found that, even with a couple of EXCEL instances running before starting the program and shelling 3 extra instances of EXCEL, the WAITFORSINGLEOBJECT command is correctly triggered when the appropriate EXCEL window is closed (it is helpful to enter the process handle into an EXCEL cell when it is created in order to keep track of which handle applies to which EXCEL instance - a message box is displayed that gives you the process handle).The TERMINATEPROCESS command works OK as well.

If you have some orphan EXCEL processes left over, as an exercise you can close them from the dialogby sending a WM_CLOSE message to the appropriate EXCEL window, using a button in the dialog, once you have teh EXCEL window's handle. The latter can be obtained using the Microsoft utility SPYXX.EXE to find the handle to any displayed window. The window handle is not the same as the process ID.

0 Kudos
Reply