I'm working on a legacy software that, among other, have a button that opens a new internet explorer windows and navigate to a specific address within it. The browser controller would hold the reference to the window so that if the button is pressed again - the same window will be used to be navigated into it again.
This basically works with SHDocVw.InternetExplorer object and I just use the "navigte" method and it works perfectly... on Windows XP (IE 6-8).
Our client wishes to upgrade to windows 7 and it seems like MS decided to screw this specific piece of code (and add security in the process :) ) by raising a "OnQuit" event every time you navigate to a different domain from what you were in (so, if I open a new window using "Browser = new SHDocVw.InternetExplorer();" and than use "Browser.Navigate("www.google.com") the Browser object will be marshalled.
Reading through this issue it seems I wasn't the only one with this problem and the suggested solution was to find my IE window that I opened and connect to it, so I have this function:
private void FindIwdBrowser()
{
if (Browser != null)
return;
SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
foreach (var window in shellWindows)
{
SHDocVw.InternetExplorer foundBrowser = ((SHDocVw.InternetExplorer)window);
if (!foundBrowser.Name.EndsWith("Internet Explorer"))
continue;
if (foundBrowser.LocationURL.StartsWith(config.UrlTemplate.Substring(0, config.UrlTemplate.IndexOf("?"))))
{
Execute.OnUIThread(() =>
{
foundBrowser.OnQuit += Browser_OnQuit;
Browser = foundBrowser;
});
return;
}
}
}
- Execute.OnUIThread - I use caliburn micro, wanted to make sure it's not a thread issue
- config.UrlTemplate - the URL I'm navigating to
This solution works... sort of, the only problem is that if it's the first tab in a new window that I open - it doesn't recognize it, if it's the second one (even if I close the first one thereafter) it does work.
after almost a week of frantically searching the internet I decided to ask the great minds here - maybe you could figure out why it works only half the times, or if my tactic is flawed and I should try a different approach.
I'd rather not to use a web client control as the website I'm navigating to is also a 3rd party and tends to crash a lot, and if it'll be within a webclient I create - the client might blame our software, which I would rather avoid.
Thanks in advance!