Search code examples
vb.net64-bitrefreshsendkeys

Refreshing IE window automatically does not work in VB.Net


I have tried to write a simple function to bring an Internet Explorer windows to the foreground and then press F5 to refresh it using the dll function:

    Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Integer) As Integer

and then later call this function using:

    SetForegroundWindow(processID.MainWindowHandle.ToInt32)
        SendKeys.Send("{F5}")

However, when I tried to debug the code, the setforeground does not work.

I am using a Windows 2008 (64 bit) so I thought I should use ToInt64 instead. However that did not seem to do the trick as well, when I called SetForegroundWindow, nothing seems to come up.

Is there any other recommendation? I am using VS 2008.

Many thnaks!


Solution

  • P/Invoke signatures I can find suggest the parameter ought to be an IntPtr, not a sized integer (which kind of makes sense - you shouldn't be needing to write separate code for 32 and 64 bit):

    <DllImport("user32.dll")> _
     Private Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
     End Function
    

    Also, focus issues are notoriously difficult to debug, since the debugger window tends to want to regain focus all of the time (e.g. if you single step, it may set the focus elsewhere, then immediately regain the focus to show the next line of code)