* C Interface
@ 2002-04-03 11:05 Henrik Quintel
2002-04-03 13:40 ` Mário Amado Alves
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Henrik Quintel @ 2002-04-03 11:05 UTC (permalink / raw)
Dear all,
I have a large C program and want to use the C Bindings win32ada for the
GNAT 3.13p.
I have insatlled GNAT and the Bindings in a correct way. In the standard I
have found the
following example about the C function strcpy:
-- Example of using the Interfaces.C package:
-- Calling the C Library Function strcpy
with Interfaces.C;
procedure Test is
package C renames Interfaces.C;
use type C.char_array;
-- Call <string.h>strcpy:
-- C definition of strcpy: char *strcpy(char *s1, const char *s2);
-- This function copies the string pointed to by s2 (including the
terminating null character)
-- into the array pointed to by s1. If copying takes place between objects
that overlap,
-- the behavior is undefined. The strcpy function returns the value of s1.
-- Note: since the C function's return value is of no interest, the Ada
interface is a procedure
procedure Strcpy (Target : out C.char_array;
Source : in C.char_array);
pragma Import(C, Strcpy, "strcpy");
Chars1 : C.char_array(1..20);
Chars2 : C.char_array(1..20);
begin
Chars2(1..6) := "qwert" & C.nul;
Strcpy(Chars1, Chars2);
-- Now Chars1(1..6) = "qwert" & C.Nul
end Test;
This example works very fine with my compiler. Now, I have tried to display
the contents
of Chars1 but I don't know how. I have tried several ways but no one worked.
Additional, I have several problems to understand the example above. If I
want to
interface to the function Strlen - Prototype is: function Strlen (Item : in
chars_ptr) return size_t;
what can I do get access to it. Can someone explain me a general way what to
do to interface
C functions in Ada? I have read the online help that comes along with
win32ada, but the explanation
is not so good. Does someone knows if there are examples which explain the
access to the
C interface functions. It would be very nice if someone can give me
references or examples.
Thanks for all in advance.
Yours Henrik
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: C Interface
2002-04-03 11:05 C Interface Henrik Quintel
@ 2002-04-03 13:40 ` Mário Amado Alves
2002-04-03 15:59 ` Ted Dennison
2002-04-04 13:12 ` Henrik Quintel
2 siblings, 0 replies; 6+ messages in thread
From: Mário Amado Alves @ 2002-04-03 13:40 UTC (permalink / raw)
"... Now, I have tried to display the contents of Chars1 but I don't
know how. I have tried several ways but no one worked."
Convert to String and print with Ada.Text_IO.Put_Line. (There should be
a To_Ada function or similar in Interfaces.C (or its child Strings) to
convert a chars_array to String.)
"... Does someone knows if there are examples which explain the access
to the C interface functions. It would be very nice if someone can give
me references or examples."
For examples look at _bindings_ to C libraries e.g. database systems
(Postgres, MySQL), graphics (X), Windows libraires, etc. There is a lot
online. Start in AdaPower.Com as usual.
I don't know of any 'tutorial' online, but I'm sure Cohen's book
explains the issue.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C Interface
2002-04-03 11:05 C Interface Henrik Quintel
2002-04-03 13:40 ` Mário Amado Alves
@ 2002-04-03 15:59 ` Ted Dennison
2002-04-03 19:20 ` Henrik Quintel
2002-04-04 13:12 ` Henrik Quintel
2 siblings, 1 reply; 6+ messages in thread
From: Ted Dennison @ 2002-04-03 15:59 UTC (permalink / raw)
"Henrik Quintel" <henrik.quintel@gmx.de> wrote in message news:<a8enka$rns13$1@ID-79136.news.dfncis.de>...
> package C renames Interfaces.C;
...
> Chars1 : C.char_array(1..20);
...
> This example works very fine with my compiler. Now, I have tried to display
> the contents
> of Chars1 but I don't know how. I have tried several ways but no one worked.
Generally, I would convert such a string into an Ada string (using
Interfaces.C.To_Ada) to deal with it inside of Ada, rather than trying
to deal directly with the C string. If you let C use C strings, and
Ada use Ada strings, the world is a happier place. :-)
--
T.E.D.
Home - mailto:dennison@telepath.com (Yahoo: Ted_Dennison)
Homepage - http://www.telepath.com/dennison/Ted/TED.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C Interface
2002-04-03 15:59 ` Ted Dennison
@ 2002-04-03 19:20 ` Henrik Quintel
2002-04-04 3:13 ` Steve Doiel
0 siblings, 1 reply; 6+ messages in thread
From: Henrik Quintel @ 2002-04-03 19:20 UTC (permalink / raw)
Dear all,
I have found the solution to display the result of the strcpy program:
Put_Line(To_ada(Chars1);
But now I have the problem that I cannot make use of the operation strlen to
get the length of the string Chars1.
Perhaps someone knows a solution.
Thanks in advance,
Yours Henrik
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C Interface
2002-04-03 19:20 ` Henrik Quintel
@ 2002-04-04 3:13 ` Steve Doiel
0 siblings, 0 replies; 6+ messages in thread
From: Steve Doiel @ 2002-04-04 3:13 UTC (permalink / raw)
Have a look at the standard package Interfaces.C.Strings.
BTW: It seems odd that you're using the C library to manipulate strings in
Ada.
SteveD
"Henrik Quintel" <henrik.quintel@gmx.de> wrote in message
news:a8fklk$rqhgc$1@ID-79136.news.dfncis.de...
> Dear all,
> I have found the solution to display the result of the strcpy program:
> Put_Line(To_ada(Chars1);
>
> But now I have the problem that I cannot make use of the operation strlen
to
> get the length of the string Chars1.
>
> Perhaps someone knows a solution.
>
> Thanks in advance,
>
> Yours Henrik
>
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: C Interface
2002-04-03 11:05 C Interface Henrik Quintel
2002-04-03 13:40 ` Mário Amado Alves
2002-04-03 15:59 ` Ted Dennison
@ 2002-04-04 13:12 ` Henrik Quintel
2 siblings, 0 replies; 6+ messages in thread
From: Henrik Quintel @ 2002-04-04 13:12 UTC (permalink / raw)
Hi everybody,
thanks all for the great answers.
I have also ordered Norman Cohen's book
"Ada as a second language". Hope this
book will answer all my questions.
Thanks again.
Yours Henrik
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2002-04-04 13:12 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-03 11:05 C Interface Henrik Quintel
2002-04-03 13:40 ` Mário Amado Alves
2002-04-03 15:59 ` Ted Dennison
2002-04-03 19:20 ` Henrik Quintel
2002-04-04 3:13 ` Steve Doiel
2002-04-04 13:12 ` Henrik Quintel
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox