comp.lang.ada
 help / color / mirror / Atom feed
From: Stephen Leake <stephen_leake@acm.org>
To: comp.lang.ada@ada-france.org
Subject: Re: Ada equivalent to &#1057;++ header sentence.
Date: 20 Aug 2004 08:37:26 -0400
Date: 2004-08-20T08:37:26-04:00	[thread overview]
Message-ID: <mailman.5.1093005464.28011.comp.lang.ada@ada-france.org> (raw)
In-Reply-To: <3f27506b.0408200022.76094a5e@posting.google.com>

rhezusfactor@yahoo.com (Johnswort) writes:

> I try to make a binding on an API, which has a header in C++ syntax.
> As a complete moron in both C/C++, I want to grab the last straw
> before giving up. So I ask for a hint: what would be the Ada
> equivalent to the following:
> 
> typedef BOOL                    (_stdcall T_hcConnect)(LPSTR lszCmd);

This declares T_hcConnect to be a type designating a function that
takes an LPSTR parameter and returns a BOOL result. The normal use in
C for such a type is to designate a pointer to a function. So the
equivalent in Ada is a function access type:

type T_hcConnect is access function (lszCmd : in LPSTR) return BOOL;

> Presumingly, LPSTR may come from Win32. 

Yes, it stands for "Long Pointer to String". You might be tempted to
define it in Ada as:

type LPSTR is access String; --  WRONG!

But since an Ada string is an Ada array, and a C string is just a
pointer to Char, this is wrong. You can use Interfaces.C.Pointers, but
that gets very cumbersome. Use System.Address, and make sure you
append ASCII.Nul to the end of any strings passed to C.

> Just in case, VB header translates it into
> 
> Declare Function hbConnect Lib "hapi.dll" Alias "hcConnect" (ByVal
> command As String) As Long

That's similar. But VB puts wrappers around such functions for you, so
it's not completely identical. For instance, it's not a type.

Since you appear to be importing a DLL, there are better ways than
declaring function access types. Read the GNAT user guide section on
importing DLLs. Or, as someone else pointed out, try using GNATCOM.

-- 
-- Stephe




  parent reply	other threads:[~2004-08-20 12:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-20  8:22 Ada equivalent to &#1057;++ header sentence Johnswort
2004-08-20 11:17 ` Jeff C,
2004-08-20 12:37 ` Stephen Leake [this message]
2004-08-20 12:53   ` Frank J. Lhota
2004-08-20 19:11   ` Johnswort
2004-08-24  3:32   ` Randy Brukardt
replies disabled

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