Internet Explorer 7+ Addon to control tabs
-
15-06-2021 - |
Question
Initial note, I am not a c# programmer, but I have developed addon/extensions for chrome and firefox, and I need a third extension for internet explorer for a work-related project.
I am trying to access the Internet Explorer instance from a BHO extension in IE. I've been using the following as resources:
- How to get started with developing Internet Explorer extensions?
- http://www.codeproject.com/Articles/19971/How-to-attach-to-Browser-Helper-Object-BHO-with-C
- http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/03a8c835-e9e4-405b-8345-6c3d36bc8941 (need help with this)
So, I have the basic addon working, and testing it by injecting a javascript alert box when the window is finished loading.
My addon will eventually have to manage two tabs (one acting as a host, the other a slave), and the last link mentioned deals with it, but it requires HWND handle for the browser, and I'm not sure how to get that.
It looks like it might be under the WebBrowser
member .Parent
but I don't know what class I'd need to cast it to get the IE handle.
La solution
So to get the handle of any browser or even any process already running on the machine, you can use the FindWindow methods within the:
User32.dll
by including a reference to it within your class in the following way:
// Find by class name and window name
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
// Find window name only
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
and you can call them within your class like following:
public FindMyWindow(string classname, string windowName)
{
IntPtr hWnd = FindWindow(classname, null);
// Or do
hWnd = FindWindowByCaption(IntPtr.Zero, windowName);
}
You can use
Spy++
Which comes with Visual Studio 2010 to find the classname if you want.