comp.lang.ada
 help / color / mirror / Atom feed
From: Brian May <bam@snoopy.apana.org.au>
Subject: Re: Thick Ada bindings to C Win32
Date: Sat, 13 Nov 2004 10:29:59 +1100
Date: 2004-11-13T10:29:59+11:00	[thread overview]
Message-ID: <sa4654ad4so.fsf@snoopy.apana.org.au> (raw)
In-Reply-To: 2vjnfbF2lioiaU1@uni-berlin.de

>>>>> "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.

It compiles OK, but it is dodgy.

As an example, I rewrote my get routine to use this function:

--- cut ---
procedure Get(Handle : in Handle_Type; Item : out Byte; Done : out Boolean) is

   Port_Handle : constant Win32.Winnt.Handle := Win32.Winnt.Handle(Handle);

   Item_Buffer : Byte_Array(1..1);
   Bool_Result : Win32.Bool;
   NumberOfBytesRead : aliased Win32.dword;

   nulla       : System.Address := Null_Address;

begin
   if Hack then M.Acquire; end if;

--Read in a byte of information
   Bool_Result :=
      Win32.Winbase.ReadFile
      (hFile => Port_Handle,                                -- Win32.Winnt.Handl
e
       lpBuffer => Addr(Item_Buffer),                       -- Win32.Lpvoid
       nNumberOfBytesToRead => 1,                           -- Win32.Dword
       lpNumberOfBytesRead => NumberOfBytesRead'Unchecked_Access,
                                                            -- Win32.Lpdword
       lpOverlapped => Convert_Pvoid_To_Lpoverlapped(nulla));-- Lpoverlapped

   if Bool_Result = Win32.False then
      Ada.Exceptions.Raise_Exception(Operation_Failed'Identity,
         "ReadFile returned False");
   end if;

   if NumberOfBytesRead = 0 then
       Item := 0;
       Done := False;
   else
--This sets the exportable character type from the character buffer
--*****NOTE: Need to change this to a 'byte' type*****
       Item := Item_Buffer(1);
       Done := True;
   end if;
   if Hack then M.Release; end if;

end Get;
--- cut ---

This, amazingly, seems to work fine. However, gnat warns me that
item_buffer is never assigned a value. So I changed the code to
initialise it:

   Item_Buffer : Byte_Array(1..1) := ( 1 => 0 );

Now all values read by the get function are 0. If I change the 0 to 1
all values are read as 1.

Just to make sure, I changed the code back again (removed the
initialisation) and it started working.

To me this doesn't make any sense.
-- 
Brian May <bam@snoopy.apana.org.au>



  parent reply	other threads:[~2004-11-12 23:29 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 [this message]
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               ` 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