Archive for the ‘Win32 API’ Category

  1. 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   
    ---------------------------
  2. 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   
    ---------------------------
  3. 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   
    ---------------------------
  4. 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   
    ---------------------------
  5. 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   
    ---------------------------
  6. 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   
    ---------------------------
  7. 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   
    ---------------------------
  8. 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   
    ---------------------------
  9. Hello, Win32 API(Go) World!

    Posted on 5月 6th, 2012 by cx20

    Win32 API(Go)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は Go言語 にて syscall.Syscall 関数を用いた呼出し例である。

    ソースコード

    package main 
     
    import ( 
        "syscall" 
        "unsafe" 
    ) 
     
    var ( 
        user32, _          = syscall.LoadLibrary("user32.dll") 
        procMessageBoxW, _ = syscall.GetProcAddress(user32, "MessageBoxW") 
    )
     
    func MessageBox(hwnd uintptr, text string, caption string, style uintptr) (int32) { 
        ret, _, _ := syscall.Syscall6(
            uintptr(procMessageBoxW), 
            4, 
            hwnd,
            uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(text))), 
            uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(caption))), 
            style, 
            0,
            0 ) 
        return int32(ret)
    } 
     
    func main() { 
        defer syscall.FreeLibrary(user32) 
        MessageBox( 0, "Hello, Win32 API(Go) World!", "Hello, World!", 0 )
    }

    Win32 データ型と Go言語 データ型の対応は主に以下のようになっている。

    Win32 データ型 C/C++ データ型 Go データ型
    HANDLE void * uintptr
    BYTE unsigned char uint8, byte
    SHORT short int16
    WORD unsigned short uint16
    INT int int32, int
    UINT unsigned int uint32
    LONG long int32
    BOOL int int
    DWORD unsigned long uint32
    ULONG unsigned long uint32
    CHAR char byte
    WCHAR wchar_t uint16
    LPSTR char * *byte
    LPCSTR const char * *byte, syscall.StringByPtr()
    LPWSTR wchar_t * *uint16
    LPCWSTR const wchar_t * *uint16, syscall.StringToUTF16Ptr()
    FLOAT float float32
    DOUBLE double float64
    LONGLONG __int64 int64
    DWORD64 unsigned __int64 uint64

    コンパイル方法

    C:¥> SET GOROOT=C:go
    C:¥> go build -ldflags -Hwindowsgui hello.go

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Go) World!
    ---------------------------
    OK   
    ---------------------------
  10. Hello, Win32 API(Delphi) World!

    Posted on 5月 5th, 2012 by cx20

    Win32 API(Delphi)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は Delphi からの呼出し例である。
    Win32 API の参照方法としては、external 宣言を用いる方法の他、Winapi.Windows ライブラリを用いる方法などがある。

    ソースコード(external 宣言)

    program hello;
     
    function MessageBox(hWnd: THandle; lpText: PAnsiChar; lpCaption: PAnsiChar; uType: Cardinal): Integer;
        stdcall; external 'user32.dll' name 'MessageBoxA';
     
    begin
        MessageBox( 0, 'Hello, Win32 API(Delphi) World!', 'Hello, World!', 0 );
    end.

    ソースコード(Winapi.Windows ライブラリ)

    program hello;
     
    uses
        Windows;
     
    begin
        MessageBox( 0, 'Hello, Win32 API(Delphi) World!', 'Hello, World!', MB_OK );
    end.

    Win32 データ型と Delphi データ型の対応は主に以下のようになっている。

    Win32 データ型 C/C++ データ型 Delphi データ型
    HANDLE void * THandle
    BYTE unsigned char Byte
    SHORT short Smallint
    WORD unsigned short Word
    INT int Integer
    UINT unsigned int Cardinal
    LONG long Integer
    BOOL int LongBool
    DWORD unsigned long LongWord
    ULONG unsigned long LongWord
    CHAR char AnsiChar
    WCHAR wchar_t WideChar, Char
    LPSTR char * PAnsiChar
    LPCSTR const char * PAnsiChar
    LPWSTR wchar_t * PWideChar, PChar
    LPCWSTR const wchar_t * PWideChar, PChar
    FLOAT float Single
    DOUBLE double Double
    LONGLONG __int64 Int64
    DWORD64 unsigned __int64 UInt64

    コンパイル方法(Delphi XE2)

    C:¥> dcc32 hello.pas

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Delphi) World!
    ---------------------------
    OK   
    ---------------------------