
Introduction
We have all seen the nice little features of the Microsoft MSN Messenger Activity SDK (well, I have ;-). 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. ;-)
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)
{
try
{
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();
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.