comp.lang.ada
 help / color / mirror / Atom feed
* Ada equivalent to С++ header sentence.
@ 2004-08-20  8:22 Johnswort
  2004-08-20 11:17 ` Jeff C,
  2004-08-20 12:37 ` Stephen Leake
  0 siblings, 2 replies; 6+ messages in thread
From: Johnswort @ 2004-08-20  8:22 UTC (permalink / raw)


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);

Presumingly, LPSTR may come from Win32. Just in case, VB header
translates it into

Declare Function hbConnect Lib "hapi.dll" Alias "hcConnect" (ByVal
command As String) As Long

Thank you in advance.

P.S. Yep, almost forgot: GNAT 3.15., Win2000 sp4. API comes from
HyperChem 6, if curious.



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

* Re: Ada equivalent to С++ header sentence.
  2004-08-20  8:22 Ada equivalent to С++ header sentence Johnswort
@ 2004-08-20 11:17 ` Jeff C,
  2004-08-20 12:37 ` Stephen Leake
  1 sibling, 0 replies; 6+ messages in thread
From: Jeff C, @ 2004-08-20 11:17 UTC (permalink / raw)



"Johnswort" <rhezusfactor@yahoo.com> wrote in message 
news:3f27506b.0408200022.76094a5e@posting.google.com...
>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:
>

While I don't have a direct answer to your question I would ask that
you check to see if this DLL or some other portion of this product
has a COM/ActiveX style interface. If it does, autocreate your binding
with GNATCOM.

http://www.adapower.com/com/

(Use the latest developer snapshot).

It has not been updated in about a year but I would never consider writing
a binding to anything complex in windows if it has a COM interface
available.






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

* Re: Ada equivalent to &#1057;++ header sentence.
  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
  2004-08-20 12:53   ` Frank J. Lhota
                     ` (2 more replies)
  1 sibling, 3 replies; 6+ messages in thread
From: Stephen Leake @ 2004-08-20 12:37 UTC (permalink / raw)
  To: comp.lang.ada

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




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

* Re: Ada equivalent to &#1057;++ header sentence.
  2004-08-20 12:37 ` Stephen Leake
@ 2004-08-20 12:53   ` Frank J. Lhota
  2004-08-20 19:11   ` Johnswort
  2004-08-24  3:32   ` Randy Brukardt
  2 siblings, 0 replies; 6+ messages in thread
From: Frank J. Lhota @ 2004-08-20 12:53 UTC (permalink / raw)



"Stephen Leake" <stephen_leake@acm.org> wrote in message
news:mailman.5.1093005464.28011.comp.lang.ada@ada-france.org...
> 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!

The Win32 packages that come with GNAT define LPSTR, along with conversion
routines to translate between Ada Strings, C.Interfaces strings and
Win32.LPSTR.





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

* Re: Ada equivalent to &#1057;++ header sentence.
  2004-08-20 12:37 ` Stephen Leake
  2004-08-20 12:53   ` Frank J. Lhota
@ 2004-08-20 19:11   ` Johnswort
  2004-08-24  3:32   ` Randy Brukardt
  2 siblings, 0 replies; 6+ messages in thread
From: Johnswort @ 2004-08-20 19:11 UTC (permalink / raw)


Stephen Leake <stephen_leake@acm.org> wrote in message news:<mailman.5.1093005464.28011.comp.lang.ada@ada-france.org>...
> equivalent in Ada is a function access type:
> 
> type T_hcConnect is access function (lszCmd : in LPSTR) return BOOL;

> importing DLLs. Or, as someone else pointed out, try using GNATCOM.

Thank you all, I'll try the thing above. I like GNATCOM a lot, but
this is not the case where it's usable. Of course, I could have
written ActiveX in VB and pass it to GNATCOM, but it's a mess indeed.

And if somebody asks why not use just VB if the interface is available
- it doesnt work right all the time, which annoys me.



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

* Re: Ada equivalent to &#1057;++ header sentence.
  2004-08-20 12:37 ` Stephen Leake
  2004-08-20 12:53   ` Frank J. Lhota
  2004-08-20 19:11   ` Johnswort
@ 2004-08-24  3:32   ` Randy Brukardt
  2 siblings, 0 replies; 6+ messages in thread
From: Randy Brukardt @ 2004-08-24  3:32 UTC (permalink / raw)


"Stephen Leake" <stephen_leake@acm.org> wrote in message
news:mailman.5.1093005464.28011.comp.lang.ada@ada-france.org...
> rhezusfactor@yahoo.com (Johnswort) writes:
...
> > 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.

I was with you until this point, but I don't agree with the advice to use
System.Address. Using it in Ada 95 (other than with an address clause)
represents a design mistake in my view.

Claw defines LPStr as
    type LPStr is access all Interfaces.C.Char;

and uses the Interfaces.C To_C and To_Ada functions to do the needed
conversions. Note that Interfaces.C.Char_Array has aliased components, so
that
    Name(Name'First)'Access
passes the first character of a Char_Array, (and thus the whole array in C
terms).

This has the advantage of being more strongly typed - it's still possible to
pass a char array that's not null terminated, but you can't accidentially
pass an array of integers or (more commonly) some record type.
Rule of thumb: Never, ever write weakly typed code when it's reasonable to
write strongly typed code.

An alternative is to use Interfaces.C.Strings (there's no reason to mess
with Interfaces.C.Pointers for this), but it tends to make you do some
storage management that's usually unnecessary. (We also didn't have it
working in Janus/Ada when we started Claw, which is probably really why we
avoided it.)

                    Randy.






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

end of thread, other threads:[~2004-08-24  3:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2004-08-20 12:53   ` Frank J. Lhota
2004-08-20 19:11   ` Johnswort
2004-08-24  3:32   ` Randy Brukardt

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