How do I use BringWindowToFront() in 2005?

Posted on 2007-10-18
Last Modified: 2008-01-09
Dear Experts!

I am using 2005. I am automating an instance of shdocvw.internetexplorer, and my boss wants me to ensure that the window (once opened) always goes to the front. So I was doing some research and I found some code and tried to implement it into my code. I got the following error:

BringWindowToTop(ie.HWND)   '<--- THIS IS THE LINE IT CRASHES ON
 (err) -> A call to PInvoke function 'IonData Maricopa Credit Watch!IonData_Maricopa_Credit_Watch.myWeb::BringWindowToTop' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long
Public myExplorer As New SHDocVw.InternetExplorer

Thank you Experts! ~Michael
Question by:JeffreyDurham
    LVL 21

    Expert Comment

    Try it using the ie javascript code?

    Author Comment

    Oh I'm writing this in winforms.. it's a windows application, not a web application. I'm automating the internetexplorer using the shdocvw.internetexplorer library (Microsoft Internet Controls, Com library, version, as for the report viewer, I'm using Microsoft.ReportViewer.Winforms

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Your API declaration is for VB6...

    For VB.Net, you need to change all "Long" to "Integer" (or "IntPtr" for window handles).

    So use this instead:

        Private Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As IntPtr) As Integer

    Author Comment

    Thank you Idle_Mind! I always appreciate your help. I was able to get that to work, using the change you suggested, altho I noticed if my program was not in the foreground and the ie window was in the background, the bringWindowToTop wouldn't work. So I was pondering trying to use FindWindowA in conjunction with it.

    Declare Function BringWindowToTop Lib "user32" (ByVal hWnd As IntPtr) As Integer
        Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

        Public myExplorer As New SHDocVw.InternetExplorer

        Public Sub New()
            ' This call is required by the Windows Form Designer.
            ' Add any initialization after the InitializeComponent() call.
            myExplorer.Visible = True
        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        End Sub

        Public Function bringIEToFront(ByRef ie As SHDocVw.InternetExplorer) As Integer
            bringIEToFront = BringWindowToTop(ie.HWND)   'This one works! :)
        End Function

        Private Sub BringIEToFront()
            ' Dim h As Long
            Dim h As Long = FindWindow("IExplore.exe", Nothing)
            h = FindWindow("IExplore.exe", Nothing) ' this line is no good
        End Sub

    I am absolutaly certain the problem with the find window is it's not returning an IntPtr, but rather a long, which is why the second way I have of doing it doesn't seem to be working. Do you have any other ideas on the subject?

    I'm going to take your first answer tho, because it does work, I was just hoping you might clarify that for me?

    Thanks, Michael
    LVL 85

    Accepted Solution

    That API declaration is for VB6 as well...

    Should be:

        Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr

    BUT...instead of BringWindowToTop(), you could try SetForegroundWindow():

        Private Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal handle As IntPtr) As Integer


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
    This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    734 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now