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: 103376,9c274893e2f7e55 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread3.news.pas.earthlink.net.POSTED!a6202946!not-for-mail From: Jeffrey Carter Organization: jrcarter commercial-at acm [period | full stop] org User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.7.3) Gecko/20040910 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Ada to C Interface: Passing Pointers Via System.Address References: <8e19522cfe30bb2ccbfe1393fdd6b235@localhost.talkaboutprogramming.com> In-Reply-To: <8e19522cfe30bb2ccbfe1393fdd6b235@localhost.talkaboutprogramming.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <%5kFd.5982$Ii4.2722@newsread3.news.pas.earthlink.net> Date: Thu, 13 Jan 2005 01:25:15 GMT NNTP-Posting-Host: 63.190.72.169 X-Complaints-To: abuse@earthlink.net X-Trace: newsread3.news.pas.earthlink.net 1105579515 63.190.72.169 (Wed, 12 Jan 2005 17:25:15 PST) NNTP-Posting-Date: Wed, 12 Jan 2005 17:25:15 PST Xref: g2news1.google.com comp.lang.ada:7704 Date: 2005-01-13T01:25:15+00:00 List-Id: 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