Connecting to a running instance of an embedded webBrowser control

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

ok, I found a way to connect to a running instance of an (external) Internet
Explorer and access - for example - the html source. That works fine!

But now I have running application with an embedded IE webBrowser Control.
Is there a possibility for my application to access the webBrowser control in
the other application, too?

Thanks in advance, Randy
 
Use remoting. In otherwords, expose your Form (which is MarshalByRefObject) over a TCP remoting channel and consume it in any other
..NET application by using client remoting. Make a public property for access to the browser control.

public AxWebBrowser Browser { get { return browserCtrl; } }

public TheForm()
{
System.Runtime.Remoting.Channels.Tcp.TcpChannel channel =
new System.Runtime.Remoting.Channels.Tcp.TcpChannel(1024);

System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel);

System.Runtime.Remoting.RemotingServices.Connect(typeof(TheForm),
"tcp://localhost/theform:1024");
}

To consume the form, your project must have a reference to the assembly which contains TheForm:

public AXWebBrowser GetBrowserFromRemotingForm()
{
System.Runtime.Remoting.Channels.Tcp.TcpChannel channel =
new System.Runtime.Remoting.Channels.Tcp.TcpChannel(0);

System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel);

TheForm form = Activator.GetObject(typeof(TheForm), "tcp://localhost/theform:1024");
return form.WebBrowser;
}


I didn't test the code, so it will probably require some mods. Also, realize that when remoting an object such as a Form, all
properties must be serializable in some fashion, whether through declaration or inheritance. In this case it might make more sense
to remote an object to act as a proxy for the Form class that can return the WebBrowser control.
 
Hi Dave,

thanks for your reply. Unfortunately the program with the embedded
webbrowser control is a "finished" application from another company (and I
don't have a chance to modify the source ;-)).

Is there a possibility to connect to the webbrowser control inside anyway
(to get the HTM Source)?

Thanks, Randy
 
Yes, there still is another solution: Interop.

Open Spy++ if you have it, and get the classname for the window you are after (the browser itself)

There are Win32 functions that allow code to access other processes, the main window, child windows, etc..

A function called FindWindowEx may be what your after:

http://msdn.microsoft.com/library/d...dowreference/windowfunctions/findwindowex.asp

[DllImport("user32.dll")]
private static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childAfter, string className, string windowName);

You can specify the MainWindowHandle (which can be obtained via System.Diagnostics.Process) of the process containing the embedded
web browser as the "parent" parameter. childAfter may be IntPtr.Zero in your circumstance, but read the docs to find out.
className should be that of the WebBrowser control (that you obtained via Spy++), and windowName can be specified as null.

------------------------------------------------------------------------
I'm not sure how to get an IUnknown pointer from just a window handle, but I assume it' possible. If so, a bit of research will get
you there. Maybe somebody will be nice enough to post a solution for you...
------------------------------------------------------------------------

If you get the IUnknown pointer, you can use the following line of code to get an interface for the browser:

SHDocVw.IWebBrowser2 browser =
System.Runtime.InteropServices.Marshal.GetTypedObjectForIUnknown(pUnk, typeof(SHDocVw.IWebBrowser2));

At this point, you can use your existing code to access the HTML.


GL :)
 
Hi Dave,

thanks a lot again....now all is much clearer for me...

unfortunately I could't find anything in the www that solves the problem
above... Could anybody help me? or does anybody know a complete script that
shows the way to solve the described problem?

Thanks a lot,

Randy
 
Back
Top