How To - Using DLLs with ObjectAda


In order to use a DLL with ObjectAda, you will need to have a .lib file for the DLL and have a liker-only search link in your ObjectAda project settings. If you don't have the lib file (such as a DLL you didn't create or have source to), the following advice from Gordan White will tell you how to create it.
I've called functions in a
Borland C++ .dll from my Ada application; here's how.

My DLL exported functions defined in this header:

   #define  STRICT
   #include 

   typedef unsigned char U_Char ;      // 8-bit unsigned data
   typedef unsigned short int U_Int;   // 16-bit unsigned data
   typedef unsigned long U_Long ;      // 32-bit unsigned data
   #ifndef FALSE
      enum  Boolean {
           FALSE = 0, TRUE
      };
   #else
      enum Boolean {
       BOOL_FALSE = 0, BOOL_TRUE = 1
      };
   #endif

   typedef U_Int DOS_Error;
   typedef U_Long LBA; // logical block address
   const NUMBER_OF_DATA_BYTES = 512;
   typedef U_Char Data[NUMBER_OF_DATA_BYTES];

   extern void _export FAR get_device_parameters (
       DOS_Error FAR *dos_error,
       Boolean FAR *is_error
   );

   extern void _export FAR read_sector(
         LBA lba,
             Data FAR data,
         DOS_Error FAR *dos_error,
         Boolean FAR *is_error
   );

   extern void _export FAR write_sector(
         LBA lba,
             Data FAR data,
         DOS_Error FAR *dos_error,
         Boolean FAR *is_error
   );

The DOS command line "impdef sample.def sample.dll" created the following
sample.def file from the DLL:

LIBRARY     SAMPLE.DLL

EXPORTS
    @get_device_parameters$qpusp7Boolean @2   ;
get_device_parameters(unsigned short*,Boolean*)
    @read_sector$qulpucpusp7Boolean @1   ; read_sector(unsigned
long,unsigned char*,unsigned short*,Boolean*)
    @write_sector$qulpucpusp7Boolean @3   ; write_sector(unsigned
long,unsigned char*,unsigned short*,Boolean*)
    __DebuggerHookData             @4   

Then, the DOS command line "lib /def:sample.def /out:sample.lib
/machine:ix86" created the sample.lib file that I needed for the Ada link.
The impdef utility is Borland's product.
[Editors Note: A utility dll2def from Jerry's Ada on Win32 site will also do the job. There is also a freeware tool call implib32 available here.]
The lib program is in ObjectAda's bin directory.  I placed the sample.lib
file in a directory with a linker-only search link in my Ada project settings.

Finally, I wrote this Ada specification binding to the mangled C++ names:

package Sample_Dll is 

procedure Get_Device_Parameters(
    Dos_Error : out Short_Integer;
    Is_Error : out Boolean
);
pragma Import(Cdecl, Get_Device_Parameters, Link_Name =>
"@get_device_parameters$qpusp7Boolean");

type Data_Byte_Type is new Integer range 0 .. 255;
for Data_Byte_Type'Size use 8;
type Data_Array is array (1 .. 512) of Data_Byte_Type;

procedure Read_Sector(
    Lba : in Integer;
    Data : out Data_Array;
    Dos_Error : out Short_Integer;
    Is_Error : out Boolean
);
pragma Import(Cdecl, Read_Sector, Link_Name =>
"@read_sector$qulpucpusp7Boolean");

procedure Write_Sector(
    Lba : in Integer;
    Data : in Data_Array;
    Dos_Error : out Short_Integer;
    Is_Error : out Boolean
);
pragma Import(Cdecl, Write_Sector, Link_Name =>
"@write_sector$qulpucpusp7Boolean");

end Sample_Dll;

Contributed by: Gordan White
Contributed on: February 19, 1999
License: Public Domain