Archive for 6月 28th, 2012

  1. 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   
    ---------------------------
  2. 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   
    ---------------------------