Archive for the ‘.NET’ Category

  1. Hello, Win32 API(Oxygene) World!

    Posted on 6月 26th, 2012 by cx20

    Win32 API(Oxygene)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は Oxygene による Win32 API の呼出し例である。

    ソースコード

    namespace hello;
     
    interface
    uses
        System,
        System.Runtime.InteropServices;
     
    type
        Hello =  static class
    public
        [DllImport('user32.dll')]
        method MessageBox(hwnd: Integer; text: String; caption: String; utype: Integer): Integer; external;
        class method Main(args: array of String): Integer;
    end;
     
    implementation
     
    [STAThread] 
    class method Hello.Main(args: array of String): Integer;
    begin
        MessageBox( 0, 'Hello, Win32 API(Oxygene) World!', 'Hello, World!', 0 );
    end;
     
    end.

    コンパイル方法

    C:¥> oxygene hello.pas

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Oxygene) World!
    ---------------------------
    OK   
    ---------------------------
  2. Hello, Win32 API(Nemerle) World!

    Posted on 6月 25th, 2012 by cx20

    Win32 API(Nemerle)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は Nemerle による Win32 API の呼出し例である。

    ソースコード

    using System;
    using System.Runtime.InteropServices;
     
    class Hello {
        [DllImport("user32.dll")]
        public extern static MessageBox(hwnd : int, text : string, caption : string, type : int) : int;
     
        public static Main() : void {
            _ = MessageBox( 0, "Hello, Win32 API(Nemerle) World!", "Hello, World!", 0);
        }
    }

    コンパイル方法

    C:¥> ncc -o hello hello.n

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Nemerle) World!
    ---------------------------
    OK   
    ---------------------------
  3. Hello, Win32 API(Boo) World!

    Posted on 6月 24th, 2012 by cx20

    Win32 API(Boo)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は Boo による Win32 API の呼出し例である。

    ソースコード

    import System.Runtime.InteropServices
     
    [DllImport("User32.dll", EntryPoint:"MessageBox")]
    def MessageBox(hwnd as int, text as string, caption as string, type as int):
        pass
     
    MessageBox(0, "Hello, Win32 API(Boo) World!", "Hello, World", 0)

    コンパイル方法

    C:¥> booc Hello.boo

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Boo) World!
    ---------------------------
    OK   
    ---------------------------
  4. Hello, Win32 API(Cobra) World!

    Posted on 6月 23rd, 2012 by cx20

    Win32 API(Cobra)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    Ver0.8 現在、Cobra には Win32 API を直接呼び出す機能は実装されていないが、C# を経由することで、Win32 API を呼び出すことが可能となっている。

    ソースコード(C#)

    using System;
    using System.Runtime.InteropServices;
     
    namespace Win32Lib
    {
        public class Win32
        {
             [DllImport("user32.dll", CharSet=CharSet.Auto)]
             public extern static uint MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
        }
    }

    コンパイル方法(C#)

    C:¥> csc /target:library Win32Lib.cs

    ソースコード(Cobra)

    use Win32Lib
     
    class Hello
     
        def main is shared
            Win32.messageBox( IntPtr.zero, "Hello, Win32 API(Cobra) World!", "Hello, World!", 0 )

    実行方法

    C:¥> cobra hello.cobra

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Cobra) World!
    ---------------------------
    OK   
    ---------------------------
  5. Hello, Win32 API(Phalanger) World!

    Posted on 6月 22nd, 2012 by cx20

    Win32 API(Phalanger)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    Phalanger には Win32 API を直接呼び出す機能は実装されていないが、C# を経由することで、Win32 API を呼び出すことが可能となっている。

    ソースコード(C#)

    using System;
    using System.Runtime.InteropServices;
     
    namespace Win32Lib
    {
        public class Win32
        {
             [DllImport("user32.dll", CharSet=CharSet.Auto)]
             public extern static uint MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
        }
    }

    コンパイル方法(C#)

    C:¥> csc /target:library Win32Lib.cs

    ソースコード(Phalanger)

    <?
    use System;
    use Win32Lib;
     
    $win32 = new Win32LibWin32();
    $win32->MessageBox( new SystemIntPtr(0), "Hello, Win32 API(Phalanger) World!", "Hello, World!", 0);
    ?>

    コンパイル方法(Phalanger)

    C:¥> phpc /lang:pure /r:Win32Lib.dll Hello.php

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Phalanger) World!
    ---------------------------
    OK   
    ---------------------------
  6. Hello, Win32 API(ClojureCLR) World!

    Posted on 6月 21st, 2012 by cx20

    Win32 API(ClojureCLR)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    ClojureCLR には Win32 API を直接呼び出す機能は実装されていないが、C# を経由することで、Win32 API を呼び出すことが可能となっている。

    ソースコード(C#)

    using System;
    using System.Runtime.InteropServices;
     
    namespace Win32Lib
    {
        public class Win32
        {
             [DllImport("user32.dll", CharSet=CharSet.Auto)]
             public extern static uint MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
        }
    }

    コンパイル方法(C#)

    C:¥> csc /target:library Win32Lib.cs

    ソースコード(ClojureCLR)

    (System.Reflection.Assembly/LoadFrom "Win32Lib.dll")
    (Win32Lib.Win32/MessageBox 0  "Hello, Win32 API(ClojureCLR) World!" "Hello, World!", 0 )

    実行方法

    C:¥> Clojure.Main hello.clj

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(ClojureCLR) World!
    ---------------------------
    OK   
    ---------------------------
  7. Hello, Win32 API(IronScheme) World!

    Posted on 6月 20th, 2012 by cx20

    Win32 API(IronScheme)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は IronScheme による Win32 API 呼出し例となっている。

    ソースコード

    (import
        (rnrs)
        (ironscheme clr)
    )
     
    (define msgbox (pinvoke-call user32 MessageBox int (intptr string string int)))
    (msgbox 0 "Hello, Win32 API(IronScheme) World!" "Hello, World!" 0 )

    実行方法

    C:¥> isc hello.ss

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(IronScheme) World!
    ---------------------------
    OK   
    ---------------------------
  8. Hello, Win32 API(IronRuby) World!

    Posted on 6月 19th, 2012 by cx20

    Win32 API(IronRuby)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    IronRuby には Win32 API を直接呼び出す機能は実装されていないが、C# を経由することで、Win32 API を呼び出すことが可能となっている。

    ソースコード(C#)

    using System;
    using System.Runtime.InteropServices;
     
    namespace Win32Lib
    {
        public class Win32
        {
             [DllImport("user32.dll", CharSet=CharSet.Auto)]
             public extern static uint MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);
        }
    }

    コンパイル方法(C#)

    C:¥> csc /target:library Win32Lib.cs

    ソースコード(IronRuby)

    require 'System'
    require 'Win32Lib'
    include System
    include Win32Lib
     
    Win32.MessageBox( IntPtr.Zero, "Hello, Win32 API(IronRuby) World!", "Hello, World!", 0 )

    実行方法

    C:¥> ir hello.rb

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(IronRuby) World!
    ---------------------------
    OK   
    ---------------------------
  9. Hello, Win32 API(IronPython) World!

    Posted on 6月 18th, 2012 by cx20

    Win32 API(IronPython)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は IronPython にて clrtype ライブラリを使用した Win32 API の呼出し例である。

    ソースコード

    import clr
    import clrtype
    import System
    from System.Reflection import BindingFlags
     
    class Win32(object):
        __metaclass__ = clrtype.ClrClass
     
        from System.Runtime.InteropServices import DllImportAttribute
        DllImport = clrtype.attribute(DllImportAttribute)
     
        @staticmethod
        @DllImport("user32.dll")
        @clrtype.accepts(System.IntPtr, System.String, System.String, System.UInt32)
        @clrtype.returns(System.Int32)
        def MessageBox(hwnd, text, caption, type): raise RuntimeError("Runtime Error")
     
    Win32.MessageBox(System.IntPtr.Zero, "Hello, Win32 API(IronPython) World!", "Hello, World!", 0)

    実行方法

    C:¥> ipy hello.py

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(IronPython) World!
    ---------------------------
    OK   
    ---------------------------
  10. Hello, Windows Forms(UWSC) World!

    Posted on 6月 17th, 2012 by cx20

    Windows Forms(UWSC)

    Windows フォーム(Windows Forms)は .NET Framework におけるユーザーインターフェイス基盤である。Windows アプリケーションにおけるウィンドウやダイアログに対応する。
    以下は UWSC における Windows フォーム の例となっている。
    UWSC 自体は .NET 対応言語ではないが、v4.6b より追加された PowerShell() 関数により、PowerShell 経由で Windows フォームが利用できるようになっている。

    ソースコード(UWSC + PowerShell)

    Option Explicit
     
    Main()
     
    Procedure Main()
        PowerShell( helloBlock, True, True )
    FEnd
     
    TextBlock helloBlock
        [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
        $form = New-Object Windows.Forms.Form
        $form.Size = New-Object Drawing.Size 640,480
        $form.Text = "Hello, World!"
        $label1 = New-Object Windows.Forms.Label
        $label1.Size = New-Object Drawing.Size 320, 20
        $label1.Text = "Hello, Windows Forms(UWSC) World!"
        $form.Controls.Add( $label1 )
        $form.ShowDialog()
    EndTextBlock

    ソースコード(UWSC + PowerShell + C#)

    Option Explicit
     
    Main()
     
    Procedure Main()
        PowerShell( helloBlock, True, True )
    FEnd
     
    TextBlock helloBlock
        $source = @"
    using System;
    using System.Drawing;
    using System.Windows.Forms;
     
    public class HelloForm : Form
    {
        public HelloForm()
        {
            this.Size = new Size( 640, 480 );
            this.Text = "Hello, World!";
     
            Label label1 = new Label();
            label1.Size = new Size( 320, 20 );
            label1.Text = "Hello, Windows Forms(UWSC) World!";
     
            this.Controls.Add( label1 );
        }
        [STAThread]
        public static void Main()
        {
            HelloForm form = new HelloForm();
            Application.Run(form);
        }
    }
    "@
        Add-Type -Language CSharp -TypeDefinition $source -ReferencedAssemblies ("System.Drawing", "System.Windows.Forms" )
        [HelloForm]::Main()
    EndTextBlock

    ソースコード(UWSC + PowerShell + VB.NET)

    Option Explicit
     
    Main()
     
    Procedure Main()
        PowerShell( helloBlock, True, True )
    FEnd
     
    TextBlock helloBlock
        $source = @"
    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
     
    Public Class HelloForm
        Inherits Form
     
        Public Sub New()
            Me.Size = New Size( 640, 480 )
            Me.Text = "Hello, World!"
     
            Dim label1 As New Label
            label1.Size = New Size( 320, 20 )
            label1.Text = "Hello, Windows Forms(UWSC) World!"
     
            Me.Controls.Add( label1 )
        End Sub
     
        <STAThread> _
        Shared Sub Main()
            Dim form As New HelloForm()
            Application.Run(form)
        End Sub
    End Class
    "@
        Add-Type -Language VisualBasic -TypeDefinition $source -ReferencedAssemblies ("System.Drawing", "System.Windows.Forms" )
        [HelloForm]::Main()
    EndTextBlock

    実行方法

    C:¥> uwsc /K hello.uws

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Windows Forms(UWSC) World!         |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+