Discussion:
ShellExecuteEx SetFocus problem
(too old to reply)
Sinna
2007-01-09 14:15:17 UTC
Permalink
Hi all,

I'm facing a very strange problem on 1 PC in my office:
When I launch a file with ShellExecuteEx it just displays fine. When I
close the application invoked to display the file, and relaunch the
file, the application is started normally (not minimized) but doesn't
get the focus.
The application is launched with vbNormalFocus (SW_SHOWNORMAL as you wish).
Does anyone has a clue?

I already tried to activate the launched app by calling the AppActivate
function but it fails with Error 5. Even introducing an additional
WaitForInputIdle-call (while already set as flag in the
SHELLEXECUTEINFO) doesn't work out.
Using APIs that cope with window handle(s) is not really an option as it
is quite difficult to determine the correct one. This is because
multiple files can be displayed using the same application.

TIA,

Sinna
J French
2007-01-09 15:33:52 UTC
Permalink
On Tue, 09 Jan 2007 15:15:17 +0100, Sinna
Post by Sinna
Hi all,
When I launch a file with ShellExecuteEx it just displays fine. When I
close the application invoked to display the file, and relaunch the
file, the application is started normally (not minimized) but doesn't
get the focus.
The application is launched with vbNormalFocus (SW_SHOWNORMAL as you wish).
Does anyone has a clue?
I already tried to activate the launched app by calling the AppActivate
function but it fails with Error 5. Even introducing an additional
WaitForInputIdle-call (while already set as flag in the
SHELLEXECUTEINFO) doesn't work out.
Using APIs that cope with window handle(s) is not really an option as it
is quite difficult to determine the correct one. This is because
multiple files can be displayed using the same application.
More info needed.
Sinna
2007-01-09 15:49:52 UTC
Permalink
Post by J French
On Tue, 09 Jan 2007 15:15:17 +0100, Sinna
Post by Sinna
Hi all,
When I launch a file with ShellExecuteEx it just displays fine. When I
close the application invoked to display the file, and relaunch the
file, the application is started normally (not minimized) but doesn't
get the focus.
The application is launched with vbNormalFocus (SW_SHOWNORMAL as you wish).
Does anyone has a clue?
I already tried to activate the launched app by calling the AppActivate
function but it fails with Error 5. Even introducing an additional
WaitForInputIdle-call (while already set as flag in the
SHELLEXECUTEINFO) doesn't work out.
Using APIs that cope with window handle(s) is not really an option as it
is quite difficult to determine the correct one. This is because
multiple files can be displayed using the same application.
More info needed.
I suppose you need some sources:

Private Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hWnd As Long
lpVerb As Long
lpFile As Long
lpParameters As Long
lpDirectory As Long
nShow As Long
hInstApp As Long
' fields
lpIDList As Long
lpClass As Long
hKeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type


Private Declare Function ShellExecuteEx Lib "shell32.dll" Alias
"ShellExecuteExW" ( _
ByRef lpExecInfo As SHELLEXECUTEINFO) As Long

(...)

Private mvarShellExecuteInfo As SHELLEXECUTEINFO

(...)

With mvarShellExecuteInfo
.cbSize = Len(mvarShellExecuteInfo)
.fMask = SEE_MASK_NOCLOSEPROCESS Or SEE_MASK_FLAG_DDEWAIT Or _
SEE_MASK_DOENVSUBST Or SEE_MASK_FLAG_NO_UI Or _
SEE_MASK_UNICODE Or SEE_MASK_WAITFORINPUTIDLE
.lpVerb = StrPtr(Action2Verb(Action))
.lpFile = StrPtr(sShortPathName)
.lpParameters = 0&
.lpDirectory = StrPtr(modAppPath.Path())
.nShow = VBAppWinStyle2ShowWindow(WindowStyle)

(...)
End With

Some explanation:
* Action2Verb() translates an enumeration into the strings open/edit/...
* sShortPathName contains the SFN of the file to be displayed.
* modAppPath.Path() returns the current application folder (in most
cases this equals App.Path)
* VBAppWinStyle2ShowWindow() maps the VbAppWinStyle constants on the
SW_xxx constants

Note that I use the ShellExecuteExW-variant as I have to support Unicode
Filenames.


If this is not what you need, please specify.

Sinna
Sinna
2007-01-18 07:20:54 UTC
Permalink
Post by Sinna
Hi all,
When I launch a file with ShellExecuteEx it just displays fine. When I
close the application invoked to display the file, and relaunch the
file, the application is started normally (not minimized) but doesn't
get the focus.
The application is launched with vbNormalFocus (SW_SHOWNORMAL as you wish).
Does anyone has a clue?
I already tried to activate the launched app by calling the AppActivate
function but it fails with Error 5. Even introducing an additional
WaitForInputIdle-call (while already set as flag in the
SHELLEXECUTEINFO) doesn't work out.
Using APIs that cope with window handle(s) is not really an option as it
is quite difficult to determine the correct one. This is because
multiple files can be displayed using the same application.
TIA,
Sinna
As there's very little input here, I'll try to explain a bit further.
Surfing some newsgroups/forums most of the topics indicate the hWnd
parameter of the SHELLEXECUTEINFO structure has to be initialized with
the handle of the calling window. Only in that way ShellExecute(Ex)
'knows' where to put the launched application in the Z-Order.

