comp.lang.ada
 help / color / mirror / Atom feed
* Calling C's fopen from Ada (Aonix compiler)
@ 2000-12-12  1:15 Wayne Magor
  2000-12-12  3:06 ` Robert Dewar
  2000-12-19  7:54 ` Gerhard Häring
  0 siblings, 2 replies; 6+ messages in thread
From: Wayne Magor @ 2000-12-12  1:15 UTC (permalink / raw)


I need to call a C procedure which requires a file pointer (the file
must be opened by
the caller).  So I want to call C's fopen from Ada (I'm using the Aonix
Ada compiler on
Windows NT).

This causes an access error exception in the program that requires the
file pointer, however,
if I just write a little C function called "my_fopen" which looks just
like fopen and is just
a call-through function, it works fine.  Below is an example of how the
interfaces to the
standard C function and mine work.

I could go with the work-around and link in an extra obj file that just
does the call-through,
but I'd rather figure out why this doesn't work.  Can anyone give me
some insight as to what
I don't know about this?  In the debugger, the pointers appear to point
to a similar block
of data for my_fopen and the C fopen, but the C one just will not work.
.
.
.
  package C renames Interfaces.C;

  F : File_Ptr;

  function my_fopen (Name : in C.Char_Array;
                     Mode : in C.Char_Array) return File_Ptr;
  pragma Import (C, my_fopen, "my_fopen");

  function fopen (Name : in C.Char_Array;
                  Mode : in C.Char_Array) return File_Ptr;
  pragma Import (C, fopen, "fopen");

begin

  F := my_fopen("my_file.txt" & C.nul, "r" & C.nul);
  F := fopen("my_file.txt" & C.nul, "r" & C.nul);





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

* Re: Calling C's fopen from Ada (Aonix compiler)
  2000-12-12  1:15 Calling C's fopen from Ada (Aonix compiler) Wayne Magor
@ 2000-12-12  3:06 ` Robert Dewar
  2000-12-19  7:54 ` Gerhard Häring
  1 sibling, 0 replies; 6+ messages in thread
From: Robert Dewar @ 2000-12-12  3:06 UTC (permalink / raw)


In article <3A357C48.D4CAC970@nowhere.com>,
  Wayne Magor <none@nowhere.com> wrote:
>   F := my_fopen("my_file.txt" & C.nul, "r" & C.nul);
>   F := fopen("my_file.txt" & C.nul, "r" & C.nul);

This should work with any compiler that follows the
implementation advice on how arrays should be passed
to C. This is however only IA, so your compiler might
ignore it. The best advice with a problem like this is
to ask your vendor for help and support, it is exactly
the sort of potentially implementation dependent
question they should be able to answer for you.

Failing that, perhaps someone on the list has used the
particular compiler in question and can answer why it does
not work. Certainly I see nothing wrong with your code
(of course I may be missing something obvious -- it's
always possible :-)


Sent via Deja.com http://www.deja.com/
Before you buy.



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

* Re: Calling C's fopen from Ada (Aonix compiler)
  2000-12-12  1:15 Calling C's fopen from Ada (Aonix compiler) Wayne Magor
  2000-12-12  3:06 ` Robert Dewar
@ 2000-12-19  7:54 ` Gerhard Häring
  2000-12-19 15:33   ` Robert Dewar
  2000-12-19 18:18   ` David Botton
  1 sibling, 2 replies; 6+ messages in thread
From: Gerhard Häring @ 2000-12-19  7:54 UTC (permalink / raw)


I think the error is that you used Char_Array where Interfaces.C.Chars_Ptr
should be used. So the compiler passes the arguments to fopen in by-value
instead of by-reference mode. IMO, an interface to fopen should be something
like:

-------------------------------------------------------------------------
WITH Interfaces.C.Strings;
USE  Interfaces.C.Strings;

PACKAGE FO_Wrapper IS

    SUBTYPE File_Ptr IS chars_ptr;
    
    FUNCTION fopen( name: chars_ptr; mode: chars_ptr ) RETURN File_Ptr;
    PRAGMA Import( C, fopen, "fopen" );
            
END FO_Wrapper;
-------------------------------------------------------------------------
            
Gerhard H�ring

