comp.lang.ada
 help / color / mirror / Atom feed
* Thick Ada bindings to C Win32
@ 2004-11-10  2:41 Brian May
  2004-11-10  4:36 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 18+ messages in thread
From: Brian May @ 2004-11-10  2:41 UTC (permalink / raw)


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 <bam@snoopy.apana.org.au>



^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2004-11-29 14:34 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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               ` Thick Ada bindings to C Win32 Frank J. Lhota

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