Home > OCaml, Windows > Your first and second OCaml programs for Win32

Your first and second OCaml programs for Win32

Since “C Programming language” by Kernigan and Ritchie most books about C started with “first program in C” to print “Hello, world!” on console. Windows is so much different from Unix so it even has it’s own “main function” named “WinMain” (instead of “main” for Unix). Because of that huge difference books about programming with Win32API usually start with their own “first program in C for Windows”, simple 5-line program for showing same message using MessageBox function. You can find such examples in book by Petzold and tutorials like this. Let’s make exactly the same program in OCaml.

Program will look like:

open Win32

let _ =
  ignore (message_box null_hwnd “Content of MsgBox” “Caption of MsgBox” [MB_OK]);
  exit 0;

Yes, that’s it, simple. There are no WinMain here, because it’s OCaml, not C. OCaml specifies it’s own entry point to program. WinMain has several arguments; they are available as global variables, however they are stored not by implementation of WinMain, but by invoking some Win32 API functions.

After this simple program books go in different directions. Some books go to windows, message reading/dispatching cycle and window procedures. I prefer to go to dialogs, because first program was also about dialogs. Dialogs are convenient because they could be defined in resources. My second OCaml program for Win32 consists of two files. Here is a OCaml file:

open Win32

let dlg_processors = [
    on_wm_initdialog (fun ~wnd ~msg ~focus ->
        message_return true)
    on_wm_command (fun ~wnd ~msg ~notify_code ~id ~ctrl ->
        if id == control_id_of_standard IDOK && notify_code == bn_clicked then
           end_dialog ~dlg:wnd ~result:0;

let _ =
        ignore (
                ~name:(Rn_string “TestDialog”)
                ~proc:(standard_dialog_proc ~processors:dlg_processors));

        exit 0

        e ->
            let s = Printexc.to_string e in
            ignore (message_box ~wnd:null_hwnd ~text:s ~caption:”Uncaught exception” ~options:[MB_OK]);
            exit 1

And here is test.rc file:

#include <windows.h>
ABOUTDLG DIALOG 20, 20, 199, 99
CAPTION “Example caption”
FONT 8, “MS Shell Dlg”
  CONTROL “Hello everybody”,104,”STATIC”,SS_LEFT |WS_CHILD |WS_VISIBLE ,45,14,128,8
  CONTROL “example custom dialog”,105,”STATIC”,SS_LEFT |WS_CHILD |WS_VISIBLE ,45,35,99,8

First program used pre-defined generic purpose dialog, second procedure used custom dialog defined in resource file. I hope these two programs will give a good start for everybody who are interested in using OCaml for Win32 programming. Next steps will include making custom resizable Windows with menues, and much more. Unfortunatelly there are not so many information about writing GUI application in OCaml. But maybe with time there will be books even better then one by Petzold!

Categories: OCaml, Windows
  1. No comments yet.
  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: