comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Interfacing with C
Date: Mon, 18 May 2009 05:11:27 -0700 (PDT)
Date: 2009-05-18T05:11:27-07:00	[thread overview]
Message-ID: <2120d50c-55a9-40b6-8637-d9dbec3f97e0@z5g2000vba.googlegroups.com> (raw)
In-Reply-To: 6i3cwvzndx7y.1j38k2uakyqyp$.dlg@40tude.net

On May 18, 1:58 pm, "Dmitry A. Kazakov" <mail...@dmitry-kazakov.de>
wrote:
> On Mon, 18 May 2009 04:29:44 -0700 (PDT), cedric wrote:
> > I would like to connect a C interface to another software package with
> > my Ada programs.
>
> > The h-file of the api contains the following pre-processor commands:
>
> > #ifdef SPSSXD_EXPORTS
> > # define SPSSXD_API __declspec(dllexport)
> > #else
> > # if MS_WINDOWS
> > #   define SPSSXD_API __declspec(dllimport)
> > # else
> > #   define SPSSXD_API
> > # endif
> > #endif
>
> From this you should figure out the call convention. It can be either C or
> stdcall. The rest can be usually ignored. Usually this stuff tells whether
> the entry point is a reference or else a vector for dynamic run-time
> linking or else an entry point, when the library itself is compiled,
> nothing interesting for an Ada client.
>
> > Some functions headers are
>
> > SPSSXD_API bool IsBackendReady();
> > SPSSXD_API int SetUpFromSPSS(SpssAdapter* anAdapter, SpssXDSmb*
> > anSmb);
>
> > What do I have to do to get a connection between the C functions and
> > my Ada program with regard to the commands of the pre-processor?
>
> Print the preprocessor output if you are in doubts. Otherwise it could be
> like this:
>
>    with Interfaces.C;  use Interfaces.C;
>    ...
>    function IsBackendReady return Int; -- C does not have Boolean
>    pragma Import (stdcall, IsBackendReady, "IsBackendReady");
>
>    type SpssAdapter is record
>        ... -- Components of struct SpssAdapter
>    end record;
>    pragma Convention (C, SpssAdapter);
>
>    type anSmb is record
>        ... -- Components of struct anSmb
>    end record;
>    pragma Convention (C, anSmb);
>
>    function SetUpFromSPSS
>       (  anAdapter : access SpssAdapter;
>           anSmb : access SpssXDSmb
>       );
>    pragma Import (stdcall, SetUpFromSPSS, "SetUpFromSPSS");
>
> --
> Regards,
> Dmitry A. Kazakovhttp://www.dmitry-kazakov.de

Funny, I wrote the bindings for these two particular functions
yesterday afternoon at the Ada-Belgium general assembly for one of our
attendees. The types SpssAdapter and SpssXDSmb are opaque in the
specification (they are declared as:

class SpssAdapter;
class SpssXDSmb;

and the only function using them takes pointers to them). My solution
was something like:

package SPSS is
   type Adapter is null record;
   type Adapter_Access is access all Adapter;
   pragma Convention (C, Adapter_Access);

   type XD_SMB is null record;
   type XD_SMB_Access is access all XD_SMB;
   pragma Convention (C, XD_SMB_Access);

   function Set_Up_From_SPSS
     (An_Adapter : in Adapter; An_SMB : in XD_SMB_Access)
     return Interfaces.C.int;
   pragma Import (Convention => C, Entity => Set_Up_From_SPSS,
     Linker_Name => "SetUpFromSPSS");
end SPSS;

This solution avoids the need to define an Ada type equivalent to, and
with the same representation as, the C type, and it respects the
opaqueness of the types in the C API.

--
Ludovic Brenta.



  reply	other threads:[~2009-05-18 12:11 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-18 11:29 Interfacing with C cedric
2009-05-18 11:51 ` Maciej Sobczak
2009-05-18 11:58 ` Dmitry A. Kazakov
2009-05-18 12:11   ` Ludovic Brenta [this message]
2009-05-18 12:13     ` Ludovic Brenta
2009-06-01  8:24   ` David Thompson
     [not found] <vhiln6cllz7.fsf@grotte.ifi.uio.no>
1999-12-30  0:00 ` Simon Wright
1999-12-31  0:00 ` Jerry van Dijk
1999-12-31  0:00   ` Simon Wright
     [not found] <7rtlav$bm7$1@nnrp1.deja.com>
     [not found] ` <37e2b0e7.0@news.pacifier.com>
1999-09-22  0:00   ` Richard D Riehle
1999-09-22  0:00     ` Ted Dennison
1999-09-22  0:00       ` David Botton
1999-09-23  0:00     ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox