Archive for 2月 9th, 2013

  1. Hello, COM(Nemerle) World!

    Posted on 2月 9th, 2013 by cx20

    COM(Nemerle)

    COM(Component Object Model)はマイクロソフトの提唱するプログラム部品の仕様である。
    COM を用いて開発された部品であれば言語を問わず利用することができる。
    以下は Nemerle による COM クライアントの例となっている。

    ソースコード(実行時バインディング)

    using System;
    using System.Reflection;
    using System.Runtime.InteropServices;
     
    class Hello {
        public static Main() : void {
            def objType = Type.GetTypeFromProgID("Shell.Application"); 
            def shell = Activator.CreateInstance(objType);
            def hwnd = 0;
            def title = "Hello, COM(Nemerle) World!" : object;
            def option = 0;
            def rootFolder = 36;
            def param = array[ hwnd, title, option, rootFolder ];
            def folder = shell.GetType().InvokeMember( 
                "BrowseForFolder", BindingFlags.InvokeMethod, null, shell, param );
            when (folder != null)
            {
                _ = Marshal.ReleaseComObject(folder);
            }
            _ = Marshal.ReleaseComObject(shell);
        }
    }

    コンパイル方法(実行時バインディング)

    C:¥> ncc -o Hello.exe Hello.n

    実行結果

    +----------------------------------------+
    |Browse For Folder                    [X]|
    +----------------------------------------+
    | Hello, COM(Nemerle) Wolrd!             |
    |                                        |
    | +------------------------------------+ |
    | |[Windows]                           | |
    | | +[addins]                          | |
    | | +[AppCompat]                       | |
    | | +[AppPatch]                        | |
    | | +[assembly]                        | |
    | |     :                              | |
    | |     :                              | |
    | |     :                              | |
    | +------------------------------------+ |
    | [Make New Folder]    [  OK  ] [Cancel] |
    +----------------------------------------+