• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1481
  • Last Modified:

How do I use BringWindowToFront() in vb.net 2005?

Dear Experts!

I am using vb.net 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.

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

Thank you Experts! ~Michael
0
JeffreyDurham
Asked:
JeffreyDurham
  • 2
  • 2
1 Solution
 
surajgupthaCommented:
Try it using the ie javascript code?
http://www.codelifter.com/main/tips/tip_008.shtml
0
 
JeffreyDurhamAuthor Commented:
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 1.1.0.0), as for the report viewer, I'm using Microsoft.ReportViewer.Winforms 8.0.0.0

~Michael
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
JeffreyDurhamAuthor Commented:
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.
        InitializeComponent()
        ' 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
        bringIEToFront()
    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
        BringWindowToTop(h)
    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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now