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:

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.

Était-ce utile?

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top