Posts Tagged ‘Win32 API’

  1. Hello, Win32 API(SWT) World!

    Posted on 1月 6th, 2013 by cx20

    Win32 API(SWT)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は Java の GUI ツールキット SWT の非公開 API を使用した Win32 API 呼出しの例となっている。

    ソースコード

    import org.eclipse.swt.internal.win32.OS;
    import org.eclipse.swt.internal.win32.TCHAR;
     
    public class Hello {
        public static void main(String[] args) {
            TCHAR lpText = new TCHAR(0, "Hello, Win32 API World!", true);
            TCHAR lpCaption = new TCHAR(0, "Hello, World", true);
            OS.MessageBox(0, lpText, lpCaption, OS.MB_OK );
        }
    }

    コンパイル&実行方法

    C:¥> javac -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello.java
    C:¥> java -cp org.eclipse.swt.win32.win32.x86_3.6.1.v3655c.jar;. Hello

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API World!
    ---------------------------
    OK   
    ---------------------------
  2. 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   
    ---------------------------
  3. Hello, Win32 API(COBOL) World!

    Posted on 6月 29th, 2012 by cx20

    Win32 API(COBOL)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は OpenCobol による Win32 API の呼び出し例となっている。
    上記 COBOL コンパイラは直接 Win32 API を呼び出すことが出来ない為、C言語(gcc)で作成したライブラリをリンクすることで呼出しを実現している。

    ソースコード(C言語)

    #include <windows.h>
     
    int msgbox( LPCSTR lpszHwnd, LPCSTR lpszText, LPCSTR lpszCaption, LPCSTR lpszType ) {
        typedef int (WINAPI *PFNMESSAGEBOX)(HWND, LPCSTR, LPCSTR, UINT);
        HWND hWnd  = (HWND)atoi(lpszHwnd);
        UINT uType = (UINT)atoi(lpszType);
        HANDLE hdll;
        PFNMESSAGEBOX MessageBox;
        hdll = LoadLibrary("user32.dll");
        MessageBox = (PFNMESSAGEBOX)GetProcAddress(hdll, "MessageBoxA");
        MessageBox( hWnd, lpszText, lpszCaption, uType );
        return;
    }

    コンパイル方法(GNU C)

    C:¥> gcc -c msgbox.c

    ソースコード(COBOL)

           IDENTIFICATION DIVISION.
           PROGRAM-ID. hello.
           DATA DIVISION.
           WORKING-STORAGE SECTION.
           01 HWND    PIC 9(1)  VALUE 0.
           01 MSG     PIC X(30) VALUE "Hello, Win32 API(COBOL) World!".
           01 CAPTION PIC X(13) VALUE "Hello, World!".
           01 STYLE   PIC 9(1)  VALUE 0.
           PROCEDURE DIVISION.
           CALL "msgbox" USING HWND MSG CAPTION STYLE.
           STOP RUN.

    コンパイル&リンク方法(OpenCobol)

    C:¥> cobc -x hello.cob msgbox.o

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(COBOL) World!
    ---------------------------
    OK   
    ---------------------------
  4. Hello, Win32 API(Fortran) World!

    Posted on 6月 28th, 2012 by cx20

    Win32 API(Fortran)

    Win32 API は、Windows の機能にアクセスする為の API(Application Programming Interface)である。
    以下は gfortran(GNU Fortran)ならびに g95 による Win32 API の呼び出し例となっている。
    上記の Fortran コンパイラは直接 Win32 API を呼び出すことが出来ない為、C言語(gcc)で作成したライブラリをリンクすることで呼出しを実現している。

    ソースコード(C言語)

    #include <windows.h>
     
    int msgbox_( HWND* phWnd, LPCSTR lpszText, LPCSTR lpszCaption, UINT* puType ) {
        typedef int (WINAPI *PFNMESSAGEBOX)(HWND, LPCSTR, LPCSTR, UINT);
     
        HANDLE hdll;
        PFNMESSAGEBOX msgbox;
        hdll = LoadLibrary("user32.dll");
        msgbox = (PFNMESSAGEBOX)GetProcAddress(hdll, "MessageBoxA");
        return msgbox( *phWnd, lpszText, lpszCaption, *puType );
    }

    コンパイル方法(GNU C)

    C:¥> gcc -c msgbox.c

    ソースコード(Fortran)

    call msgbox( 0, 'Hello, Win32 API(Fotran) World!'//CHAR(0), 'Hello, World!'//CHAR(0), 0 )
    end

    コンパイル&リンク方法(GNU Fortran)

    C:¥> gfortran -o hello hello.f90 msgbox.o

    コンパイル&リンク方法(g95)

    C:¥> g95 -o hello hello.f90 msgbox.o

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Fortran) World!
    ---------------------------
    OK   
    ---------------------------
  5. Hello, Win32 API(Pascal) World!

    Posted on 6月 28th, 2012 by cx20

    Win32 API(Pascal)

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

    ソースコード

    {$APPTYPE GUI}
    {$MODE DELPHI}
    program hello;
     
    uses
        Windows;
     
    begin
        MessageBox( 0, 'Hello, Win32 API(Pascal) World!', 'Hello, World!', MB_OK );
    end.

    コンパイル方法(Free Pascal)

    C:¥> fpc hello.pas

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Pascal) World!
    ---------------------------
    OK   
    ---------------------------
  6. Hello, Win32 API(Haskell) World!

    Posted on 6月 27th, 2012 by cx20

    Win32 API(Haskell)

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

    ソースコード

    import Graphics.Win32.GDI.Types
    import System.Win32.Types
     
    messageBox :: HWND -> String -> String -> UINT -> IO UINT
    messageBox hwnd text caption style =
      withTString text $  c_text ->
      withTString caption $  c_caption ->
      failIfZero "MessageBox" $ c_MessageBox hwnd c_text c_caption style
    foreign import stdcall unsafe "windows.h MessageBoxW"
      c_MessageBox :: HWND -> LPCTSTR -> LPCTSTR -> UINT -> IO UINT
     
    main = messageBox nullHANDLE "Hello, Win32 API(Haskell) World!" "Hello, World!" 0

    コンパイル方法

    C:¥> ghc hello.hs

    実行結果

    ---------------------------
    Hello, World!
    ---------------------------
    Hello, Win32 API(Haskell) World!
    ---------------------------
    OK   
    ---------------------------
  7. 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   
    ---------------------------
  8. 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   
    ---------------------------
  9. 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   
    ---------------------------
  10. 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   
    ---------------------------