On Mon, 11 Dec 2000 19:15:52 -0600, Wayne Magor <none@nowhere.com> wrote:
>I need to call a C procedure which requires a file pointer (the file
>must be opened by
>the caller).  So I want to call C's fopen from Ada (I'm using the Aonix
>Ada compiler on
>Windows NT).
>
>This causes an access error exception in the program that requires the
>file pointer, however,
[snip]
>  package C renames Interfaces.C;
>
>  F : File_Ptr;
>
[snip]
>  function fopen (Name : in C.Char_Array;
>                  Mode : in C.Char_Array) return File_Ptr;
>  pragma Import (C, fopen, "fopen");
>
-- 
mail:   gerhard <at> bigfoot <dot> de
web:    http://highqualdev.com



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

* Re: Calling C's fopen from Ada (Aonix compiler)
  2000-12-19  7:54 ` Gerhard Häring
@ 2000-12-19 15:33   ` Robert Dewar
  2000-12-19 18:18   ` David Botton
  1 sibling, 0 replies; 6+ messages in thread
From: Robert Dewar @ 2000-12-19 15:33 UTC (permalink / raw)


In article
<slrn93u5jr.1ip.gerhard.nospam@lilith.hqd-internal.de>,
  gerhard.nospam@bigfoot.de (Gerhard =?iso-8859-1?Q?H=E4ring?=)
wrote:
> I think the error is that you used Char_Array where
Interfaces.C.Chars_Ptr
> should be used. So the compiler passes the arguments to fopen
in by-value
> instead of by-reference mode.

This is possible, but would clearly violate the implementation
advice. If a compiler does violate the IA, it should tell you
that it does (see RM M(2)).


Sent via Deja.com
http://www.deja.com/



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

* Re: Calling C's fopen from Ada (Aonix compiler)
  2000-12-19  7:54 ` Gerhard Häring
  2000-12-19 15:33   ` Robert Dewar
@ 2000-12-19 18:18   ` David Botton
  2000-12-20  0:31     ` Robert Dewar
  1 sibling, 1 reply; 6+ messages in thread
From: David Botton @ 2000-12-19 18:18 UTC (permalink / raw)
  To: comp.lang.ada

No, char_array will be passed as a char*.

David Botton

----- Original Message -----
From: "Gerhard H�ring" <gerhard.nospam@bigfoot.de>


> I think the error is that you used Char_Array where Interfaces.C.Chars_Ptr
> should be used. So the compiler passes the arguments to fopen in by-value
> instead of by-reference mode. IMO, an interface to fopen should be
something
> like:

<CLIP>

>
> Gerhard H�ring
>
> On Mon, 11 Dec 2000 19:15:52 -0600, Wayne Magor <none@nowhere.com> wrote:
> >I need to call a C procedure which requires a file pointer (the file
> >must be opened by
> >the caller).  So I want to call C's fopen from Ada (I'm using the Aonix
> >Ada compiler on
> >Windows NT).
> >
> >This causes an access error exception in the program that requires the
> >file pointer, however,
> [snip]
> >  package C renames Interfaces.C;
> >
> >  F : File_Ptr;
> >
> [snip]
> >  function fopen (Name : in C.Char_Array;
> >                  Mode : in C.Char_Array) return File_Ptr;
> >  pragma Import (C, fopen, "fopen");






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

* Re: Calling C's fopen from Ada (Aonix compiler)
  2000-12-19 18:18   ` David Botton
@ 2000-12-20  0:31     ` Robert Dewar
  0 siblings, 0 replies; 6+ messages in thread
From: Robert Dewar @ 2000-12-20  0:31 UTC (permalink / raw)


In article <00b401c069e8$1ff912c0$850118ac@dbdell2000>,
  comp.lang.ada@ada.eu.org wrote:

> No, char_array will be passed as a char*.

You cannot say this with such confidence, there is no
requirement that this be the case ...


Sent via Deja.com
http://www.deja.com/



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

end of thread, other threads:[~2000-12-20  0:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-12-12  1:15 Calling C's fopen from Ada (Aonix compiler) Wayne Magor
2000-12-12  3:06 ` Robert Dewar
2000-12-19  7:54 ` Gerhard Häring
2000-12-19 15:33   ` Robert Dewar
2000-12-19 18:18   ` David Botton
2000-12-20  0:31     ` Robert Dewar

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