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,7661856b1d8dc0ab X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news3.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!wns13feed!worldnet.att.net!attbi_s22.POSTED!53ab2750!not-for-mail From: "Jeffrey R. Carter" User-Agent: Thunderbird 1.5.0.9 (Windows/20061207) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Calling Ada from C References: <1172159208.098190.143360@t69g2000cwt.googlegroups.com> <1172580562.037493.251070@a75g2000cwd.googlegroups.com> In-Reply-To: <1172580562.037493.251070@a75g2000cwd.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <5a6Fh.19316$PD2.1313@attbi_s22> NNTP-Posting-Host: 12.201.97.213 X-Complaints-To: abuse@mchsi.com X-Trace: attbi_s22 1172631297 12.201.97.213 (Wed, 28 Feb 2007 02:54:57 GMT) NNTP-Posting-Date: Wed, 28 Feb 2007 02:54:57 GMT Organization: AT&T ASP.att.net Date: Wed, 28 Feb 2007 02:54:57 GMT Xref: g2news2.google.com comp.lang.ada:9589 Date: 2007-02-28T02:54:57+00:00 List-Id: hannibal.holm@gmail.com wrote: > > The data structure in C is just a pointer to a block of data to be > sent over a network link. This need to be converted to a byte array > defined as: > > Byte_Size : constant := 8; > > type Byte is range 0 .. 2**Byte_Size - 1; > for Byte'Size use Byte_Size; Anything you use to talk to C should be convention C. Since, from below, in C this seems to be char, you should use Interfaces.C.Char and Interfaces.C.Char_Array. Since Char_Array is convention C, Ada won't expect it to have bounds associated with it. > > type Byte_Array is array (Natural range <>) of Byte; > pragma Pack (Byte_Array); > > As said, I am not extremly familiar with Ada and come from a C- > background, so I have a few problems trying to figure out what the ada > runtime and type system actually does. I tried something like > > -- This proc is exported as with C-conventions > procedure Some_Ada_Proc(Msg : in Byte_Array; > Len : in Natural) is > Msg_Constr : Byte_Array := Msg(0 .. Length - 1); Again, you should use Interfaces.C.Int instead of Natural. Where does Length come from? > This is called with: > > char foo[] = {0xde, 0xad, 0xbe, 0xef}; > some_ada_proc(foo, sizeof(foo)); // sizeof static array = 4 This is where I get char from above. If using Char_Array still crashes, you can use a very large constrained subtype of Char_Array: subtype Many_Chars is Interfaces.C.Char_Array (Interfaces.C.Size_T); and slice the result with 0 .. Interfaces.C.Size_T (Len). This can then be converted into your array of Bytes. -- Jeff Carter "I would never want to belong to any club that would have someone like me for a member." Annie Hall 41