comp.lang.ada
 help / color / mirror / Atom feed
From: Jeffrey Carter <spam@spam.com>
Subject: Re: Thick Ada bindings to C Win32
Date: Sat, 13 Nov 2004 00:51:09 GMT
Date: 2004-11-13T00:51:09+00:00	[thread overview]
Message-ID: <1Ucld.25101$KJ6.2286@newsread1.news.pas.earthlink.net> (raw)
In-Reply-To: <sa4654ad4so.fsf@snoopy.apana.org.au>

Brian May wrote:
>>>>>>"Nick" == Nick Roberts <nick.roberts@acm.org> 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




  reply	other threads:[~2004-11-13  0:51 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-11-10  2:41 Thick Ada bindings to C Win32 Brian May
2004-11-10  4:36 ` tmoran
2004-11-10 19:31 ` Jeffrey Carter
2004-11-12  1:51   ` Brian May
2004-11-12 12:09 ` Nick Roberts
2004-11-12 17:57   ` tmoran
2004-11-12 18:50     ` Martin Krischik
2004-11-12 23:29   ` Brian May
2004-11-13  0:51     ` Jeffrey Carter [this message]
2004-11-25 23:19       ` Brian May
2004-11-26  9:50         ` Martin Krischik
2004-11-26 12:23           ` Frank J. Lhota
2004-11-26 19:19         ` Jeffrey Carter
2004-11-27 23:56           ` Brian May
2004-11-28 18:57             ` Jeffrey Carter
2004-11-28 23:45             ` Brian May
2004-11-29  7:50               ` Message-ID: <sa4vfbpzgfu.fsf@snoo tmoran
2004-11-29 14:34               ` Thick Ada bindings to C Win32 Frank J. Lhota
replies disabled

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