Ada Binding to Win32 (WinAPI)

First Release of Petzold Edition 5 Samples

Developed by: John Walker, JSW Technology
Distributed by: Aonix

Copyright John Walker & JSW Technology 2000. All Rights Reserved.
The information contained herein is subject to change without notice.

ObjectAda is a trademark of Aonix
Microsoft, Visual C++, Win32 and Windows NT are trademarks of Microsoft, Inc.

Last updated: 14 September 2000

Contents

This page will be used to post ongoing work on more samples for the benefit of existing and potential customers. These samples are derived from the examples in Programming Windows Fifth Edition by Charles Petzold and published by Microsoft Press (see http://www.charlespetzold.com/).

Many of the samples in Programming Windows Fifth Edition are very similar to the samples in Programming Windows 95 Edition 4, also by Charles Petzold and published by Microsoft Press. All the samples in Programming Windows 95 Edition 4 have been converted to Ada as described in Fourth Release of Petzold Edition 4 Samples and Petzold Edition 4 Chapter 20 Samples

Go back to Ada Binding to Win32 (WinAPI)

Go back to start of JSW Technology Home Page


Rationale

These samples are intended to help Ada programmers who are using Petzold Programming Windows Fifth Edition to learn about Win32. The Ada code is kept as close to the C samples as possible. This is intended to assist with cross-referencing between Petzold and the Ada code. When developing real Ada applications it is expected that a stronger Ada flavour will be adopted, particularly for string handling.

Go back to Contents


Microsoft Articles

This document makes reference to some Microsoft articles. You can obtain these articles by e-mail from mshelp@microsoft.com. Enter the Article ID number (Qnnnnnn) in the Subject line of your message.

Go back to Contents


Compatibility

These samples have been built as described using:

These samples have not, as yet, been tested using the GNAT version of WinAPI.

Note that the sample UpdDemo in Chapter 23 also requires WinInet which is available from this web site at Ada Binding to Win32 (WinAPI) Version 3.1 Release 2 Upgrade

OpenPack 7.2 includes WinAPI Version 3.1 which does not support the Win32 macros described in Unicode in the Win32 API. This release of the Petzold samples uses the ASCII form of these functions.

The Petzold samples have been developed using a development version of WinAPI which supports these macros as well as ASCII and Unicode versions of the bindings. These features will be included in a future release of WinAPI.

Go back to Contents


Installation

The samples can be downloaded using petzold51.zip (38 KB) into objectada\winapi.

The sources can be extracted directly into the samples directories set up by the product installation. For example, assuming that ObjectAda is installed in c:\program files\aonix\objectada then the files can be extracted as follows:

>c:
>cd \"program files"\aonix\objectada\winapi
>..\bin\unzip petzold51.zip
Note that unzip.exe is supplied with ObjectAda in objectada\bin.

That should have installed the following samples directories:

objectada\winapi\samples\petzold\edition5
objectada\winapi\samples\petzold\edition5\ascii
objectada\winapi\samples\petzold\edition5\unicode
objectada\winapi\samples\petzold\edition5\ch01
objectada\winapi\samples\petzold\edition5\ch03
objectada\winapi\samples\petzold\edition5\ch04

... etc

Go back to Contents


Project Setup

This section provides a step by step guide to setting up a project for the sample HelloWin. Similar steps are required to set up projects for the other samples.

Create a project using ObjectAda File New Project. Set Project name to hellowin. Choose a directory for the project, for example:

C:\Program Files\Aonix\ObjectAda\WinAPI\Samples\petzold\edition5\ch03\HelloWin

Use Project Add Files to add the Ada sources described in Petzold Chapter 3. Note that petzold\extracon.ads and petzold\gnatcon.ads are alternates. Extracon.ads is required for ObjectAda, gnatcon.ads is required for GNAT.

Use Project Settings Search to add to the Directory Links the WinAPI library:

..\winapi\binding\lib

Use Project Settings Build to set the main unit to hellowin.

Use Project Settings Link to set the Application type to Windows and set Remove uncalled code.

Select Build Compile All to compile the application.

Use Build Build hellowin.exe to build the application and Build Execute hellowin.exe to run it.

By default that will build an application with debug options. Use Project Targets Win32(Intel)Release to build without debug options.

Go back to Contents


Resource Files

Resource files (*.res) are included with the Ada sources where required. They should be included using Project Add Files. The resource files can be rebuilt from their source files using the resource compiler provided with ObjectAda as described in Programming Windows Fifth Edition.

Go back to Contents


Missing DLLs

The following DLLs should be installed in order to run executables on Windows 95:

OpenGL.dll
Glu32.dll
Note that error messages relating to missing DLLs are displayed when running Windows applications directly but not when invoked using Project Execute in the ObjectAda IDE.

Go back to Contents


Helpers and Helpers.TC

The WinAPI bindings are generated using the general purpose C++ to Ada Binding Technology. The packages Helpers and Helpers.TC include extensions to the bindings which have been found useful in the development of the Petzold samples. The packages are included in the following source files:

petzold\edition5\helpers.ads
petzold\edition5\helpers.adb
petzold\edition5\ascii\tc.ads   : ASCII version
petzold\edition5\unicode\tc.ads : Unicode version

WinMain Arguments

Helpers includes the following functions which are bound to Ada runtime functions which return the arguments available to WinMain in C and C++ programs:
hInstance
hPrevInstance
lpCmdLine
nCmdShow

Win32 Characters and Strings

Helpers includes support for Win32 ASCII and Unicode characters and strings.

The Character Helpers.Nul provides the null character used at the end of Win32 null terminated ASCII strings.

The function Helpers.To_AddressOfChar returns a pointer, of type VC.AddressOfChar, to the first character of the String parameter. This is the correct type and value for Win32 ASCII strings of type char * in C and C++. Typically a null terminated Ada string can be passed to a Win32 ASCII function as follows:

Helpers.To_AddressOfChar ("An Ada string" & Helpers.Nul)

The Wide_Character Helpers.Wide_Nul provides the null character used at the end of Win32 null terminated Unicode strings.

The function Helpers.To_AddressOfwchar_t returns a pointer, of type WinAPI.AddressOfwchar_t, to the first character of the Wide_String parameter. This is the correct type and value for Win32 Unicode strings of type wchar_t * in C and C++. Typically a null terminated Ada Wide_String can be passed to a Win32 Unicode function as follows:

Helpers.To_AddressOfwchar_t ("An Ada string" & Helpers.Wide_Nul)

Two versions of Helpers.TC are provided. One version supports ASCII characters and strings, the other supports Unicode characters and strings. Ada applications can be written such that an ASCII or Unicode version of an application can be built by including the appropriate version of Helpers.TC.

Helpers.TC.Nul provides the null character used at the end of a Win32 ASCII or Unicode null terminated string.

In the ASCII versions of Win32 and WinAPI TCHAR is declared as char (VC.char in WinAPI) and PTCHAR as char * (VC.AddressOfChar in WinAPI). In the Unicode versions TCHAR is declared as wchar_t (WinAPI.wchar_t) and PTCHAR as wchar_t * (WinAPI.AddressOfwchar_t).

The function Helpers.TC.To_PTCHAR returns a pointer, of type WinAPI.PTCHAR, to the first character of the string parameter. In the ASCII version ASCII strings and characters are used, in the Unicode version Unicode characters and strings are used. Typically a null terminated Ada string can be passed to a Win32 function as follows:

Helpers.TC.To_AddressOfPTCHAR ("An Ada string" & Helpers.TC.Nul)

Macros

The remainder of the packages Helpers and Helpers.TC include implementations of some Win32 macros as Ada subprograms.

Go back to Contents


ExtraConstants

The package ExtraConstants includes additional constants to those generated by the C++ to Ada Binding Technology in WinAPIConstants. These constants have been found useful in the development of the Petzold samples. ExtraConstants is included in the source file:

petzold\edition5\extracon.ads
The following constants provide null values for some Win32 types:
Null_HINSTANCE
Null_HWND
Null_HMENU
The specification of these types changed between version 4.2 and 6.0 of Visual C++ causing an incompatibility when using GNAT 3.10p. The constants are provided to ease migration from 4.2 to 6.0 using GNAT.

The remainder of the package ExtraConstants includes some Win32 constants not included in WinAPIConstants.

Go back to Contents


Variable Arguments

The function wsprintfA, used in many of the samples, has variable arguments in C. It is used in Ada by copying the specification from WinAPI and adding the variable arguments required as shown in petzold\edition5\sysmets1\wndpack.adb (for example). The pragma Import has been modified so that it will work with GNAT 3.10p as well as ObjectAda.

Go back to Contents


"Optimisation" on toupper et al

The Link_Name used for some functions is incorrect in versions of WinAPI previous to 3.2 (see Release Note - Optimisation). This affects the function toupper in sample HexCalc. The workaround described in Release Note - Optimisation is used.

In some cases, such as strupr and ltoa also used in HexCalc, the optimisation is valid.

Go back to Contents


Aliased Record Components

The components of records will be aliased in a future release of WinAPI. In the meantime the sample OwnerDrw illustrates a workaround for the current release.

Go back to Contents


Chapter 1

The sample HelloMsg requires the following source files:

petzold\edition5\extracon.ads : package ExtraConstants for ObjectAda
petzold\edition5\gnatcon.ads  : package ExtraConstants for GNAT
petzold\edition5\helpers.ads
petzold\edition5\helpers.adb

petzold\edition5\ascii\tc.ads

petzold\edition5\ch03\hellowin\hellomsg.adb

Go back to Contents


Chapter 3

The sample HelloWin requires the following source files:

petzold\edition5\extracon.ads : package ExtraConstants for ObjectAda
petzold\edition5\gnatcon.ads  : package ExtraConstants for GNAT
petzold\edition5\helpers.ads
petzold\edition5\helpers.adb
petzold\edition5\wndpack.ads

petzold\edition5\ascii\tc.ads

petzold\edition5\ch03\hellowin\hellowin.adb
petzold\edition5\ch03\hellowin\wndpack.adb
The packages ExtraConstants and Helpers are described above.

HelloWin

HelloWin is the main subprogram. It reflects the C code described by Petzold. Note the use of Helpers to obtain the parameters to WinMain and for string handling. The exception handler has been added as an aid to debugging.

WndProc

WndProc is the window procedure. Note that it must be exported with calling convention stdcall. This procedure is called by Windows through the pointer in the class structure registered by the main subprogram, HelloWin.

In this simple example the window procedure is implemented as a library subprogram. In more complex examples the window procedure may be encapsulated within a package.

Go back to Contents


Petzold Chapter 4

The samples SysMets1, SysMets2 and SysMets3 require the following source files:

petzold\edition5\extracon.ads : package ExtraConstants for ObjectAda
petzold\edition5\gnatcon.ads  : package ExtraConstants for GNAT
petzold\edition5\helpers.ads
petzold\edition5\helpers.adb
petzold\edition5\wndpack.ads

petzold\edition5\ascii\tc.ads

petzold\edition5\ch04\sysmets.ads  : package SystemMetrics
and the source files from the relevant directory below:
petzold\edition5\ch04\sysmets1
petzold\edition5\ch04\sysmets2
petzold\edition5\ch04\sysmets3

The packages ExtraConstants and Helpers are described above.

SystemMetrics

The package SystemMetrics maps to the C header file sysmets.h. It is used by SysMets1, SysMets2 and SysMets3.

Note that WndProc assumes the array index runs from 0 and that NUMLINES is the length of the array sysmetrics.

WndPack

The package WndPack is used to encapsulate the window procedure WndProc which uses static variables. The specification of WndPack is common to SysMets1, SysMets2 and SysMets3. However the implementations are different.

The function wsprintfA, used in the body of WndPack, has variable arguments in C. It is used in Ada by copying the specification from WinAPI and adding the variable arguments required as shown in the body of WndPack. The pragma Import has been modified so that it will work with GNAT 3.10p as well as ObjectAda.

The implementation of WndProc is chosen to reflect the code described by Petzold. An "Ada" application might choose a different implementation, particularly for the creation of the strings for painting.

Go back to Contents


Petzold Chapter 23

The samples NetTime and UpdDemo require the following source files:

petzold\edition5\extracon.ads : package ExtraConstants for ObjectAda
petzold\edition5\gnatcon.ads  : package ExtraConstants for GNAT
petzold\edition5\helpers.ads
petzold\edition5\helpers.adb

petzold\edition5\ascii\tc.ads
and the source and resource (.res) files from the relevant directory below:
petzold\edition5\ch23\nettime
petzold\edition5\ch23\upddemo
The resource files can be rebuilt from their source files using the resource compiler provided with ObjectAda as described in Programming Windows Fifth Edition.

Note that UpdDemo also requires WinInet which is available from this web site at Ada Binding to Win32 (WinAPI) Version 3.1 Release 2 Upgrade

At the time of writing UPDDEMO fails to find the FTP site ftp.cpetzold.com on my development system. This problem also occurs with the Petzold C version and is under investigation.

Go back to Contents