Blog

Mono.Mozilla.Browser On Windows

Update: Andreia has posted some really useful tips and “fixes to my approach that might interest you too. Check them, and a blog post detailing the process and some tunning.

A few weeks ago Andreia (a Portuguese like me :)) published in her blog about the latest improvements she has made towards a MWF WebBrowser control. I’m sure that, like me, many people become excited about this and gave it a try. In my case, I trust people when they say something is working on the Mono tree as long as its on Linux systems, so I wanted to test it on Windows and Andreia told me it was entirely possible. So hands to work and here is my progress so far, a bit reordered from my original path… for your sanity.

Prerequisites

First step is to read a bit about Mozilla Build Prerequisites and in my case, because I already had Visual Studio .Net 2003 with C#/C++ support and Visual Studio .Net 2005 with C#, was to get the pre-built Win Tools. You need to extract it somewhere and run one of the “start-*.bat” files according to your main Visual Studio. I did nothing more than that, it is suggested by the Mozilla Build Prerequisites page that you setup a variable and install it, I didn’t do it and it worked fine.

Next step is to get the mozembed Mono code. It can be obtained in several ways, including the Subversion Mono repository. Please note that is you do use Subversion to get the code you might have some problems, I use TortoiseSVN subversion and the “_svn” feature but maybe it would happen with “.svn” too, so I needed to delete the _svn folders from inside the mozembed/build directory – the correct thing to do is to checkout the code to any place in your system and then export it. Another noteworthy thing is that you need to export/place the mozembed in a path without spaces or special chars, something like c:\mozembed, because Mozilla builds on windows don’t handle spaces in paths correctly.

Once you’ve your mozembed tree the first step is to prepare some headers and extra files. This is done using the shell provided by the Mozilla Win Tools package. Just “cd c:\mozembed\build”. You need to get xulrunner and xulrunner sdk files extracted here, and for that there is a script called get-gecko-sdk.sh in the build folder that won’t work on Windows. Instead go to this Mozilla ftp folder, download and extract win32/xulrunner-1.8.1.3.en-US.win32.zip and sdk/xulrunner-1.8.1.3.en-US.win32.sdk.zip files into your mozembed/build folder. You also need libIDL-0.6.dll and glib-1.2.dll in that folder, these files can be found in the Mozilla Win Tools mozilla-build\moztools-180compat\bin folder. After that you just do “make” in the shell and all the headers shall be created and places in the correct folders.

Building xulbrowser.dll

Now its time to open the solution, mozembed/xulbrowser/xulbrowser.sln or the 2k5 version according to your Visual Studio version. In my case I removed the other projects from the solution until all I had left was the xulbrowser project. It also happens that the current (2003) solution lacks some files, so after the solution is open you need to “Add existing file..” and add this files: SecurityWarningDialog.cpp PromptService.cpp LocationProvider.cpp SecurityWarningDialog.h and PromptService.hLocationProvider.h; all are in the xulbrowser project directory.

Next, you open the project properties “C/C++ > Include Directories” and add C:\mozembed\build\gecko-sdk\include and C:\mozembed\build\include. The same goes to “Linker > Additional Library Directories” where you need to add c:\mozembed\build\lib. Finally your project should be ready to build except for a missing file that I really don’t understand where it is supposed to come from… so I just googled for “nsAppDirectoryServiceDefs.h” and downloaded the first that I saw. Place it in one of the include paths.

You should now have a complete xulbrowser.dll in the project folder.

Creating a Test application

Now, we need to get Mono.Mozilla, in my case from the Subversion Mono repository. Just open and build it, the only problem I came across was the Mono Assembly key that I removed. After this we’ve most of whats needed to run a Mozilla web browser in our applications, but we still lack a control implementation to be used. As I’m very lazy I just shortcutted it and got the Mono one from here. What we need is WebBrowser.cs and WebBrowserBase.cs.

