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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8da8963fe574ea15 X-Google-Attributes: gid103376,public From: Keith Thompson Subject: Re: Newbie question about interface to C Date: 2000/05/30 Message-ID: #1/1 X-Deja-AN: 629298139 Sender: kst@king.cts.com References: X-Trace: thoth.cts.com 959744315 66299 205.163.0.22 (31 May 2000 03:38:35 GMT) Organization: CTS Network Services Newsgroups: comp.lang.ada X-Complaints-To: abuse@cts.com Date: 2000-05-30T00:00:00+00:00 List-Id: David Gressett writes: > I need to call C routines which are prototyped like these examples: > > shapelessblob *somefunction(char *s); /* creates a shaplessblob */ > > int otherfunction(shapelessblob *b); /* uses a shaplessblob */ > > where shaplessblob is a pointer to a structure that is created by > somefunction. The calling Ada code will never look at the insides of a > shapelessblob; It will only pass it to a C routine. > > How do I import such a pointer as an Ada access type? > > The Ada LRM that comes with gnat3.12p is not very helpful. (it is, > after all, a RM, not a tutorial.) The Cohen book has only a single > trivial example of a C interface. I'd probably do something like this: with Interfaces.C; with Interfaces.C.Strings; package Foo is type Shapeless_Blob is private; type Shapeless_Blob_Pointer is access Shapeless_Blob; function Some_Function (S: Interfaces.C.Strings.chars_ptr) return Shapeless_Blob_Pointer pragma Import(C, Some_Function, "somefunction"); function Other_Function (B: Shapeless_Blob_Pointer) return Interfaces.C.Int; pragma Import(C, Other_Function, "otherfunction"); private type Shapeless_Blob is record Dummy: Interfaces.C.int; -- C doesn't allow empty structs end record; pragma Convention(C, Shapeless_Blob); pragma Convention(C, Shapeless_Blob_Pointer); end Foo; This takes advantage of the fact that all struct pointers in C are represented the same way (something that's subtly implied by the C standard). You could probably get away with declaring type Shapeless_Blob_Pointer is new System.Address; and omitting the declaration of Shapeless_Blob, but it's potentially less portable; some C implementations might represent, say, struct pointers and char pointers differently. -- Keith Thompson (The_Other_Keith) kst@cts.com San Diego Supercomputer Center <*> Welcome to the last year of the 20th century.