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,594935f0c2f19bc4 X-Google-Attributes: gid103376,public From: mheaney@ni.net (Matthew Heaney) Subject: Re: how to pass access string across pragma C interface? Date: 1997/06/24 Message-ID: #1/1 X-Deja-AN: 252412872 References: <866734587.8496@dejanews.com> Organization: Estormza Software Newsgroups: comp.lang.ada Date: 1997-06-24T00:00:00+00:00 List-Id: In article <866734587.8496@dejanews.com>, burch@cyberhighway.net wrote: >I'm using Ada83 with the Alsys compiler and I'm having problems getting >access strings to pass to C char* across the pragma interface call. What >kind of type definitions does one have to do on the Ada side to get the >types to match up? This is what I've done so far, which gives a >constraint error on run time. > type string_access_type is access string( 1 ..2048); > > function read_func ( > socket_fd : in integer; > buffer : in string_access_type > ) return integer; > pragma interface ( C , read_func ); > pragma interface_name( read_func, "read" ); > > . > . > . > begin > > > . > . > status_read := read_func ( socket_fd, buffer ); > >the C function "read" is > >int read (int, char*); > >What I'm trying to accomplish is to have "buffer" passed by reference to >C. C will go out and get the string from a socket, return, and >(hopefully) I'll be able to use it back on the Ada side. You don't need to use Ada access types. When transferring data across an external interface, use System.Address on a buffer allocated on the stack: declare function read (fd : Interfaces.C.int; Buffer : System.Address) return Interfaces.C.int; pragma Interface (C, read); pragma Interface_Name (read, "read"); The_Buffer : String (1 .. 2048); pragma Volatile (The_Buffer); -- ??? -- or perhaps we need to make it aliased ??? -- The_Buffer : aliased String (1 .. 2048); -- pragma Volatile (The_Buffer); The_Status : constant Interfaces.C.int := read (fd, The_Buffer (1)'Address); begin ... Please don't put the buffer on the heap; this is completely unnecessary. Perhaps "Robert and the Bobs" can provide info about whether pragma Volatile or aliased is necessary. You can make the interface even more bulletproof by using the string (character array) types declared in package Interfaces.C. But the code fragment above should work for you. Email me if you're having any more problems. Matt -------------------------------------------------------------------- Matthew Heaney Software Development Consultant (818) 985-1271