It’s specific for every browser. That’s for the major ones:
- Internet Explorer – You can use SHDocVw (like you did)
- Firefox – You can get the URL using DDE (source below)
- Chrome – You can get the URL while enumerating all the child windows untill you get to the control with class “Chrome_OmniboxView” and then get the text using
GetWindowText
- Opera – You can use the same thing as Firefox, but with “opera”
- Safari – There is no known method since it uses custom drawn controls
EDIT: Since 2014, Chrome has changed and you need to get the URL with Acessibility.
Code to get the URL from Firefox/Opera using DDE (which used NDDE – the only good DDE wrapper for .NET):
//
// usage: GetBrowserURL("opera") or GetBrowserURL("firefox")
//
private string GetBrowserURL(string browser) {
try {
DdeClient dde = new DdeClient(browser, "WWW_GetWindowInfo");
dde.Connect();
string url = dde.Request("URL", int.MaxValue);
string[] text = url.Split(new string[] { "\",\"" }, StringSplitOptions.RemoveEmptyEntries);
dde.Disconnect();
return text[0].Substring(1);
} catch {
return null;
}
}