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,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit From: Brian May Newsgroups: comp.lang.ada Subject: Thick Ada bindings to C Win32 Date: Wed, 10 Nov 2004 13:41:57 +1100 Message-ID: User-Agent: Gnus/5.1006 (Gnus v5.10.6) Emacs/21.3 (gnu/linux) Cancel-Lock: sha1:TGnjzOKE+rtPhwi6jt8GZ20JCw4= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: dsl-202-173-153-89.vic.westnet.com.au X-Trace: news.melbourne.pipenetworks.com 1100054498 202.173.153.89 (10 Nov 2004 12:41:38 +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.maxwell.syr.edu!news-north.connect.com.au!duster.adelaide.on.net!news.melbourne.pipenetworks.com!not-for-mail Xref: g2news1.google.com comp.lang.ada:6098 Date: 2004-11-10T13:41:57+11:00 List-Id: Hello, I have a program that uses Win32 serial I/O. Initially I found a set of thick bindings to do this on the Internet, but I found a number of limitations, and have been working to enhance it. One issue that still remains though. I have the following functions: type Byte_Array is array (Positive range <>) of Byte; procedure Get(Handle : in Handle_Type; Item : out Byte; Done : out Boolean); procedure Put(Handle : Handle_Type; Item : in Byte_Array); (hmmm... ideally the Get function should read in a Byte_Array, but lets not confuse matters... My application only wants one byte at a time anyway) However, the equivalent Win32 functions require a Win32.Lpcvoid (null terminator not required). How do I convert to/from the required types? At the moment, my somewhat not-elegant code, found via trial-and-error, is: function Convert_Char_Ptr_To_Lpvoid is new Ada.Unchecked_Conversion (Source => Char_Ptr, Target => Win32.Lpvoid); function Convert_Byte_Array_Ptr_To_Char_Array_Ptr is new Ada.Unchecked_Conversion (Source => Byte_Array_Access, Target => Interfaces.C.Strings.Char_Array_Access); function Convert_Chars_Ptr_To_Lpcvoid is new Ada.Unchecked_Conversion (Source => Interfaces.C.Strings.Chars_Ptr, Target => Win32.Lpcvoid); procedure Get(Handle : in Handle_Type; Item : out Byte; Done : out Boolean) is ... char_item : character; Bool_Result : Win32.Bool; NumberOfBytesRead : aliased Win32.dword; Char_Buffer : aliased Interfaces.C.Char; ... begin ... Result := Win32.Winbase.ReadFile( ... lpBuffer => Convert_Char_Ptr_To_Lpvoid(Char_Buffer'Unchecked_Access), nNumberOfBytesToRead => Win32.Dword(1), ....); ... char_item := Interfaces.C.To_Ada(Char_Buffer); Item := char_to_byte(char_item); ... end Get; procedure Put(Handle : Handle_Type; Item : in Byte_Array) is ... Buffer : aliased Byte_Array := Item; A : Interfaces.C.Strings.Char_Array_Access := Convert_Byte_Array_Ptr_To_Char_Array_Ptr(Buffer'Unchecked_Access); B : Interfaces.C.Strings.Chars_Ptr := Interfaces.C.Strings.To_Chars_Ptr(A,False); Buffer_Ptr : Win32.Lpcvoid := Convert_Chars_Ptr_To_Lpcvoid(B); begin Bool_Result := Win32.Winbase.WriteFile ( ... lpBuffer => Buffer_Ptr, -- Win32.Lpcvoid nNumberOfBytesToWrite => Win32.Dword(Item'Length), -- Win32.Dword => 1 bytes ... ) end Put; While the get function is OK, I think the put function is horrible. It also requires copying the data, ideally I don't want this as I want to maximize efficiency, even for large transactions (not currently a strict requirement though). Is there anyway this could be improved? Thanks in advance for any suggestions. Alternatively, if somebody knows of a good quality set of thick bindings that I could use, preferably GPL (or compatible), then please let me know. Thanks. -- Brian May