Strange enough I pass the correct window handle, but the application
doesn't pop up, it pops under instead. (Note that the application is not
launched minimized. When I minimize the calling app, the called app is
shown normally.)

Any ideas? Any suggestions?
Thanks in advance!

Sinna
MikeD
2007-01-22 13:55:33 UTC
Permalink
Post by Sinna
Post by Sinna
Hi all,
When I launch a file with ShellExecuteEx it just displays fine. When I
close the application invoked to display the file, and relaunch the file,
the application is started normally (not minimized) but doesn't get the
focus.
The application is launched with vbNormalFocus (SW_SHOWNORMAL as you wish).
Does anyone has a clue?
I already tried to activate the launched app by calling the AppActivate
function but it fails with Error 5. Even introducing an additional
WaitForInputIdle-call (while already set as flag in the SHELLEXECUTEINFO)
doesn't work out.
Using APIs that cope with window handle(s) is not really an option as it
is quite difficult to determine the correct one. This is because multiple
files can be displayed using the same application.
TIA,
Sinna
As there's very little input here, I'll try to explain a bit further.
Surfing some newsgroups/forums most of the topics indicate the hWnd
parameter of the SHELLEXECUTEINFO structure has to be initialized with the
handle of the calling window. Only in that way ShellExecute(Ex) 'knows'
where to put the launched application in the Z-Order.
Don't know where you read that, but that's not what the "official"
documentation says. MSDN Library states:

hwnd
Window handle to any message boxes that the system might produce while
executing this function.


There's no mention whatsoever of the passed in hwnd having anything to do
with the z-order.
Post by Sinna
Strange enough I pass the correct window handle, but the application
doesn't pop up, it pops under instead. (Note that the application is not
launched minimized. When I minimize the calling app, the called app is
shown normally.)
Any ideas? Any suggestions?
Thanks in advance!
Dunno if this will fix things or even make any difference at all, but try
passing the desktop's hwnd instead of your form's hwnd (assuming that's what
you're passing since you weren't clear and didn't post code). Use the
GetDesktopWindow Win32API function.

Public Declare Function GetDesktopWindow Lib "user32" Alias
"GetDesktopWindow" () As Long

If that makes no difference, can you post code which reproduces the problem?
Posting your code, even if we can't reproduce the problem, may allow us to
give you a solution.
--
Mike
Microsoft MVP Visual Basic
Sinna
2007-02-07 07:23:41 UTC
Permalink
Post by MikeD
Post by Sinna
Post by Sinna
Hi all,
When I launch a file with ShellExecuteEx it just displays fine. When I
close the application invoked to display the file, and relaunch the file,
the application is started normally (not minimized) but doesn't get the
focus.
The application is launched with vbNormalFocus (SW_SHOWNORMAL as you wish).
Does anyone has a clue?
I already tried to activate the launched app by calling the AppActivate
function but it fails with Error 5. Even introducing an additional
WaitForInputIdle-call (while already set as flag in the SHELLEXECUTEINFO)
doesn't work out.
Using APIs that cope with window handle(s) is not really an option as it
is quite difficult to determine the correct one. This is because multiple
files can be displayed using the same application.
TIA,
Sinna
As there's very little input here, I'll try to explain a bit further.
Surfing some newsgroups/forums most of the topics indicate the hWnd
parameter of the SHELLEXECUTEINFO structure has to be initialized with the
handle of the calling window. Only in that way ShellExecute(Ex) 'knows'
where to put the launched application in the Z-Order.
Don't know where you read that, but that's not what the "official"
hwnd
Window handle to any message boxes that the system might produce while
executing this function.
There's no mention whatsoever of the passed in hwnd having anything to do
with the z-order.
Post by Sinna
Strange enough I pass the correct window handle, but the application
doesn't pop up, it pops under instead. (Note that the application is not
launched minimized. When I minimize the calling app, the called app is
shown normally.)
Any ideas? Any suggestions?
Thanks in advance!
Dunno if this will fix things or even make any difference at all, but try
passing the desktop's hwnd instead of your form's hwnd (assuming that's what
you're passing since you weren't clear and didn't post code). Use the
GetDesktopWindow Win32API function.
Public Declare Function GetDesktopWindow Lib "user32" Alias
"GetDesktopWindow" () As Long
If that makes no difference, can you post code which reproduces the problem?
Posting your code, even if we can't reproduce the problem, may allow us to
give you a solution.
Sorry for the late response, but it took some time to get the PC back
where the problem occurs.
Switching the hWnd from ForeGroundWindow to DesktopWindow doesn't solve it.

I posted some code in response to J French and that's in fact the main
thing (only some additional declarations that are application specific).

The strangest thing of all is that it only occurs on 1 PC at the moment.
Nor my development PC nor my build PC expose this behavior.

If you have any suggestion, please let me know.
Otherwise I'll have to go back to my implementation where I *tried* to
mimic this functionality by digging into the registry. In 95% it worked
out quite well, but in the latter 5% it had some caveats. The main issue
there was how to determine if the user has changed the default
application to launch a file given its extension.


Sinna

Loading...