We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


GetForegroundWindow returns 0 on occassion

TechMonster asked
Medium Priority
Last Modified: 2012-06-21
What is the fix for the GetForegroundWindow returning 0 or null on occassion.  I see a MSDN known problem..http://msdn.microsoft.com/en-us/library/ms633505(v=vs.85).aspx
 USING VB. 2005  / .NET 2.0
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

        If m.Msg = uMsgNotify Then

        End If

        Select Case m.WParam.ToInt32
            Case ShellEvents.HSHELL_WINDOWACTIVATED
                Dim curWindow As IntPtr = GetForegroundWindow <<<<--PROBLEM IS HERE...ALTERNATIVE???>>>
                If (Not curWindow.Equals(IntPtr.Zero)) AndAlso (Not curWindow.Equals(Me.Handle)) Then
                    lastWindow = curWindow
                End If
        End Select


    End Sub

Open in new window

Watch Question

Most Valuable Expert 2012
Top Expert 2014
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
      Declare Auto Function GetForeGroundWindow Lib "user32" () As System.IntPtr
       Dim hWnd As IntPtr = GetForegroundWindow()

From this link:  http://www.pcreview.co.uk/forums/getforegroundwindow-t1407559.html
oops..that should be:
    Declare Auto Function GetForegroundWindow Lib "user32" () As System.IntPtr
       Dim hWnd As IntPtr = GetForegroundWindow()

Most Valuable Expert 2012
Top Expert 2008

From that MSDN reference, I see, "The foreground window can be NULL in certain circumstances, such as when a window is losing activation."  It would have been nice if there was an enumeration of what those "certain circumstances" were, like having another active desktop.  

I would like to know, from the 1000 meter view (instead of the 10 meter view), is what you are working with here?
Top Expert 2010

Usually the handle is already provided for you in the callback with this specific notification. The only problem is that there are two different ways one using WH_SHELL hook the nCode is the message and wParam is the handle to the activated window. In your  example your wParam is the nCode so I think you are using the RegisterShellHookWindow() and not the hook in that case the lParam member of the callback is the window handle.


It'll be awhile before I can test this in a production envirnoment.  I tested it in a dev and it seemed to do ok.  

Top Expert 2010

LParam already gives you the window handle.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.