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=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,74bc23dcb20218db X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news4.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsfeed.freenet.ag!feeder.erje.net!news.doubleslash.org!open-news-network.org!news.teledata-fn.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: How to access this package written in C? Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <4e9d2aaf-c0a6-4798-b838-8f5b7c4a39d1@k33g2000yqc.googlegroups.com> Date: Wed, 21 Apr 2010 19:39:23 +0200 Message-ID: <1ccis7uwsta0r$.1qvwug3z3f1sk$.dlg@40tude.net> NNTP-Posting-Date: 21 Apr 2010 19:39:20 CEST NNTP-Posting-Host: ee09b0b1.newsspool4.arcor-online.net X-Trace: DXC=[W?D`GJb<_6lIh70@[f0 X-Complaints-To: usenet-abuse@arcor.de Xref: g2news2.google.com comp.lang.ada:11089 Date: 2010-04-21T19:39:20+02:00 List-Id: On Wed, 21 Apr 2010 09:43:02 -0700 (PDT), resander wrote: > I am working on software that manipulates SQL databases via a GUI. It > is written in C or C++ without classes and compiled on Linux. Would > also want it to run on Windows eventually. This software has > essentially one entry procedure: > > int guidb ( int appno , int recaddress ); // in C This is awful even on C standards. > which returns an outcome as an int, takes an int appno which specifies > application and an address of a record in recaddress. The latter is > used for passing the memory address of any recordtype variable and is > cast to a byte address inside guidb. [...] > Desired use in Ada (same as above): > > type PRODUCT is RECORD int prodcode; float price; char descr > [50]...END RECORD; > > procedure getproduct ( p : out PRODUCT ; result : out int ) is > begin > result = guidb ( 0 , address_of(p) how ??? ) ; > end > > prod : PRODUCT; > outcome : int ; [...] > How to do this in in Ada? If it cannot be done, can the interface > above be changed to make it possible? Make it a generic function: SQL_Error : exception; generic type Data_Type is private; function Generic_Get (No : Integer) return Data_Type; The implementation of: with Ada.Exceptions; use Ada.Exceptions; with Interfaces.C; use Interfaces.C; function Generic_Get (No : Integer) return Data_Type is function Internal (No : int; Data : access Data_Type) return int; pragma Import (C, Internal, "guilib"); Data : aliased Data_Type; Result : int; begin Result := Internal (int (No), Data'Access); if Result /= 0 then Raise_Exception (SQL_Error'Identity, "Error code:" & int'Image (Result)); else return Data; end if; end Generic_Get; Declare a type you need, e.g. Product: type Product is record Code : int; Price : float; Descr : char_array (1..50); end record; pragma Convention (C, Product); -- Note this pragma! Instantiate the function: function Get is new Generic_Get (Product); Use it as: X : Product := Get (0); -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de