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-Thread: a07f3367d7,fb17569fccf2604b X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!e4g2000prn.googlegroups.com!not-for-mail From: Jerry Newsgroups: comp.lang.ada Subject: Re: Trouble writing Ada callback from C Date: Wed, 16 Sep 2009 16:19:14 -0700 (PDT) Organization: http://groups.google.com Message-ID: <5a22a705-1fdb-4a70-950f-4d1a1060a17e@e4g2000prn.googlegroups.com> References: <241a6745-0848-4bd0-bb1b-1e826e084911@j19g2000yqk.googlegroups.com> NNTP-Posting-Host: 75.172.194.61 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1253143154 14820 127.0.0.1 (16 Sep 2009 23:19:14 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 16 Sep 2009 23:19:14 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: e4g2000prn.googlegroups.com; posting-host=75.172.194.61; posting-account=x5rpZwoAAABMN2XPwcebPWPkebpwQNJG User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_8; en-US) AppleWebKit/528.16+(KHTML, like Gecko, Safari/528.16) OmniWeb/v622.8.0,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:8355 Date: 2009-09-16T16:19:14-07:00 List-Id: On Sep 12, 11:40=A0am, Vadim Godunko wrote: > On Sep 11, 11:57=A0am, Jerry wrote: > > > I have a problem with writing an Ada procedure, used as a callback > > from C, in which one of the arguments is an output char * (in the C > > version). The actual length of the returned "string" isn't known at > > calling time, even though the caller apparently allocates 40 > > characters (see below). However, the actual number of characters > > returned by the C version is less than 40. > > > My problem is how to pass probably a char_array back out which is the > > correct length, but which length is not known at the time the Ada > > callback is called from C. > > subtype constrained_char_array is Interfaces.C.char_array (1 .. 40); > > procedure Y (X : out constrained_char_array); > pragma Convention (C, Y); Sorry for being slow to respond--I had to step away from this project for a few days. This works--thanks! After seeing this suggestion, I felt a little embarrassed because it's so simple, and in hindsight, obvious. Also, I realized that part of the problem was that to match the C results, I had to insert my string into the left of the allocated 41 characters, then place a null character, then fill the rest with spaces, like this: -- Declared elsewhere is this, with Label_String_Length set to 40: subtype Label_String_Type is Interfaces.C.char_array (0 .. size_t(Label_String_Length)); -- Then: function Unbounded_To_Weird_C (Item : Unbounded_String; C_Length : size_t) return Label_String_Type is begin return To_C(To_String(Item & ASCII.nul & (Label_String_Length - Length(Item)) * " "), False); end Unbounded_To_Weird_C; Thanks, everyone, for the help. Jerry