I created a new Windows.Forms project in Visual Studio and added the 2 files (WebBrowser.cs and WebBrowserBase.cs) to it. The files obviously won’t build out-the-box, you need to remove or deactivate those MonoTODO attributes (a find and replace of “[MonoTODO” by “//[MonoTODO”) and change the namespace to something else, in my case I just appended a “1” to it leaving me with System.Windows.Forms1.

I then edited the form Visual Studio generated when creating the project and added a WebBrowser control from the toolbox and edited the URL property to point to “http://whatsmyuseragent.com/”. After that all I needed was to edit the code and change the namespace of the control to the new namespace assigned to the WebBrowser.cs implementation.

Lastly, your application should build but not run correctly. You need to place xulbrowser.dll next to the .exe of your application, but that leads to an ungly memory exception. There are some files missing and I’m not sure which files, but copying the .exe, Mono.Browser.dll and xulbrowser.dll to the c:\mozembed/build/xulrunner folder leaves you with a running application.

Results

screen.jpg

This is a nice result as long as it can be seen in the screenshot but the truth is that its not working except for clicks and selecting text. I can’t input any text to textboxes or anything else. But hey! At least its mozilla rendering it! Now all we need is bug fixing 😉

This is of course a fast and hackish way of getting things to work, but I hope its a start and soon we can have something really usefull to developers who whish to use Mozilla from Windows.Forms applications.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

6 Responses to “Mono.Mozilla.Browser On Windows”

  1. Hey! Thank you for trying it out, it’s always helpful to have unbiased eyes
    looking at how things are doing :) There’s a few things that should make things easier to get this going:

    Building:

    You don’t need the whole mozilla shell to setup the headers,
    the only thing you need is
    – the xulrunner sdk
    – xpidl.exe (from the xulrunner sdk)
    – libIDL-0.6.dll and glib-1.2.dll (from the wintools zip)

    There’s now a make.cmd on the “build”
    directory that does the same as the Makefile, so the steps are:

    – unzip the xulrunner sdk somewhere
    – put xpidl.exe and the two dlls in the build directory
    – open a command prompt, go
    there, and run “make [path-to-xulrunnersdk]”.

    In the include and linker configurations, you only need to point to your newly created build\include and build\lib, respectively. These
    contain everything that is in the sdk plus the extra header files.

    The missing nsAppDirectoryDefs header has been added, so you don’t need to google for it anymore. Ooopsie :)

    Running:

    To run an application, you always need to have in the same directory:
    – the contents of the xulrunner runtime package
    – your app’s exe :)
    – the mono.mozilla dll
    – the
    xulbrowser.dll

    If you want to have the runtime in a different directory, you’ll need to first register it: in a command prompt, go to the xulrunner runtime directory, and run “xulrunner.exe
    –register-global”. Then, on the build directory, run “setup-runtime.cmd [path-to-your-application-exe], which will create some directories that are required to be where your app is.

    WRT svn
    and tortoise:
    I use tortoise for all my work and I’ve never had any problems :p Of course, I’ve never used the _svn variant, it’s too much trouble and incompatibility just to have svn with
    webapps on vstudio. I suggest using the regular version for fun and happiness :)

    Browser problems:
    You should update your version, the problem with the project has been fixed since. Also,
    the problem you had with inputting text is fixed as well (12-09, iirc).

    Do nag me if you notice problems with the setup, projects, or anything else. I need to keep linux, windows+vs2k3 and
    windows+vs2k5 in synch, sometimes things slip by :)

    Thank you again for the tests!

  2. Hey there!

    I left a previous
    comment, but I don’t think it got in, so leaving another one :p

    Thanks for the testing, it’s great to have people look at this stuff! I’ve looked into some of the problems you had, and blogged
    about it (it’s a big long), so check it out at worldofcoding.com.

    Do let me know if you have any problems, I’d like to make things easy and smooth on all systems.

    Again, kudos for the great
    job and very nice post :)

  3. alexmipego says:

    Thank you Andreia, I’ll try each
    of your tip. I tried to get the xulrunner sdk and get from there but it didn’t work, maybe it was before I found some files were missing so I will give it another try :)

    About the version of
    xulrunner, are you talking about a mozilla svn one – because the one I tried was the last stable version…?

    @Your second post: Comments are delayed for approval, it is amazing how much spam a guy
    can receive in one day even with several filters and protections :(

  4. Ah, about the first post, it didn’t tell me the post had gone through, it just
    took me back to the original post when I submitted, so I figured it just didn’t like me. Ah well, now it’s blogged :)

    As for the version thing, I’m talking about the xulbrowser and mono.mozilla
    code in the mono svn, not the xulrunner (yes, the names are a mess… :p). The problems you had with not being able to input the text are related to my code and are fixed in mono svn
    head.

  5. Patricio says:

    Hi
    i have a problem
    the Mono Repository (
    http://anonsvn.mono-project.com/source/trunk/mozembed/ ) don’t work, somebody have another repository to download the mozembed sources?

  6. Glauber says:

    Hi,

    Andreia, I?.´m brazilian and need you help…

    You
    can move your email for me?

    thanks

Leave a Reply

For spam filtering purposes, please copy the number 7019 to the field below: