Mar 302009
 

Here we go, the HelloWorld for Windows written in Ada – put the codes below to a hellowwindows.adb, then run:

gnat make -IC:\GNAT\2008\include\Win32ada -LC:\GNAT\2008\lib\Win32Ada HelloWindows

HelloWindows

sure you need to change the include and library directory to the right one.

Compile it, run it with “hellowindows”, you will see a pop-up window as shown on right.

Here are codes:

-- 我的第一个Windows Ada程序
with Ada.Command_Line;
with Ada.Unchecked_Conversion;
with Interfaces.C;
with System;
with Win32;
with Win32.WinDef;
with Win32.WinMain;
with Win32.WinUser;

procedure HelloWindows is

    function CP (pch: Interfaces.C.CHAR_Array) return Win32.LPCSTR is
        function CSTR_To_Address is new Ada.Unchecked_Conversion(System.Address, Win32.LPCSTR);
    begin
        return CSTR_To_Address(pch(pch'FIRST)'ADDRESS);
    end CP;

    function WinMain( hInstance    : Win32.Windef.HINSTANCE;
	              hPrevInstance: Win32.Windef.HINSTANCE;
	              lpszCmdLine  : Win32.PCSTR;
	              nCmdShow     : Win32.INT) return Win32.UINT is


        szMessage : constant Interfaces.C.CHAR_Array := Interfaces.C.To_C( "你好,Ada世界!", True );
        szTitle   : constant Interfaces.C.CHAR_Array := Interfaces.C.To_C( "第一个Windows Ada程序" );

        hWnd : Win32.Windef.HWND;
        nretVal : Interfaces.C.int;
    begin
        hWnd := Win32.Winuser.GetDesktopWindow;
        nRetVal := Win32.Winuser.MessageBox( hWnd, CP(szMessage), CP(szTitle), Win32.Winuser.MB_OK );
        return 0;
    end WinMain;

begin
    Ada.Command_Line.Set_Exit_Status(
        Ada.Command_Line.Exit_Status(
            WinMain( Win32.Winmain.Get_hInstance,
                     Win32.Winmain.Get_hPrevInstance,
                     Win32.Winmain.Get_lpCmdline,
                     Win32.Winmain.Get_nCmdShow )
        )
    );

end HelloWindows;
 Posted by at 16:52

  2 Responses to “My first Windows Ada program”

  1. Too bad, I run into this problem again – seems WordPress does not support codes quite well, I have to dig out how to make it looks better, but later. 🙂

  2. Here’s how to solve Chinese displaying comments and strings problem in gps, other languages may use similar solution: in gps, from menu Edit -> Preferences, in General, make sure Character set is GB2312, in Editor -> Fonts & Colors, use Chinese fonts for “Comments” and “Strings”, fonts like MingLiu, SimSun, SimHei, etc.

    I won’t use Chinese for identifiers so I don’t need anything else to be Chinese enabled.

Sorry, the comment form is closed at this time.