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,47957cb8c33729e5 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news2.google.com!newsfeed.stanford.edu!headwall.stanford.edu!newshub.sdsu.edu!elnk-nf2-pas!newsfeed.earthlink.net!stamper.news.pas.earthlink.net!newsread1.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.4) Gecko/20030624 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Thick Ada bindings to C Win32 References: <2vjnfbF2lioiaU1@uni-berlin.de> In-Reply-To: Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <1Ucld.25101$KJ6.2286@newsread1.news.pas.earthlink.net> Date: Sat, 13 Nov 2004 00:51:09 GMT NNTP-Posting-Host: 63.184.104.206 X-Complaints-To: abuse@earthlink.net X-Trace: newsread1.news.pas.earthlink.net 1100307069 63.184.104.206 (Fri, 12 Nov 2004 16:51:09 PST) NNTP-Posting-Date: Fri, 12 Nov 2004 16:51:09 PST Xref: g2news1.google.com comp.lang.ada:6183 Date: 2004-11-13T00:51:09+00:00 List-Id: Brian May wrote: >>>>>>"Nick" == Nick Roberts writes: > > > Nick> Try: > Nick> function Addr (S : BYTE_Array) return LPCVOID is > > Nick> function To_LPCVOID is new > Nick> Ada.Unchecked_Conversion (System.Address, LPCVOID); > > Nick> begin > Nick> return To_LPCVOID( S(S'First)'Address ); > Nick> end; > > Is that legal? I would have thought it would return the address of the > local variable S, which might be different to what is passed as the > parameter. If S is passed by copy, you'll get garbage. If it's passed by reference, you may still get garbage, since there's no guarantee that an address has the same representation as a convention-C access value. Since Byte_Array is not a by-copy nor a by-reference type, the compiler is free to choose. GNAT seems to have chosen by reference, not surprising for an unconstrained array type, and does use the same representation. But be aware that you are relying on implementation dependencies here. Rational, for example, does not use the same representation for addresses and access values. (I've been around code that was being ported from GNAT to rational that did this, and it didn't smell nice.) > It compiles OK, but it is dodgy. Yes, and not portable, either. Assuming that Lp[c?]void is a convention-C access type, you can safely convert between convention-C access types using Unchecked_Conversion. Your best bet may be to declare such an access type designating a Byte_Array (maybe you want Storage_Array here?), declare Item_Buffer aliased, store its 'Unchecked_Access in a variable of the appropriate type, and convert it to the Win32 access type. You might also want to declare Item_Buffer volatile, so the compiler thinks its contents might spontaneously change. -- Jeff Carter "Monsieur Arthur King, who has the brain of a duck, you know." Monty Python & the Holy Grail 09