From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,8f0fbe01a5c0ce8c X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!z5g2000vba.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: Interfacing with C Date: Mon, 18 May 2009 05:11:27 -0700 (PDT) Organization: http://groups.google.com Message-ID: <2120d50c-55a9-40b6-8637-d9dbec3f97e0@z5g2000vba.googlegroups.com> References: <250d9b93-13bf-4375-8b35-a1b0a7f74d99@j12g2000vbl.googlegroups.com> <6i3cwvzndx7y.1j38k2uakyqyp$.dlg@40tude.net> NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1242648687 21710 127.0.0.1 (18 May 2009 12:11:27 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 18 May 2009 12:11:27 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: z5g2000vba.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:5897 Date: 2009-05-18T05:11:27-07:00 List-Id: On May 18, 1:58=A0pm, "Dmitry A. Kazakov" 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 > > # =A0 define SPSSXD_API __declspec(dllimport) > > # else > > # =A0 define SPSSXD_API > > # endif > > #endif > > From this you should figure out the call convention. It can be either C o= r > stdcall. The rest can be usually ignored. Usually this stuff tells whethe= r > 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: > > =A0 =A0with Interfaces.C; =A0use Interfaces.C; > =A0 =A0... > =A0 =A0function IsBackendReady return Int; -- C does not have Boolean > =A0 =A0pragma Import (stdcall, IsBackendReady, "IsBackendReady"); > > =A0 =A0type SpssAdapter is record > =A0 =A0 =A0 =A0... -- Components of struct SpssAdapter > =A0 =A0end record; > =A0 =A0pragma Convention (C, SpssAdapter); > > =A0 =A0type anSmb is record > =A0 =A0 =A0 =A0... -- Components of struct anSmb > =A0 =A0end record; > =A0 =A0pragma Convention (C, anSmb); > > =A0 =A0function SetUpFromSPSS > =A0 =A0 =A0 ( =A0anAdapter : access SpssAdapter; > =A0 =A0 =A0 =A0 =A0 anSmb : access SpssXDSmb > =A0 =A0 =A0 ); > =A0 =A0pragma 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 =3D> C, Entity =3D> Set_Up_From_SPSS, Linker_Name =3D> "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.