comp.lang.ada
 help / color / mirror / Atom feed
* Access to data indirection (newbie)
@ 2007-11-27  7:10 axtens
  2007-11-27  8:11 ` Martin Krischik
  0 siblings, 1 reply; 6+ messages in thread
From: axtens @ 2007-11-27  7:10 UTC (permalink / raw)


G'day everyone

I have a DLL which is receiving a value that is the result of a VARPTR
in VB6, so the value arriving is the address and the data desired can
be found at that address.

So far I've gotten as far as

   function HandlePointer (
         Left : in     Win32.LONG)
     return Win32.LONG is
     begin

   end HandlePointer;

which is, of course, brain-dead. Fact is, though, so far as Ada is
concerned, I'm a newbie.

So tell me, what's the syntax?

Once you've figured that one out, tell me how I'd handle 'Left'
pointing to a record of two integers, such that I can access both
elements of the record.

Kind regards,
Bruce.



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

* Re: Access to data indirection (newbie)
  2007-11-27  7:10 Access to data indirection (newbie) axtens
@ 2007-11-27  8:11 ` Martin Krischik
  2007-11-27 17:32   ` Martin Krischik
  2007-11-27 22:20   ` Simon Wright
  0 siblings, 2 replies; 6+ messages in thread
From: Martin Krischik @ 2007-11-27  8:11 UTC (permalink / raw)


axtens schrieb:
> G'day everyone
> 
> I have a DLL which is receiving a value that is the result of a VARPTR
> in VB6, so the value arriving is the address and the data desired can
> be found at that address.
> 
> So far I've gotten as far as
> 
>    function HandlePointer (
>          Left : in     Win32.LONG)
>      return Win32.LONG is
>      begin
> 
>    end HandlePointer;
> 
> which is, of course, brain-dead. Fact is, though, so far as Ada is
> concerned, I'm a newbie.
> 
> So tell me, what's the syntax?
> 
> Once you've figured that one out, tell me how I'd handle 'Left'
> pointing to a record of two integers, such that I can access both
> elements of the record.

Your next step would be an Unchecked_Conversion:

http://en.wikibooks.org/wiki/Ada_Programming/Type_System#Unchecked_conversion

however - for my taste you are thinking to "Low-Level" and to "C-Like".
In Ada one does not pass pointer or addresses as a pointer. Down that
road lies sorrow not satisfaction.

How about:

type Options is (Long, Short, Whatever);

type Var (Option : Options) is record
   case Option is
	Long =>
	   A_Long : Win32.LONG;
	Short =>
	   A_Short : Win32.SHORT;
        Whatever =>
	   A_Whatever_1 : Win32.LONG;
	   A_Whatever_2 : Win32.LONG;
   end case;
end record:

pragma Unchecked_Union (Var);
pragma Convention (C, Var);

See: http://en.wikibooks.org/wiki/Ada_Programming/Types/record#Union

type VarPtr is access all Var;

pragma Convention (C, VarPtr);

function HandlePointer (
  Left : in VarPtr)
  return Win32.LONG is
begin

end HandlePointer;

pragma Export (C, HandlePointer);

or

function HandlePointer (
  Left : access Var)
  return Win32.LONG is
begin

end HandlePointer;

pragma Export (C, HandlePointer);

or

function HandlePointer (
  Left : in Var)
  return Win32.LONG is
begin

end HandlePointer;

pragma Export (C, HandlePointer);


The later might surprise you bout since the functions is declared
"export c" "in Var" is passed as a pointer.

Martin

-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



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

