GetForegroundWindow returns 0 on occassion

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





        MyBase.WndProc(m)


    End Sub

Open in new window

TechMonsterAsked:
Who is Participating?
 
CodeCruiserConnect With a Mentor Commented:
Try this

Dim objWindow As Object = GetForegroundWindow
If IsNothing(objWindow) = False
   Dim curWindow As IntPtr = objWindow
   ...
End If
0
 
puppydogbuddyCommented:
0
 
puppydogbuddyCommented:
try:
      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
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
puppydogbuddyCommented:
oops..that should be:
    Declare Auto Function GetForegroundWindow Lib "user32" () As System.IntPtr
       Dim hWnd As IntPtr = GetForegroundWindow()

0
 
Bob LearnedCommented:
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?
0
 
nffvrxqgrcfqvvcCommented:
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.

0
 
TechMonsterAuthor Commented:
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.  

Thanks.
0
 
nffvrxqgrcfqvvcCommented:
LParam already gives you the window handle.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.