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=unavailable autolearn_force=no version=3.4.4 Path: buffer2.nntp.dca1.giganews.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!feeder.erje.net!1.eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Returning a string from C Date: Wed, 22 Jul 2015 21:48:23 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <27cd219d-fff7-4c2a-90c7-ec10a917b4cb@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 7bit Injection-Date: Thu, 23 Jul 2015 04:46:55 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="a3855fbfe1a666be9aefba0563039ed5"; logging-data="7962"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19X9gYl7Zv2dtwYGWAgAPz3Itk47JE5ewM=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.1.0 In-Reply-To: <27cd219d-fff7-4c2a-90c7-ec10a917b4cb@googlegroups.com> Cancel-Lock: sha1:A9POShaeKIicng9v4TvyeXSatBw= X-Enigmail-Draft-Status: N1110 Xref: number.nntp.giganews.com comp.lang.ada:194306 Date: 2015-07-22T21:48:23-07:00 List-Id: On 07/22/2015 07:20 PM, NiGHTS wrote: > > Say I have the following C function: > > void Return_Hello (char *inout_Test_String ) { > strcpy( inout_Test_String, "Hello World" ); > } > > This function will use the string memory generated on the Ada side and return a simple string "Hello World". Here is how I would interface with it: > > procedure Return_Hello ( Test_String : in out chars_ptr ); > pragma Import (C, Return_Hello, "Return_Hello"); > > And this is how I would use it. > > use Interfaces.C; > use Interfaces.C.Strings; > > declare > > Test_String : String (1 .. 100); > Test_String_Char : chars_ptr := New_String (Test_String); > > begin > > Return_Hello ( Test_String_Char ); > Test_String := Value ( Test_String_Char ) ; > Ada.Text_IO.Put_Line ( Test_String ); > Free (Test_String_Char); > > end; > > When I run the program I get a runtime error "length check failed" on the line "Test_String := Value ( Test_String_Char );". > > What am I doing wrong here? Well, for one thing, you're thinking like a C person and using unnecessary pointers. I'd import the procedure as procedure Return_Hello (Test_String : out Char_Array); and call it with Test_String : Char_Array (1 .. 100); Return_Hello (Test_String => Test_String); Ada.Text_IO.Put_Line (Item => To_Ada (Test_String) ); As for the exception you get, Value returns the string pointed to by Item up to but not including the terminating NUL, which is 11 characters. Test_String is 100 characters, so the lengths don't match. -- Jeff Carter "Sir Robin the not-quite-so-brave-as-Sir-Lancelot, who had nearly fought the Dragon of Angnor, who nearly stood up to the vicious Chicken of Bristol, and who had personally wet himself at the Battle of Badon Hill." Monty Python & the Holy Grail 68