* Re: Access to data indirection (newbie)
  2007-11-27  8:11 ` Martin Krischik
@ 2007-11-27 17:32   ` Martin Krischik
  2007-11-27 22:20   ` Simon Wright
  1 sibling, 0 replies; 6+ messages in thread
From: Martin Krischik @ 2007-11-27 17:32 UTC (permalink / raw)


Martin Krischik wrote:

> In Ada one does not pass pointer or addresses as a pointer. Down that
> road lies sorrow not satisfaction.

Ups what a funky typo. It shoud read:

In Ada one does not pass pointer or addresses as a long. Down that
road lies sorrow not satisfaction.

Martin
-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com



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

* Re: Access to data indirection (newbie)
  2007-11-27  8:11 ` Martin Krischik
  2007-11-27 17:32   ` Martin Krischik
@ 2007-11-27 22:20   ` Simon Wright
  2007-11-28  3:04     ` axtens
  1 sibling, 1 reply; 6+ messages in thread
From: Simon Wright @ 2007-11-27 22:20 UTC (permalink / raw)


Martin Krischik <krischik@users.sourceforge.net> writes:

> function HandlePointer (
>   Left : in Var)
>   return Win32.LONG is
> begin
>
> end HandlePointer;
>
> pragma Export (C, HandlePointer);
>
>
> The later might surprise you bout since the functions is declared
> "export c" "in Var" is passed as a pointer.

Not sure that's necessarily true! LRM95 B.3(68.1/1 & 69/1) mention
C_Pass_By_Copy-compatible -- probably safer to use (68) & declare it
as 'access Var' (which seems less surprising to me, the reader will
just think "oh, I see" and pass on, whereas the use of 'in Var' would
mean dashing off to the manual every time ...)



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

* Re: Access to data indirection (newbie)
  2007-11-27 22:20   ` Simon Wright
@ 2007-11-28  3:04     ` axtens
  2007-11-28  3:45       ` axtens
  0 siblings, 1 reply; 6+ messages in thread
From: axtens @ 2007-11-28  3:04 UTC (permalink / raw)


On Nov 28, 7:20 am, Simon Wright <simon.j.wri...@mac.com> wrote:
> Martin Krischik <krisc...@users.sourceforge.net> writes:

Well, anyway, I figured out a solution to my question, after reading
the "Summary of Operations on Access Values" in Feldman's "Software
Construction and Data Structures with Ada 95."

For ages I couldn't figure out why I felt a prompting to pick this
book up at a booksale in the local library. Now I think I know what
God was up to.

Nevertheless, thanks to both of you for adding your two bob's worth to
the mix. What I've ended up with, at least this far in, is:

VB6:
  Declare Function handlepointer Lib "release\api.dll" (ByVal x As
Long) As Long
  Dim i As Long
  i = 1001
  Debug.Print VarPtr(i)
  Debug.Print handlepointer(VarPtr(i))

Output:
 1309644
 1001

Api.ads (part of):
   function HandlePointer (
       Left : in     Win32.LONG)
   return Win32.LONG;
   ...
   pragma Export (DLL, HandlePointer );

Api.adb (part of):
   function HandlePointer (
         Left : in     Win32.LONG)
     return Win32.LONG is

      type LongRec is
         record
            L : Win32.LONG;
         end record;

      type PointerToLongRec is access LongRec;

      function Conv is
      new Unchecked_Conversion (Win32.LONG, PointerToLongRec );

      PLongRec : PointerToLongRec;
   begin
      PLongRec := Conv(Left);
      return PLongRec.All.L;
   end HandlePointer;

Kind regards,
Bruce.




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

* Re: Access to data indirection (newbie)
  2007-11-28  3:04     ` axtens
@ 2007-11-28  3:45       ` axtens
  0 siblings, 0 replies; 6+ messages in thread
From: axtens @ 2007-11-28  3:45 UTC (permalink / raw)


Okay, it's all there at http://codeaholic.blogspot.com/2007/11/ada-gnatgcc-passing-data-with-vb6s.html

Kind regards,
Bruce.



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

end of thread, other threads:[~2007-11-28  3:45 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-27  7:10 Access to data indirection (newbie) axtens
2007-11-27  8:11 ` Martin Krischik
2007-11-27 17:32   ` Martin Krischik
2007-11-27 22:20   ` Simon Wright
2007-11-28  3:04     ` axtens
2007-11-28  3:45       ` axtens

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