Ada Programming/Platform/Windows/Visual C++ - GNAT interface

This is a guide for interfacing from Ada to C++ in Windows, using GNAT and MS Visual C++.

Before starting, you should check your environment. This was the environment used while writing this guide.


 * 1) Ada GPL 2006 and GPS.
 * 2) Microsoft Visual Studio 2005

These are the steps we will follow:
 * 1) write your Ada code and build dynamic library
 * 2) make a .lib file (for static linking)
 * 3) work together
 * 4) run your code

Step 1. Write Ada source code
- t.ads interfacesc t      C  interfacesc testabzdecint export stdcall test "test" t

t.adb adatext_io tth t      testabzdecint myd integer  0 tthtgetToken myd adatext_ioput_linemydimg " and e value is"  cinte t

th.ads tth T         getToken t   integer d integer  0 tth

-th.adb adatext_io tth T         adatext_io getToken t  integer t d               getToken d d  1 tth

And most important thing is your project file. It make you more easy.

project Testdll is   for Library_Name use "Te"; for Library_Dir use "dll"; for Library_Ali_Dir use "ali"; for Library_Kind use "dynamic"; for Languages use ("Ada"); for Object_Dir use "obj"; for Library_Interface use ("t"); for Library_Auto_Init use "False"; for Library_Src_Dir use "dll"; end Testdll;

You can configure almost all settings in your GPS IDE. But note that GPS cannot do Library_Interface setting correctly. After configuring it, you should check the project file and modify it accordingly. Library_Interface means which package contains export functions in DLL.

Library_Auto_Init is interesting and you must choose a correct setting in your project.

If you choose True, it will call adaInit when library load (which means elaboration appears in loading time automatically) if choose False, it will export init and finalize functions in DLL and you must call them by yourself.

If you export functions like C/C++ "normal" functions without any elaboration parts, you may set library_auto_init to True.

If your Ada code does not only work like C/C++ but also needs Ada elaboration function, you should initialize/finalize by yourself.

"DANGEROUS" DANGEROUS" DANGEROUS"DANGEROUS"DANGEROUS"

For example, if you have a library level task needing elaboration in load time and use implied linking and loading it will "deadlock WAITING" when DllMain calls init code. It's unexpected behaviour that you don't want to.

DANGEROUS"DANGEROUS"DANGEROUS"DANGEROUS"DANGEROUS"

In some case, you may use LoadLibrary / GetProcAddress to work (Explicit loading). But it is not easy to manage your code, especially if you export many functions.

Step 2. Make a .lib file (for static linking)
You can use utilities dumpbin and lib to generate .lib file (2.A)

dumpbin/exports Te.dll > te.def,

the file content is

Microsoft (R) COFF/PE Dumper Version 8.00.50727.42 Copyright (C) Microsoft Corporation. All rights reserved. Dump of file te.dll

File Type: DLL

Section contains the following exports for Te.dll

00000000 characteristics 44E2ABD2 time date stamp Wed Aug 16 13:23:30 2006 0.00 version 1 ordinal base 19 number of functions 19 number of names ordinal hint RVA     name 1   0 00001461 Tefinal 2   1 000011A0 Teinit 3   2 00005130 _CRT_MT 4   3 000050E0 t_E 5   4 000050D0 t__th_E 6   5 0000155F t__th__P2sIP 7   6 00007040 t__th___chain 8   7 00001745 t__th___elabb 9   8 0000156E t__th___elabs 10   9 00007044 t__th___master 11   A 000050D8 t__th__d 12   B 00007048 t__th__t 13   C 00006010 t__th__tT1 14   D 000016B9 t__th__tTKB 15   E 000050D1 t__th__tTKE 16   F 00001490 t__th__tTKVIP 17  10 000050D4 t__th__tTKZ 18  11 00005000 temain_E 19  12 0000178F test@20 Summary 1000 .bss 1000 .data 1000 .edata 2000 .idata 1000 .rdata 1000 .reloc 2E000 .stab E3000 .stabstr 4000 .text

(2.b) remove all in Te.def but leave ...

EXPORTS Tefinal Teinit _CRT_MT t_E t___elabb t__th_E t__th__P2sIP t__th___elabb t__th__d t__th__tB t__th__tE t__th__tVIP t__th__tZ temain_E test@20

The Teinit and Tefinal is used to elaborate and terminate the Ada elaboration part after Te.dll loaded. (the name does not call adainit/adafinalize but still the same function as adainit/adafinalize)

Step 3. Work together in MS VC++
Start Visual Studio C++ project and add Te.lib to your linker setting.

Writing a Te.h header for Ada DLL (GPS should generate .lib and .h for you).

And write a C/C++ test code to generate a .exe file

Step 4. Run your code
Put .exe and .dll in the same directory and run the test

The loader will find dll following in this order:
 * 1) current directory
 * 2) path variable
 * 3) Windows directory

Put the dll and exe in the same directory, it is the best option.

|