comp.lang.ada
 help / color / mirror / Atom feed
From: "Frank J. Lhota" <NOSPAM.Lhota.adarose@verizon.net>
Subject: Re: Thick Ada bindings to C Win32
Date: Mon, 29 Nov 2004 14:34:48 GMT
Date: 2004-11-29T14:34:48+00:00	[thread overview]
Message-ID: <csGqd.1854$_C2.1714@trndny01> (raw)
In-Reply-To: sa4vfbpzgfu.fsf@snoopy.apana.org.au

"Brian May" <bam@snoopy.apana.org.au> wrote in message 
news:sa4vfbpzgfu.fsf@snoopy.apana.org.au...
>>>>>> "Brian" == Brian May <bam@snoopy.apana.org.au> writes:
>
>    Brian> I seem to remember that I encountered problems doing things
>    Brian> as simply as you suggest, but I can't remember why nor can
>    Brian> I imagine why - I will try it again and report back here
>    Brian> with details of any errors encountered.
>
> Unfortunately, I was right, I did have problems:
>
>     type Byte_Array_Access is access all Byte_Array;
>     pragma Convention (C,Byte_Array_Access);
>
> produces:
>
> serial.adb:107:11: warning: this access type does not correspond to C 
> pointer
>
> and:
>
>   function To_LPCVOID is new
>      Ada.Unchecked_Conversion (Byte_Array_Access, Win32.LPCVOID);
>
> produces:
>
> serial.adb:144:04: warning: types for unchecked conversion have different 
> sizes
>
> So I tried:
>
>   Type Char_Array_Ptr is access all Interfaces.c.Char_Array;
>   pragma Convention (C,Char_Array_Ptr);
>
> which produces:
>
> serial.adb:89:09: warning: this access type does not correspond to C 
> pointer

The reason for this is that Ada needs to keep track of the array bounds of 
an Byte_Array object. In the case of Byte_Array_Access, the GNAT compiler 
choose the "fat pointer" approach of including the array bounds as part of 
the access type. In other words, a Byte_Array_Access object contains a 
pointer to the bytes, and additional components specifying 'First and 'Last. 
This is why the pragma Convention stuff failed.

You could fix this problem in one of two ways. One way is that use a 
representation clause to specify the size of a Byte_Array_Access object to 
be the size of a pointer, e.g.

    for Byte_Array_Access'Size use 32;

This forces GNAT to not include array bounds in a Byte_Array_Access object.

A cleaner and more portable approach would be to apply the 'Access attribute 
to the first element of the array. We can do this using the following 
definitions:

    type Byte_Array is array (Positive range <>) of aliased Byte;    -- Note 
the addition of "aliased"
    type Byte_Array_Access is access all Byte;
    pragma Convention (C,Byte_Array_Access);

By declaring the components of Byte_Array to be aliased, we guarantee that 
Byte_Array objects will never be packed in such a way that the components 
are not individually addressable, and hence we are free to apply 'Access or 
'Unchecked_Access to the components of a Byte_Array object. Then we define 
the Byte_Array_Access as a pointer to one of the components.

Now assume that we have the object

    Data : Byte_Array( 1 .. Data_Length );

and that we want to pass it to the subprogram

    function DoSomethingInC (
        lpBytes : in Byte_Array_Access
        ) return Interfaces.C.Int;
    pragma Import (C, DoSomethingInC );

then we could do this by applying 'Unchecked_Access to the first component 
of the array, as follows:

    Result := DoSomethingInC( lpBytes => 
Data(Data'First)'Unchecked_Access );

Hope this helps.








      parent reply	other threads:[~2004-11-29 14:34 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
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               ` Frank J. Lhota [this message]
replies disabled

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