Archive for 6月 30th, 2012

  1. Hello, Win32 API(Ada) World!

    Posted on 6月 30th, 2012 by cx20

    Win32 API(Ada)

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

    ソースコード

    with Interfaces;    use Interfaces;
    with Interfaces.C;  use Interfaces.C;
    with System;        use System;
     
    with Ada.Unchecked_Conversion;
     
    procedure Hello is
       function LoadLibrary (lpFileName : char_array) return Unsigned_32;
       pragma Import (Stdcall, LoadLibrary, "LoadLibrary", "_LoadLibraryA@4");
     
       function GetProcAddress (hModule : Unsigned_32; lpProcName : char_array) return Address;
       pragma Import (Stdcall, GetProcAddress, "GetProcAddress", "_GetProcAddress@8");
     
       type MessageBox is access function (
          hWnd      : Address;
          lpText    : char_array;
          lpCaption : char_array;
          uType     : Unsigned_16
       ) return Integer_16;
       pragma Convention (Stdcall, MessageBox);
     
       function To_MessageBox is new Ada.Unchecked_Conversion (Address, MessageBox);
     
       Result : Integer_16;
       Library : Unsigned_32;
       Pointer : Address;
    begin
       Library := LoadLibrary (To_C ("user32.dll"));
       Pointer := GetProcAddress (Library, To_C ("MessageBoxA"));
       Result := To_MessageBox (Pointer) (
          Null_Address,
          To_C ("Hello, Win32 API(Ada) World!"),
          To_C ("Hello, World!"),
          0
       );
    end Hello;

    コンパイル方法(GNAT)

    C:¥> gnatmake hello.adb

    実行結果

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