Re: Bring window from elevated process to front in Vista

Tech-Archive recommends: Fix windows errors by optimizing your registry



See below...
On Mon, 3 Mar 2008 08:41:02 -0800, Steph <steph@xxxxxxxxxxxxxxxxx> wrote:

Thank you all for your help and advice.

It does indeed look as though I may have to run my application with admin
rights to do this properly, as I do not have control over all the
applications it will be managing.

That said, Toms suggestion of using SetForegroundWindow does actually work
for elevated processes, though if I try to use it for non-elevated it makes
them permanently topmost which is not desirable. I do realise that I may not
be able to rely on this behaviour persisting in future versions of Windows.

My code now goes along the lines of

if (::BringWindowToTop(a_hwndApp) == FALSE)
****
VERY POOR programming style. Comparing any BOOL-returning value or expression to a
boolean constant is a Really Bad Idea. The correct way to write this would be
if(!::BringWindowToTop(a_hwndApp))
I have no idea where the educational failure is that suggests that writing == FALSE or ==
TRUE for a boolean expression makes sense. You might as well write
if( (a > 0) == FALSE)
which is just as foolish as a way to write
if(a <= 0)
****
{
// if access denied it's probably an elevated process
if (GetLastError() == ERROR_ACCESS_DENIED)
{
blnSuccess = ::SetForegroundWindow(a_hwndApp);
}

}
else
{
// bring to front as before
}


Thanks once again,
Steph
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.