comp.lang.ada
 help / color / mirror / Atom feed
From: NiGHTS <nights@unku.us>
Subject: Returning a string from C
Date: Wed, 22 Jul 2015 19:20:34 -0700 (PDT)
Date: 2015-07-22T19:20:34-07:00	[thread overview]
Message-ID: <27cd219d-fff7-4c2a-90c7-ec10a917b4cb@googlegroups.com> (raw)

I am having some difficulty with returning a string from a C function.

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?

             reply	other threads:[~2015-07-23  2:20 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-23  2:20 NiGHTS [this message]
2015-07-23  4:48 ` Returning a string from C Jeffrey R. Carter
2015-07-23  4:54 ` Per Sandberg
2015-07-23  4:58 ` Laurent
2015-07-23  6:41 ` Pascal Obry
2015-07-23  7:17 ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox