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-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,d71460587da14d5b X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-07-30 22:21:59 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread4.news.pas.earthlink.net.POSTED!not-for-mail From: "Matthew Heaney" Newsgroups: comp.lang.ada References: Subject: Re: Importing C structs? X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 Message-ID: Date: Thu, 31 Jul 2003 05:21:59 GMT NNTP-Posting-Host: 65.110.133.134 X-Complaints-To: abuse@earthlink.net X-Trace: newsread4.news.pas.earthlink.net 1059628919 65.110.133.134 (Wed, 30 Jul 2003 22:21:59 PDT) NNTP-Posting-Date: Wed, 30 Jul 2003 22:21:59 PDT Organization: EarthLink Inc. -- http://www.EarthLink.net Xref: archiver1.google.com comp.lang.ada:41055 Date: 2003-07-31T05:21:59+00:00 List-Id: "Freejack" wrote in message news:pan.2003.07.30.20.09.17.183172.1022@nospam.net... > > However, I'm fuzzy on importing C structs. In a nutshell...here is the > function I'm attempting to import... > > DB * > dbopen(const char *file, int flags, int mode, DBTYPE type, > const void *openinfo); type DB_Type is limited null record; type DB_Access is access all DB_Type; for DB_Access'Storage_Size use 0; pragma Convention (C, DB_Access); function dbopen (file : Interfaces.C.chars_ptr; flags : Interfaces.C.int; mode : Interfaces.C.int; dbtype : dbtype_type; --??? openinfo : System.Address) return DB_Access; pragma Convention (C, dbopen); pragma Import (C, dbopen, "dbopen"); > Where "int mode" corresponds to the C "open" declaration and DBTYPE is a > C struct. I'm using the dbopen man page for the Sleepycat Berkely DB. I don't have the man pages available to me. What exactly is DBTYPE? Is this a struct? Is is really passed by value? > Now I'm using > > pragma Import( C, dbopen ); > > And then casting everything as either a C.integer or a character pointer. > It compiles this way, but it sucks as a way to do it. Your pragma is OK, but I don't know why you're casting. You can probably substitute type Standard.Integer for C.int, but this isn't guaranteed to work. (It does on GNAT, because GNAT guarantees that the Ada representation matches the C representation.) The example above is a thin binding. You can always wrap that in higher-level ("thick") call, inlined as desired. > Specifically, would I cast DBTYPE as a seperate record type, and then > provide a seperate function to translate each item into the type expected > by the C struct? Or can import the DBTYPE struct whole hog into my Ada > specification? Import the struct. But do post what the C struct looks like.