Archive for the ‘Win32 API’ Category

  1. Hello, Win32 GUI(WTL) World!

    Posted on 7月 4th, 2012 by cx20

    Win32 GUI(WTL)

    Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
    以下は VC++(WTL) における Win32 GUI アプリケーション の例となっている。

    ソースコード

    #include <atlbase.h>
    #include <atlapp.h>
    #include <atlcrack.h>
     
    class CHelloWindow : public CWindowImpl<CHelloWindow>
    {
        BEGIN_MSG_MAP( CHelloWindow )
            MSG_WM_PAINT   ( OnPaint   )
            MSG_WM_DESTROY ( OnDestroy )
        END_MSG_MAP()
     
        void OnPaint( HDC hDC )
        {
            CPaintDC dc( m_hWnd );
            LPCTSTR lpszMessage = _T("Hello, Win32 GUI(WTL) World!");
            dc.TextOut( 0, 0, lpszMessage, lstrlen(lpszMessage) );
        }
        void OnDestroy()
        {
            PostQuitMessage( 0 );
        }
    };
     
    CAppModule _Module;
     
    int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
    {
        _Module.Init(NULL, hInstance);
     
        CMessageLoop theLoop;
        _Module.AddMessageLoop(&theLoop);
     
        CHelloWindow wnd;
        wnd.Create( NULL, CWindow::rcDefault, _T("Hello, World!"), WS_OVERLAPPEDWINDOW | WS_VISIBLE );
        wnd.ResizeClient( 640, 480 );
        int nRet = theLoop.Run();
     
        _Module.RemoveMessageLoop();
        _Module.Term();
     
        return nRet;
    }

    コンパイル方法

    C:¥> SET INCLUDE=<WTL>Include;%INCLUDE
    C:¥> cl hello.cpp

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI(WTL) World!              |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  2. Hello, Win32 GUI(ATL) World!

    Posted on 7月 3rd, 2012 by cx20

    Win32 GUI(ATL)

    Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
    以下は VC++(ATL) における Win32 GUI アプリケーション の例となっている。

    ソースコード

    #include <atlbase.h>
    #include <atlwin.h> 
     
    class CHelloWindow : public CWindowImpl<CHelloWindow>
    {
        BEGIN_MSG_MAP( CHelloWindow )
            MESSAGE_HANDLER( WM_PAINT,   OnPaint   )
            MESSAGE_HANDLER( WM_DESTROY, OnDestroy )
        END_MSG_MAP()
     
        LRESULT OnPaint( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
        {
            PAINTSTRUCT ps;
            HDC hDC = GetDC();
            LPCTSTR lpszMessage = _T("Hello, Win32 GUI(C++) World!");
            BeginPaint( &ps );
            TextOut( hDC, 0, 0, lpszMessage, lstrlen(lpszMessage) );
            EndPaint( &ps );
            return 0;
        }
     
        LRESULT OnDestroy( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
        {
            PostQuitMessage( 0 );
            return 0;
        }
    };
     
    CComModule _Module;
     
    int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
    {
        _Module.Init(NULL, hInstance);
     
        CHelloWindow wnd;
        wnd.Create( NULL, CWindow::rcDefault, _T("Hello, World!"), WS_OVERLAPPEDWINDOW | WS_VISIBLE );
        wnd.ResizeClient( 640, 480 );
        MSG msg;
        while( GetMessage( &msg, NULL, 0, 0 ) ){
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
     
        _Module.Term();
     
        return msg.wParam;
    }

    コンパイル方法

    C:¥> cl hello.cpp

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI(ATL) World!              |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  3. Hello, Win32 GUI(C++) World!

    Posted on 7月 2nd, 2012 by cx20

    Win32 GUI(C++)

    Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
    以下は C++ における Win32 GUI アプリケーション の例となっている。

    ソースコード

    #include <windows.h>
    #include <tchar.h>
     
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps;
        HDC hdc;
        LPCTSTR lpszMessage = _T("Hello, Win32 GUI(C++) World!");
     
        switch (message)
        {
        case WM_PAINT:
            hdc = BeginPaint( hWnd, &ps );
            TextOut( hdc, 0, 0, lpszMessage, lstrlen(lpszMessage) );
            EndPaint( hWnd, &ps );
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
            break;
        }
     
        return 0;
    }
     
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        LPCTSTR lpszClassName = _T("helloWindow");
        LPCTSTR lpszWindowName = _T("Hello, World!");
     
        WNDCLASSEX wcex;
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = WndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = NULL;
        wcex.lpszClassName  = lpszClassName;
        wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
     
        RegisterClassEx(&wcex);
        HWND hWnd = CreateWindow(
            lpszClassName,
            lpszWindowName,
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
            NULL, NULL, hInstance, NULL
        );
     
        ShowWindow(hWnd, SW_SHOWDEFAULT);
        UpdateWindow(hWnd);
     
        MSG msg;
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
     
        return (int)msg.wParam;
    }

    コンパイル方法

    C:¥> cl hello.cpp /link user32.lib gdi32.lib /SUBSYSTEM:WINDOWS

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI(C++) World!              |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  4. Hello, Win32 GUI(C言語) World!

    Posted on 7月 1st, 2012 by cx20

    Win32 GUI(C言語)

    Win32 アプリケーションは Windows 標準 API である Win32 API を使用した Windows アプリケーションである。
    以下は C言語 における Win32 GUI アプリケーション の例となっている。

    ソースコード

    #include <windows.h>
    #include <tchar.h>
     
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        PAINTSTRUCT ps;
        HDC hdc;
        LPCTSTR lpszMessage = _T("Hello, Win32 GUI World!");
     
        switch (message)
        {
        case WM_PAINT:
            hdc = BeginPaint( hWnd, &ps );
            TextOut( hdc, 0, 0, lpszMessage, lstrlen(lpszMessage) );
            EndPaint( hWnd, &ps );
            break;
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
            break;
        }
     
        return 0;
    }
     
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
        LPCTSTR lpszClassName = _T("helloWindow");
        LPCTSTR lpszWindowName = _T("Hello, World!");
        WNDCLASSEX wcex;
        HWND hWnd;
        MSG msg;
     
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = WndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = NULL;
        wcex.lpszClassName  = lpszClassName;
        wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
     
        RegisterClassEx(&wcex);
        hWnd = CreateWindow(
            lpszClassName,
            lpszWindowName,
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
            NULL, NULL, hInstance, NULL
        );
     
        ShowWindow(hWnd, SW_SHOWDEFAULT);
        UpdateWindow(hWnd);
     
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
     
        return (int)msg.wParam;
    }

    コンパイル方法

    C:¥> cl hello.c /link user32.lib gdi32.lib /SUBSYSTEM:WINDOWS

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, Win32 GUI World!                   |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  5. 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   
    ---------------------------
  6. 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   
    ---------------------------
  7. 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   
    ---------------------------
  8. 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   
    ---------------------------
  9. 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   
    ---------------------------
  10. 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   
    ---------------------------