?
Solved

GetForegroundWindow returns 0 on occassion

Posted on 2011-04-20
8
Medium Priority
?
1,392 Views
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





        MyBase.WndProc(m)


    End Sub

Open in new window

0
Comment
Question by:TechMonster
8 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1500 total points
ID: 35439208
Try this

Dim objWindow As Object = GetForegroundWindow
If IsNothing(objWindow) = False
   Dim curWindow As IntPtr = objWindow
   ...
End If
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 35439664
0
 
LVL 38

Expert Comment

by:puppydogbuddy
ID: 35439808
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 38

Expert Comment

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

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35446785
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
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 35446802
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
 

Author Closing Comment

by:TechMonster
ID: 35448109
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
 
LVL 29

Expert Comment

by:nffvrxqgrcfqvvc
ID: 35448501
LParam already gives you the window handle.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question