Click here to Skip to main content
13,458,022 members
Click here to Skip to main content
Add your own
alternative version


61 bookmarked
Posted 24 Sep 2006

Using the WebBrowser as an extention possibility

, 22 Jun 2007
Rate this:
Please Sign up or sign in to vote.
Creating your own extentions by using a simple WebBrowser object

Screenshot - MyActivitySDK.jpg


We have all seen the nice little features of the Microsoft MSN Messenger Activity SDK (well, I have ;-). MSN provides us with the SDK, With this SDK, the developer has the power to create little applications. Data can now easily be sent from one application to the other. I have seen a nice example on the Code Project. However, this article isn't about the implementation of the Messenger Activity SDK. Rather, it discusses taking advantage of the System.Windows.Forms.WebBrowser class in the .NET 2.0 Framework so you can create your own similar SDK.


Now how do the MSN Activity SDK developers make it possible for others to create nice extensions like that fun "Tic Tac Toe" game they give as an example? They let the developer have a choice between VBScript and JavaScript. Actually, they don't give you the choice explicitly; Internet Explorer gives you the choice of scripting languages. I personally prefer JavaScript as a scripting language, so I will show this little example in JavaScript.

JavaScript has a special namespace called window.external. This namespace is extendable by other applications or, to express it in a way I would never understand, "window.external is a built-in DOM object provided for host access." MSN Messenger uses this extendable space to provide other developers with a means to make nice web applications that can communicate with the hosting application. This, in combination with the WebBrowser class being able to call any method from the loaded page, can give you a powerful way of letting developers make extensions without any dangerous safety threats. Your application cannot be compromised in this way because the world out there has no way to get inside. At the same time, you have all the power to make more or fewer methods and properties available.

Using the code

In the demo, I have tried to keep the code as clean as possible. I hope you can understand everything. Enough talking for now, though. Let's show you some gold. ;-)

To begin, you will need a form with a WebBrowser on it and, of course, a webpage to view. I assume you can handle the creation of the form with WebBrowser and the basic HTML page. So, what do we want to do?

  • We want to call a method from the web page.
  • We want to call a method from the hosting application.
  • We want to get a string from the hosting application.
  • We want to set a string in the hosting application from the web page.

Let's start with calling a method from the web page in the hosting application:

private void myCallAMethodButton_Click(object sender, EventArgs e) 
    // You can call any method from the page you loaded... 
    // I have enclosed this call in a try-catch, because it will 
    // create a exception when there isn't a page loaded ... 
        // If you call a method, you can use a object array to 
        // supply parameters. My method only has 1 parameter, 
        // so I give 1 object in the array. 
            new object[] { "Wow, impressive !" }); 
    catch (Exception e) 

By the way, you should check out the Document property. It contains lots of valuable methods and other nice things. Let's continue while we have the spirit! First, let's call a method that is in the application, not in the HTML page. To call a method that is in the hosting application, you need to expose the class to the COM. Don't ask me why, it is just so.

[System.Runtime.InteropServices.ComVisible (true)] 
public class WindowExternal 
{ }

Any method or property in this class will be exposed to the script all through the window.external object. To view the members of the class, you should download the source. There isn't a lot to view in this class, nothing you don't yet know about. Let's now apply the class that we just created, WindowExternal, to the WebBrowser object in the Form:

public MainForm()
    myWebBrowser.ObjectForScripting = new WindowExternal(); 

You can assign any object to the ObjectForScripting property, even the form you are working in, as long the object is ComVisible!

Points of interest

People just don't know about the power JavaScript has. Simply learn the language and you will be able to do anything you want in C# or Delphi. This is now the end of my little article; I hope you have enjoyed reading it.


  • 24 September, 2006 -- This is the first version of my article and my first here on CodeProject, so there is not a real history yet. But you can be sure that there will be follow-ups!
  • 10 December, 2006 -- Article updated.
  • 22 June, 2007 -- Article edited and moved to the main article base.


This article, along with any associated source code and files, is licensed under The BSD License


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

QuestionUsing a web page that is not not hosted in my applivation Pin
triplebit20-Jul-11 19:11
membertriplebit20-Jul-11 19:11 
AnswerThanks for your help Pin
dfg147817-Nov-09 16:03
memberdfg147817-Nov-09 16:03 
GeneralRunning the code in MAC OS Pin
miauwmiauwmiauw26-Jun-08 18:56
membermiauwmiauwmiauw26-Jun-08 18:56 
GeneralRe: Running the code in MAC OS Pin
Filip van der Meeren26-Jun-08 21:45
memberFilip van der Meeren26-Jun-08 21:45 
QuestionHow to call a script when you don't know its name? Pin
sduhd7-Apr-08 16:55
membersduhd7-Apr-08 16:55 
AnswerRe: How to call a script when you don't know its name? Pin
Filip van der Meeren7-Apr-08 23:15
memberFilip van der Meeren7-Apr-08 23:15 
GeneralYou listed 4 goals, but demonstrated only two. Pin
Ashaman23-Jun-07 4:10
memberAshaman23-Jun-07 4:10 
GeneralRe: You listed 4 goals, but demonstrated only two. Pin
Filip van der Meeren23-Jun-07 6:48
memberFilip van der Meeren23-Jun-07 6:48 
GeneralVery Cool Pin
merlin98122-Jun-07 11:43
membermerlin98122-Jun-07 11:43 
GeneralRe: Very Cool Pin
Filip van der Meeren23-Jun-07 7:11
memberFilip van der Meeren23-Jun-07 7:11 
QuestionEvents and managed object Pin
Airbornes10-May-07 10:22
memberAirbornes10-May-07 10:22 
AnswerRe: Events and managed object Pin
Filip van der Meeren23-Jun-07 7:03
memberFilip van der Meeren23-Jun-07 7:03 
GeneralClean and Crisp Pin
Siddharth Barman23-Mar-07 5:56
memberSiddharth Barman23-Mar-07 5:56 
GeneralRe: Clean and Crisp Pin
Filip van der Meeren5-Apr-07 22:53
memberFilip van der Meeren5-Apr-07 22:53 
QuestionHow to call function in form hosting webbrowser? Pin
Bill SerGio, The Infomercial King5-Feb-07 17:18
memberBill SerGio, The Infomercial King5-Feb-07 17:18 
AnswerRe: How to call function in form hosting webbrowser? Pin
Filip van der Meeren9-Feb-07 23:21
memberFilip van der Meeren9-Feb-07 23:21 
Generalunedited Pin
Muammar©9-Dec-06 23:15
memberMuammar©9-Dec-06 23:15 
GeneralRe: unedited Pin
Filip van der Meeren9-Dec-06 23:46
memberFilip van der Meeren9-Dec-06 23:46 
Generalwell said Pin
Darchangel10-Oct-06 8:47
memberDarchangel10-Oct-06 8:47 
GeneralExcellent Article Pin
rajantawate1( 15:04
memberrajantawate1( 15:04 
GeneralRe: Excellent Article Pin
Filip van der Meeren8-Oct-06 20:29
memberFilip van der Meeren8-Oct-06 20:29 
GeneralRe: Excellent Article Pin
Quentin Pouplard10-Dec-06 6:03
memberQuentin Pouplard10-Dec-06 6:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180323.1 | Last Updated 22 Jun 2007
Article Copyright 2006 by Filip van der Meeren
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid