Click here to Skip to main content
Click here to Skip to main content

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

Introduction

We have all seen the nice little features of the Microsoft MSN Messenger Activity SDK (well, I have Wink | ;-) . MSN provides us with the SDK, messengeractivitySDK.zip. 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.

Background

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. Wink | ;-)

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 ... 
    try 
    { 
        // 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. 
        myWebBrowser.Document.InvokeScript("InvokeMethod",
            new object[] { "Wow, impressive !" }); 
    }
    catch (Exception e) 
    { 
        MessageBox.Show(e.Message);
    }
}

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()
{ 
    InitializeComponent();
  
    // This is it, THE MOST IMPORTANT THING IN YOUR APPLICATION ! :-)
    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.

History

  • 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 CodeProject.com article base.

License

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

Share

About the Author

No Biography provided

Comments and Discussions

 
QuestionUsing a web page that is not not hosted in my applivation Pinmembertriplebit20-Jul-11 19:11 
AnswerThanks for your help Pinmemberdfg147817-Nov-09 16:03 
GeneralRunning the code in MAC OS Pinmembermiauwmiauwmiauw26-Jun-08 18:56 
GeneralRe: Running the code in MAC OS PinmemberFilip van der Meeren26-Jun-08 21:45 
QuestionHow to call a script when you don't know its name? Pinmembersduhd7-Apr-08 16:55 
AnswerRe: How to call a script when you don't know its name? PinmemberFilip van der Meeren7-Apr-08 23:15 
GeneralYou listed 4 goals, but demonstrated only two. PinmemberAshaman23-Jun-07 4:10 
GeneralRe: You listed 4 goals, but demonstrated only two. PinmemberFilip van der Meeren23-Jun-07 6:48 
GeneralVery Cool Pinmembermerlin98122-Jun-07 11:43 
GeneralRe: Very Cool PinmemberFilip van der Meeren23-Jun-07 7:11 
QuestionEvents and managed object PinmemberAirbornes10-May-07 10:22 
AnswerRe: Events and managed object PinmemberFilip van der Meeren23-Jun-07 7:03 
GeneralClean and Crisp PinmemberSiddharth Barman23-Mar-07 5:56 
GeneralRe: Clean and Crisp PinmemberFilip van der Meeren5-Apr-07 22:53 
QuestionHow to call function in form hosting webbrowser? PinmemberBill SerGio, The Infomercial King5-Feb-07 17:18 
AnswerRe: How to call function in form hosting webbrowser? PinmemberFilip van der Meeren9-Feb-07 23:21 
Generalunedited PinmemberMuammar©9-Dec-06 23:15 
"My previous one did not go so well in scores and views"
 
dont worry Filip, just because it's not moved yet to the edited reader contributions. well, you got my 5! keep up the good work.
 

All generalizations are wrong, including this one!
(\ /)
(O.o)
(><)

GeneralRe: unedited PinmemberFilip van der Meeren9-Dec-06 23:46 
Generalwell said PinmemberDarchangel10-Oct-06 8:47 
GeneralExcellent Article Pinmemberrajantawate1(www.jhatak.com)8-Oct-06 15:04 
GeneralRe: Excellent Article PinmemberFilip van der Meeren8-Oct-06 20:29 
GeneralRe: Excellent Article PinmemberQuentin Pouplard10-Dec-06 6:03 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 22 Jun 2007
Article Copyright 2006 by Filip van der Meeren
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid