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.2 required=5.0 tests=BAYES_00,FROM_WORDY, INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,86ec22e070e319c0 X-Google-Attributes: gid103376,public From: "Nick Roberts" Subject: Re: How do I get this to work?? Date: 1999/01/22 Message-ID: <78a1oe$5m5$1@plug.news.pipex.net>#1/1 X-Deja-AN: 435638495 References: <76s0dp$1v4$1@nntp3.uunet.ca> <76tbvv$ba5$1@nntp3.uunet.ca> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 Organization: UUNET WorldCom server (post doesn't reflect views of UUNET WorldCom) Keywords: interface to C; C pointer type; C parameter; pass parameter; import; export Newsgroups: comp.lang.ada Date: 1999-01-22T00:00:00+00:00 List-Id: Dear Chris, The solution to your problem is (as ever) terrifically simple: declare the variable Session at the same static level as the type Session_Ptr_Type. You do not need a Session_Ptr variable at all. For example: package Connection_Management is procedure Open_Connection; procedure Close_Connection; Connection_Error: exception; end Connection_Management; with Interfaces.C; package body Connection_Management is type Session_Type is record Status : Interfaces.C.int := 0; end record; type Session_Ptr_Type is access all Session_Type; pragma Convention(C,Session_Ptr_Type); The_Session: aliased Session_Type; pragma Volatile(The_Session); function Open_Port (Session: in Session_Ptr_Type) return Interfaces.C.int; pragma Import(C,Open_Port,"OpenPort"); function Close_Port (Session: in Session_Ptr_Type) return Interfaces.C.int; pragma Import(C,Close_Port,"ClosePort"); procedure Open_Connection is begin if Open_Port(The_Session'Access) /= 0 then raise Connection_Error; end if; end Open_Connection; procedure Close_Connection is begin if Close_Port(The_Session'Access) /= 0 then raise Connection_Error; end if; end Close_Connection; end Connection_Management; For clarity, I have renamed Session as The_Session. Of course, all this is to illustrate the declaration and use of a C pointer type in Ada. In reality, the body of the above package would be programmed as follows: with Interfaces.C; package body Connection_Management is type Session_Type is record Status : Interfaces.C.int := 0; end record; The_Session: Session_Type; pragma Volatile(The_Session); function Open_Port (Session: in out Session_Type) return Interfaces.C.int; pragma Import(C,Open_Port,"OpenPort"); function Close_Port (Session: in out Session_Type) return Interfaces.C.int; pragma Import(C,Close_Port,"ClosePort"); procedure Open_Connection is begin if Open_Port(The_Session) /= 0 then raise Connection_Error; end if; end Open_Connection; procedure Close_Connection is begin if Close_Port(The_Session) /= 0 then raise Connection_Error; end if; end Close_Connection; end Connection_Management; Ada automatically converts between an 'in out' parameter and the appropriate C pointer type. Don't forget the Volatile pragma, or your compiler is entitled to do something brown and sticky to you. -- Nick Roberts 2104: Witty remark not found - try again later Chris Warwick wrote in message <76tbvv$ba5$1@nntp3.uunet.ca>... |Sorry, perhaps I should have been a little more specific... | |Chris Warwick wrote in message <76s0dp$1v4$1@nntp3.uunet.ca>... |> package Ada_Front_End is |> |> type Session_Type is |> record |> Status : integer := 0; |> end record; |> |> type Session_Ptr_Type is access Session_Type; |> |> function Close_Port (Session : in Session_Ptr_Type) return integer; |> end Ada_Front_End; |> |> procedure Close_Connection is |> Session : aliased constant Ada_Front_End.Session_Type; |> Session_Ptr : Session_Ptr_Type := Session'Access; | |It barfs here, because Session_Ptr_Type has greater scope than Session. My |problem is Close_Port needs a pointer to a Session, so I have to define the |Session_Ptr_Type at the highest level... | |> begin |> Ada_Front_End.Close_Port |> (Session => Session_Ptr); |> end Close_Connection;