Ada. Time-tested, safe and secure.
Driver on

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


Ada. Time-tested, safe and secure.

This can be a information for interfacing from Ada to C++ in Home windows, utilizing GNAT and MS Visible C++.

Earlier than beginning, you need to test your atmosphere.
This was the atmosphere used whereas penning this information.

  1. Ada GPL 2006 and GPS.
  2. Microsoft Visible Studio 2005

These are the steps we are going to comply with:

  1. write your Ada code and construct dynamic library
  2. make a .lib file (for static linking)
  3. work collectively
  4. run your code

Step 1. Write Ada supply code[edit]

----------------------------- t.advertisements
 with interfaces.c;
 package deal t is
 
    package deal C renames interfaces.c;
 
    process check(a,b,z,d,e:c.int) ;
    pragma export (stdcall, check, "check");
 
 finish t;
--------------------------------t.adb

 with ada.text_io;
 with t.th;
 package deal physique t is
    process check(a,b,z,d,e:c.int) is
       myd : integer := 0;
    start
       t.th.t.getToken (myd);
       ada.text_io.put_line(myd'img & " and e worth is" & c.int'picture(e));
    finish;
 
 finish t;
------------------------------------th.advertisements
 package deal t.th is
 
    process T is
       entry getToken (t : out integer);
    finish;
 personal
 d : integer := 0;
 
 finish t.th;
-------------------------------------th.adb

 with ada.text_io;
 package deal physique t.th is
    process physique T is
       use ada.text_io;
 
    start
 
       loop
          choose
             settle for getToken (t : out integer) do
                t := d;
             finish getToken;
          or
             terminate;
          finish choose;
          d := d + 1;
       finish loop;
 
    finish ;
 finish t.th;

And most vital factor is your mission file.
It make you easier.

mission 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";
finish Testdll;

You may configure virtually all settings in your GPS IDE. However observe that GPS can not do Library_Interface setting accurately.
After configuring it, you need to test the mission file and modify it accordingly.
Library_Interface means which package deal incorporates export capabilities in DLL.

Library_Auto_Init is attention-grabbing and it’s essential to select an accurate setting in
your mission.

Should you select True, it can name adaInit when library load (which implies
elaboration seems in loading time mechanically)
if select False, it can export init and finalize capabilities in DLL and also you
should name them by your self.

Should you export capabilities like C/C++ “regular” capabilities with none elaboration
elements, chances are you’ll set library_auto_init to True.

In case your Ada code doesn’t solely work like C/C++ but in addition wants Ada elaboration
perform,
you need to initialize/finalize by your self.

“DANGEROUS” DANGEROUS” DANGEROUS”DANGEROUS”DANGEROUS”

For instance,
when you’ve got a library degree process needing elaboration in load time and use
implied linking and loading
it can “impasse WAITING” when DllMain calls init code.
It is sudden behaviour that you do not need to.

DANGEROUS”DANGEROUS”DANGEROUS”DANGEROUS”DANGEROUS”

In some case, chances are you’ll use LoadLibrary / GetProcAddress to work (Specific
loading). However it isn’t simple to handle your code, particularly if you happen to export many capabilities.

Step 2. Make a .lib file (for static linking)[edit]

You should use utilities dumpbin and lib to generate .lib file
(2.A)

    dumpbin/exports Te.dll > te.def,

the file content material is

Microsoft (R) COFF/PE Dumper Model 8.00.50727.42
Copyright (C) Microsoft Company.  All rights reserved.

Dump of file te.dll

File Kind: DLL
 Part incorporates the next exports for Te.dll
   00000000 traits
   44E2ABD2 time date stamp Wed Aug 16 13:23:30 2006
       0.00 model
          1 ordinal base
         19 variety of capabilities
         19 variety of names

   ordinal trace RVA      identify

         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 [email protected]

 Abstract

       1000 .bss
       1000 .information
       1000 .edata
       2000 .idata
       1000 .rdata
       1000 .reloc
      2E000 .stab
      E3000 .stabstr
       4000 .textual content

(2.b)
take away all in Te.def however go away …

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
         [email protected]

The Teinit and Tefinal is used to elaborate and terminate the Ada
elaboration half after Te.dll loaded.
(the identify doesn’t name adainit/adafinalize however nonetheless the identical perform as
adainit/adafinalize)

Step 3. Work collectively in MS VC++[edit]

Begin Visible Studio C++ mission and add Te.lib to your linker setting.

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

extern "C" extern void _stdcall check(int a,int b,int c,int d,int e);
extern "C" extern void  Teinit();
extern "C" extern void  Tefinal();

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

#embrace "stdafx.h" //visible c++ default header
#embrace "Te.h" //your ada library header
 
int _tmain(int argc, _TCHAR* argv[])
{
 
  Teinit();  //begin init
  for (int i = 0 ; i< 300 ;i++)
     check(0,0,0,0,i);
 
  Tefinal(); //finish  init
  return 0;
 
}

Step 4. Run your code[edit]

Put .exe and .dll in the identical listing and run the check

The loader will discover dll following on this order:

  1. present listing
  2. path variable
  3. Home windows listing

Put the dll and exe in the identical listing, it’s the most suitable choice.


Leave a Reply

Your email address will not be published. Required fields are marked *