comp.lang.ada
 help / color / mirror / Atom feed
* Re: import a C function returning char *
  1999-02-03  0:00 import a C function returning char * Stephen Leake
@ 1999-02-03  0:00 ` Tucker Taft
  1999-02-03  0:00 ` Samuel Tardieu
  1 sibling, 0 replies; 4+ messages in thread
From: Tucker Taft @ 1999-02-03  0:00 UTC (permalink / raw)
  To: Stephen Leake

Stephen Leake wrote:
> 
> I need to import the C function:
> 
> char *sbs_read_error (void);
> 
> I'd like to use the facilities in Interfaces.C, but since there is no
> char_ptr type, I'm not clear what the correct method is. LRM B.3 (63
> .. 75) talk about various parameter passing modes, but there's no
> discussion of function results.
> 
> Since all parameters of an array type are passed by pointers, I could
> assume function results are also passed by pointer, and try:
> 
> declare
> 
>    function Sbs_Read_Error return Interfaces.C.char_array;
>    pragma Import (C, Sbs_Read_Error, "sbs_read_error");
> 
>    Result : String (1 .. 30);
>    Result_Last : Natural;
> 
> begin
> 
>    Interfaces.C.To_Ada (Sbs_Read_Error, Result, Result_Last);
> 
>    Text_Io.Put_Line (Result (1 .. Result_Last));
> 
> end;
> 
> Is this going to work (I'll get around to compiling this soon)? I'm
> using GNAT 3.11p and ObjectAda 7.12 on Windows NT; what about other
> Ada compilers?

This should work on ObjectAda, I believe.

> -- Stephen Leake

-- 
-Tucker Taft   stt@averstar.com   http://www.averstar.com/~stt/
Technical Director, Distributed IT Solutions  (www.averstar.com/tools)
AverStar (formerly Intermetrics, Inc.)   Burlington, MA  USA




^ permalink raw reply	[flat|nested] 4+ messages in thread

* import a C function returning char *
@ 1999-02-03  0:00 Stephen Leake
  1999-02-03  0:00 ` Tucker Taft
  1999-02-03  0:00 ` Samuel Tardieu
  0 siblings, 2 replies; 4+ messages in thread
From: Stephen Leake @ 1999-02-03  0:00 UTC (permalink / raw)


I need to import the C function:

char *sbs_read_error (void);

I'd like to use the facilities in Interfaces.C, but since there is no
char_ptr type, I'm not clear what the correct method is. LRM B.3 (63
.. 75) talk about various parameter passing modes, but there's no
discussion of function results.

Since all parameters of an array type are passed by pointers, I could
assume function results are also passed by pointer, and try:

declare

   function Sbs_Read_Error return Interfaces.C.char_array;
   pragma Import (C, Sbs_Read_Error, "sbs_read_error");

   Result : String (1 .. 30);
   Result_Last : Natural;

begin

   Interfaces.C.To_Ada (Sbs_Read_Error, Result, Result_Last);

   Text_Io.Put_Line (Result (1 .. Result_Last));

end;

Is this going to work (I'll get around to compiling this soon)? I'm
using GNAT 3.11p and ObjectAda 7.12 on Windows NT; what about other
Ada compilers?

-- Stephen Leake




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: import a C function returning char *
  1999-02-03  0:00 import a C function returning char * Stephen Leake
  1999-02-03  0:00 ` Tucker Taft
@ 1999-02-03  0:00 ` Samuel Tardieu
  1999-02-04  0:00   ` Stephen Leake
  1 sibling, 1 reply; 4+ messages in thread
From: Samuel Tardieu @ 1999-02-03  0:00 UTC (permalink / raw)
  To: Stephen Leake

>>>>> "Stephen" == Stephen Leake <Stephen.Leake@gsfc.nasa.gov> writes:

Stephen> Is this going to work (I'll get around to compiling this
Stephen> soon)? I'm using GNAT 3.11p and ObjectAda 7.12 on Windows NT;
Stephen> what about other Ada compilers?

What do you think "Sbs_Read_Error'Length" would contain? You can't tell?
Neither can the compiler :-)

Use Interfaces.C.Strings.chars_ptr, since what you get from this C
function is only a pointer onto the beginning of the string (char *),
don't expect to get more just by writing it in Ada :-)

  Sam
-- 
Samuel Tardieu -- sam@ada.eu.org




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: import a C function returning char *
  1999-02-03  0:00 ` Samuel Tardieu
@ 1999-02-04  0:00   ` Stephen Leake
  0 siblings, 0 replies; 4+ messages in thread
From: Stephen Leake @ 1999-02-04  0:00 UTC (permalink / raw)


Samuel Tardieu <sam@ada.eu.org> writes:

> >>>>> "Stephen" == Stephen Leake <Stephen.Leake@gsfc.nasa.gov> writes:
> 
> Stephen> Is this going to work (I'll get around to compiling this
> Stephen> soon)? I'm using GNAT 3.11p and ObjectAda 7.12 on Windows NT;
> Stephen> what about other Ada compilers?
> 
> What do you think "Sbs_Read_Error'Length" would contain? You can't tell?
> Neither can the compiler :-)

Yes, this was why I was wondering if it would work.

> Use Interfaces.C.Strings.chars_ptr, since what you get from this C
> function is only a pointer onto the beginning of the string (char *),
> don't expect to get more just by writing it in Ada :-)

Yes, that's the solution. I thought I remembered a chars_ptr
somewhere, and I'd have found it if I'd just looked in the LRM index.
I guess I was just having a bad day ...

-- Stephe




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1999-02-04  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-03  0:00 import a C function returning char * Stephen Leake
1999-02-03  0:00 ` Tucker Taft
1999-02-03  0:00 ` Samuel Tardieu
1999-02-04  0:00   ` Stephen Leake

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