comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Ada to C Interface: Passing Pointers Via System.Address
Date: Thu, 13 Jan 2005 01:25:15 GMT
Date: 2005-01-13T01:25:15+00:00	[thread overview]
Message-ID: <%5kFd.5982$Ii4.2722@newsread3.news.pas.earthlink.net> (raw)
In-Reply-To: <8e19522cfe30bb2ccbfe1393fdd6b235@localhost.talkaboutprogramming.com>

ReversedBias wrote:

> I am relatively new to Ada.  I am constructing an API so that my Ada 
> clients can talk to my C code.  My C functions return pointers to
> data structures (very large structures).  I need to then pass the
> pointer to the data struct to the Ada code and back into other C
> functions.  There is no interaction with the data structure in the
> Ada code except to pass the pointer from one C function to another.

This should be very easy. Define a convention-C access type and use it 
for all your C pointers:

type C_Ptr is access all Integer;
pragma Convention (C, C_Ptr);

function Get_Ptr return C_Ptr;
pragma Import (C, Get_Ptr, "c_func_name");

function Use_Ptr (Ptr : C_Ptr) return Interfaces.C.Int;
pragma Import (C, Use_Ptr, "another_c_name");

Result : Interfaces.C.Int;

Result := Use_Ptr (Get_Ptr);

if Result /= 0 then
    ...
end if;

> I have been trying to use the C interface pragma and return the
> pointers from C into an Ada System.Address and then pass that
> System.Address into the another pragma back down to the C functions.
> My hope is that the C will be able to type cast the address back into
> the correct pointer type.  So far I have not been able to get this to
> work properly.  Please advise on another approach or post an example
> of how I could do this using the System.Address

There's no guarantee that an Ada System.Address has anything in common 
with a C pointer. On some platform/compiler combinations they're the 
same, but on others they're not. A convention-C access type should work 
everywhere. (Caveat: convention-C, in Ada, means a specific C compiler. 
For example, for GNAT, it means gcc, and may not correspond to the 
conventions used by MS C. If you're using another compiler, it's up to 
you to make sure things correspond.)

-- 
Jeff Carter
"That was the most fun I've ever had without laughing."
Annie Hall
43



  parent reply	other threads:[~2005-01-13  1:25 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-01-12  2:16 Ada to C Interface: Passing Pointers Via System.Address ReversedBias
2005-01-12  3:09 ` tmoran
2005-01-12  7:36   ` ReversedBias
2005-01-12  8:38     ` tmoran
2005-01-12 15:50       ` Mark Lorenzen
2005-01-13 17:14   ` Martin Krischik
2005-01-13  1:25 ` Jeffrey Carter [this message]
2005-01-13  3:05 ` Steve
replies disabled

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