Home > OCaml, Windows > Making Win32 GUI applications in OCaml

Making Win32 GUI applications in OCaml

In my recent article I was talking about my attempts to find language other that C/C++ for writing GUI applications. Well, I should admit that last time I did GUI application myself was 5 years ago. It was written in C++ and used MFC. All last time I did only server-side programming. Only now I need to write a new GUI application, and I want this application to be written in programming language other than C/C++, so I could practice that language.  So, I didn’t GUI programing for long time, so my opinion about languages, libraries and graphical toolkits is not very solid. But I’m windows user, and I have my opinion about GUI programs that I use. For me very important things about GUI program is that UI should look native and be fast, and that it should be small and launch quickly. That makes external impression about program.

Nowdays almost everybody use some object-oriented wrapper library around Windows ui.dll, because it simplifies development greatly. I know a good wrapper for C++, called SmartWin++, but I never used it myself seriously. I’ve just downloaded it and tried to build example applications. Executables are small and fast, and UI looks native. Code for applications is small and clean, especially compared to MFC-oriented applications. At last, SmartWin++ could be used with both Microsoft Visual C++ compiler and with MinGW compiler, which is also important for me. Probably soon they will add support for other compilers. All those things make me happy with SmartWin++ library if I choose C/C++ as implementation language.

Making access to Win32 UI library for other languages is more difficult, because there should be a binding to C for each function. Taking into account size of Win32API it’s a huge task. So, people who develop other languages usually do binding to some simplified wrapper around ui.dll. Microsoft did that for Visual Basic. Borland did that for Delphi. Perl, Python and OCaml have bindings to wxWidgets.

Some words about wxWidgets. It’s good library, and could be used for fast application development in C++, because it’s much simpler to use then raw Win32 UI API, and easier than MFC. For me the only problem of wxWidgets is that it’s not just object-oriented wrapper around Win32 UI, but cross-platform library with wrappers around lots of platform-specific toolkits. I don’t like this idea, and prefer SmartWin++.

So, finally, I came to conclusion: when you develop GUI, you don’t choose the language which you like, but choose the language which has best libraries for that GUI.

In abovementioned article I told that OCaml is very Unix-biased. I’m apologising for that, I was wrong. OCaml has good support for windows sockets and threads in standard library, but it also has good binding to Win32API as additional library. This makes it perfectly suitable for development of native Win32 UI applications. However, this path is not easy at the beginning. Here I whould like to share my own experience, hoping it will save somebody’s time.

First, few words about OCaml. For Win32 it’s available in three flavours: CygWin, MinGW and Microsoft Visual C++. Initially I thought that it means that those compilers were used to compile OCaml tools. That’s right, but also that means that those compilers will be used by OCaml tools themselves to compile your source code. If you installing MinGW version, you should have MinGW itself installed. If you installing Visual C++ version, then you should have Visual C++. Fortunatelly, both compilers are available for free. This additional requirement for C compiler was not obvious for me, and I’ve spent some time trying to figure it out.

If you want to use ocam-win32 library, you must have version of OCaml for Visual C++. I hope someday this library could be also built from MinGW.

Library is available in source form from here. Unzip it in any place, and you will have lots of sources in both C and Ocaml, together with several small text files. Those files are not very helpfull, so it’s not easy to figure out what to do. Finally I understood that I should make library myself. The only toolkit I had was MinGW, so I’ve tried to invoke “make”, but received “delimeter absent” error, because Makefile used spaces instead of tabs. After some time I’ve realized that I need Visual C++. I’ve installed free compiler and tried to build it by invoking nmake. No complains about delimeters this time, but OCaml kept invoking gcc, which complained about compiler keys. So I undestood that I need version of OCaml built for Visual C++. That was not obvious.  I’ve re-installed OCaml, and run nmake again. Better this time, now it was complaining about missing headers. It occured that I should have Microsoft Platform SDK installed. It’s big, so I’ve installed only Platform core SDK.  To correctly compile a lots of environment variables should be set correctly. Include directories should contain both Visual C++ runtime headers and Platform SDK headers. The same story for libraries. I was able to do that by first invoking “set environment” script of Protocol SDK, then, from same command prompt, invoking vcvars32.bat. Another attempt to compile, and this time it was complaining about missing “shlwapi.h” file. After some googling I’ve found that I should install not just Platform core SDK, but also IE SDK. I did that, and, finally, got the library!

There is a test application, which starts very fast and looks native. So, now I’m quite optimistic about usage of OCaml for GUI applications for Win32 platform. OCaml seems much easier language than C++.

Many thanks to authors of this library. I had lots of problems during installation because it’s a long time since I’ve last used Visual C++, and because library is  for hackers who know that to do. Library authours should extend their readme files, and they should also redistribute compiled versions of libraries.

Update: this article is a first one dedicated to GUI programming in OCaml. See subsequent posts which will describe by progress on the path. That’s not a tutorial, just information written by newbie, so don’t trust me.

Categories: OCaml, Windows
  1. Anonymous
    October 15, 2009 at 4:11 pm


  2. June 15, 2010 at 10:23 pm

    thank’s a lot!! for this article!

  3. Steff
    June 30, 2010 at 2:53 pm

    Which Windows did you use?

    On Windows 7 x64 I get:

    win32_commondialog.c(798) : error C2065: ‘WM_CHOOSEFONT_SETFLAGS’ : undeclared identifier
    win32_commondialog.c(811) : error C2065: ‘WM_CHOOSEFONT_SETLOGFONT’ : undeclared identifier

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: