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-31 04:29:57 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!skynet.be!skynet.be!newsgate.cistron.nl!transit.news.xs4all.nl!rhinewceros.xs4all.nl!not-for-mail Newsgroups: comp.lang.ada Subject: Re: Importing C structs? From: Ching Bon Lam References: <1V1Wa.299$jp.55@newsread4.news.pas.earthlink.net> Organization: Universiteit Twente Message-ID: User-Agent: Xnews/5.04.25 Date: 31 Jul 2003 11:29:56 GMT NNTP-Posting-Host: 213.84.97.6 X-Trace: 1059650996 dreader7.news.xs4all.nl 26127 213.84.97.6:25866 Xref: archiver1.google.com comp.lang.ada:41072 Date: 2003-07-31T11:29:56+00:00 List-Id: "Matthew Heaney" wrote in news:1V1Wa.299$jp.55@newsread4.news.pas.earthlink.net: > > "Ching Bon Lam" wrote in > message news:Xns93C9C9EB630Fcblamstudentutwenten@194.109.133.20... >> >> that would be: >> >> type DB is interfaces.c.extensions.opaque_structure_def; > > This is probably overkill. For an opaque type all you need to do is: > > 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); overkill? i don't think so.. opaque_structure_def is defined in Interfaces.C.Extensions (i-cexten.ads in gnat) as: subtype opaque_structure_def is System.Address; just a memory address. That's the same as DB *. Since the api (well, most api written in C) only use DB * and not DB, you don't need another access type. that's why subtype DB is interfaces.c.extensions.opaque_structure_def; Note that it's actually subtype of opaque_structure_def. small error on my side. > > >> -- i have too less information about DBTYPE, so i assume >> -- it's just a struct >> type DBTYPE is interfaces.c.extensions.opaque_structure_def; >> >> function dbopen >> (file : interfaces.c.char_array; >> flags : interfaces.c.int; >> mode : interfaces.c.int; >> type : DBTYPE; >> openinfo : interfaces.c.extensions.void_ptr) >> return DB; >> >> pragma Import(C, dbopen, "dbopen'); > > The type of parameter file is incorrect. The char_array type is an > unconstrained Ada array type, which does not correspond directly to > any C type. Specifically, your problem is that on the Ada side a dope > vector describing the array object is passed -- this is definitely > *not* what you want. > > You need to use the type Interfaces.C.Strings.chars_ptr instead. const char * is in this context the same as a string, which is in fact an array of char. and interfaces.c.char_array is the same thing. and with chars_ptr you need to make an extra temp variable for the conversion. > > You can probably use System.Address for void*. The C type void* was > accidently omitted from Interfaces.C in the Ada95 standard, but I > assume that oversite will be corrected in a future version of the > language. again defined in interfaces.c.extensions (at least in gnat): subtype void_ptr is System.Address; > > It's not clear whether your return type should be DB or an explicit > access type (as in DB_Access), because I'm not familiar with the > package Interfaces.C.extensions. I would use DB_Access, which is > completely portable. i have used interfaces.c.extensions with success, that's why i'm using opaque_structure_def because then i don't have to use 'hacks'. > > The type DBTYPE is a bit of a mystery at this point, since structs > aren't typically passed by value in C. Perhaps DBTYPE is really a > pointer? > i guess :) CBL