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 From: Brian May Newsgroups: comp.lang.ada Subject: Re: Thick Ada bindings to C Win32 References: <2vjnfbF2lioiaU1@uni-berlin.de> Date: Sat, 13 Nov 2004 10:29:59 +1100 Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:m/aj2wQzeDgz8ruYORj922oTw2E= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: snoopy.microcomaustralia.com.au X-Trace: news.melbourne.pipenetworks.com 1100302181 202.173.153.89 (13 Nov 2004 09:29:41 +1000) X-Complaints-To: abuse@pipenetworks.com X-Abuse-Info: Please forward all headers to enable your complaint to be properly processed. Path: g2news1.google.com!news2.google.com!news.glorb.com!newsfeed-east.nntpserver.com!nntpserver.com!news1.optus.net.au!optus!news.mel.connect.com.au!news-south.connect.com.au!news-north.connect.com.au!news.alphalink.com.au!news.melbourne.pipenetworks.com!not-for-mail Xref: g2news1.google.com comp.lang.ada:6175 Date: 2004-11-13T10:29:59+11:00 List-Id: >>>>> "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. 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