Ada Binding to Win32

Using Win32

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

Copyright John Walker & JSW Technology 1997-2001. 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.
Phar Lap is Registered in the U.S. Patent and Trademark Office by Phar Lap Software, Inc.
ETS is a trademark of Phar Lap Software, Inc.

Last updated: 16 May 2001

This page will be used to publish hints and tips on the use of the Ada Binding to Win32. The comments here are relevant to:

which include bindings to part of Win32 as well as: Please send comments and suggestions to john@jswalker.demon.co.uk.


Contents

Go back to


The Symbol Table Viewer (STViewer)

The Symbol Table Viewer (STViewer) can be used to browse the symbol tables from which the Ada bindings are generated.

STViewer incorporates The Derive Ada Class Tool (DACTOOL).

STViewer and the symbol tables are distributed with the Ada bindings.

Go to start of Using Win32


The Derive Ada Class Tool (DACTool)

DACTool generates an Ada package specification and body corresponding to a C++ class derived from a known C++ base class.

DACTool is incorporated into The Symbol Table Viewer (STViewer).

Go to start of Using Win32


Win32 Symbols Missing from Ada Binding

Go to start of Using Win32


Unicode in the Win32 API

The Win32 API supports ANSI and Unicode (wide character) strings. Many Win32 functions are provided in two forms, one for ANSI, the other for Unicode. These functions can be identified by a trailing A for ANSI and W for Unicode in the function name (eg LoadLibraryA and LoadLibraryW). These functions are documented using their simple name (eg LoadLibrary).

The Win32 header files contain macros which convert simple names to ANSI or Unicode forms depending on whether the symbols UNICODE, for the Win32 header files, and _UNICODE for the C Run-time header files, are defined or not.

Version 4.1 and later versions of the bindings support the macros described above and two versions of the bindings are included, one for Unicode, with the symbols UNICODE and _UNICODE defined, and the other for ASCII with the symbols not defined.

The samples provided with version 4.1 and later versions of the bindings illustrate how to develop common application code for ASCII and Unicode implementations. Functions with Variable Arguments describes how functions with variable arguments can be used in common application code.

Go to start of Using Win32


Functions with Variable Arguments

The function wsprintfA, used in many of the WinAPI samples, is an example of a function with variable arguments in C. It is used in Ada by copying the specification from WinAPI and adding the variable arguments as shown in the following example from the sample source file petzold\edition5\ch13\devcaps2\wndpack.adb. The pragma import has been modified so that it will work with GNAT as well as ObjectAda.

   -- Win32 function with variable arguments
   -- This works for Objectada and GNAT
   function wsprintfA (
      P1 : WinAPI.LPSTR;
      P2 : WinAPI.LPCSTR;
      P3 : WinAPI.LPCSTR;
      P4 : VC.int)
   return VC.int;
   -- Most efficient convention for ObjectAda only
   -- pragma Import (dll_cdecl, wsprintfA, Link_Name=>"__imp__wsprintfA");
   -- Convention c works for GNAT and ObjectAda
   pragma Import (c, wsprintfA, "wsprintfA");

Typically the function may be used several times but with different parameter profiles. In such cases it can be overloaded as illustrated below.

   -- Win32 function with variable arguments
   -- This works for Objectada and GNAT
   function wsprintfA (
      P1 : WinAPI.LPSTR;
      P2 : WinAPI.LPCSTR;
      P3 : WinAPI.LPCSTR;
      P4 : VC.int)
   return VC.int;
   function wsprintfA (
      P1 : WinAPI.LPSTR;
      P2 : WinAPI.LPCSTR;
      P3 : WinAPI.LPCSTR;
      P4 : WinAPI.LPCSTR)
   return VC.int;
   function wsprintfA (
      P1 : WinAPI.LPSTR;
      P2 : WinAPI.LPCSTR)
   return VC.int;
   -- Most efficient convention for ObjectAda only
   -- pragma Import (dll_cdecl, wsprintfA, Link_Name=>"__imp__wsprintfA");
   -- Convention c works for GNAT and ObjectAda
   pragma Import (c, wsprintfA, "wsprintfA");

Support for the Unicode functions can be added as follows:

   function wsprintfW (
      P1 : WinAPI.LPWSTR;
      P2 : WinAPI.LPCWSTR;
      P3 : WinAPI.LPCWSTR;
      P4 : VC.int)
   return VC.int;
   function wsprintfW (
      P1 : WinAPI.LPWSTR;
      P2 : WinAPI.LPCWSTR;
      P3 : WinAPI.LPCWSTR;
      P4 : WinAPI.LPCWSTR)
   return VC.int;
   function wsprintfW (
      P1 : WinAPI.LPWSTR;
      P2 : WinAPI.LPCWSTR)
   return VC.int;
   -- Most efficient convention for ObjectAda only
   -- pragma Import (dll_cdecl, wsprintfA, Link_Name=>"__imp__wsprintfW");
   -- Convention c works for GNAT and ObjectAda
   pragma Import (c, wsprintfW, "wsprintfW");

Finally, if function calls common to ASCII and Unicode are required, overloaded functions can be implemented as follows:

   function wsprintf (
      P1 : WinAPI.LPSTR;
      P2 : WinAPI.LPCSTR;
      P3 : WinAPI.LPCSTR;
      P4 : VC.Int)
   return VC.int is
   pragma Inline (wsprintf);
   begin
      return wsprintfA (P1, P2, P3, P4);
   end wsprintf;
   function wsprintf (
      P1 : WinAPI.LPSTR;
      P2 : WinAPI.LPCSTR;
      P3 : WinAPI.LPCSTR;
      P4 : WinAPI.LPCSTR)
   return VC.int is
   pragma Inline (wsprintf);
   begin
      return wsprintfA (P1, P2, P3, P4);
   end wsprintf;
   function wsprintf (
      P1 : WinAPI.LPSTR;
      P2 : WinAPI.LPCSTR)
   return VC.int is
   pragma Inline (wsprintf);
   begin
      return wsprintfA (P1, P2);
   end wsprintf;

   function wsprintf (
      P1 : WinAPI.LPWSTR;
      P2 : WinAPI.LPCWSTR;
      P3 : WinAPI.LPCWSTR;
      P4 : VC.int)
   return VC.int is
   pragma Inline (wsprintf);
   begin
      return wsprintfW (P1, P2, P3, P4);
   end wsprintf;
   function wsprintf (
      P1 : WinAPI.LPWSTR;
      P2 : WinAPI.LPCWSTR;
      P3 : WinAPI.LPCWSTR;
      P4 : WinAPI.LPCWSTR)
   return VC.int is
   pragma Inline (wsprintf);
   begin
      return wsprintfW (P1, P2, P3, P4);
   end wsprintf;
   function wsprintf (
      P1 : WinAPI.LPWSTR;
      P2 : WinAPI.LPCWSTR)
   return VC.int is
   pragma Inline (wsprintf);
   begin
      return wsprintfW (P1, P2);
   end wsprintf;

Go to start of Using Win32