comp.lang.ada
 help / color / mirror / Atom feed
* Mapping c++ type VARIANT to Ada
@ 2002-10-06 18:27 Björn Lundin
  2002-10-06 19:16 ` Jeffrey Carter
  2002-10-07  3:21 ` SteveD
  0 siblings, 2 replies; 5+ messages in thread
From: Björn Lundin @ 2002-10-06 18:27 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=us-ascii, Size: 1606 bytes --]

Hello!
I'm trying to interface to an OPC Dll on W2k, which uses callbacks.

What I've done successfully do far is to set up a procedure to be runned by 
the dll, as a callback. I get the callback, and can interpret some of its 
contents, but I fail when I map a c++ VARIANT type.

The dll looks like this:

void CALLBACK EXPORT OPCUpdateCallback (HANDLE hGroup, HANDLE hItem, VARIANT 
*pVar, FILETIME *pTimeStamp, DWORD *pQuality)

I mapped it like this to a procedure like this:

  type Opc_Callback_Handler_Type is access protected
          procedure(Group     : Group_Handle_Type;
                    Item      : Item_Handle_Type;
                    Value     : Byte_Array_Ptr;
                    Timestamp : File_Time_Type;
                    Quality   : D_Word);

where I also have these definitions

  package C renames Interfaces.C;

  type Byte is range 0..255;
  for Byte'Size use 8;
  type Byte_Array is array (Natural range <>) of aliased Byte;

  type Byte_Array_Ptr is access all Byte_Array(1..16);

  type Server_Handle_Type is new C.Long;
  type Group_Handle_Type  is new C.Long;
  type Item_Handle_Type   is new C.Long;
  type D_Word is range 0 .. 2**16 -1;
  for D_Word'Size use 16;

  type File_Time_Type is record
      Low_Date_Time  : C.Long := C.Long'First;
      High_Date_Time : C.Long := C.Long'First;
  end record;



I get the Group and the Item and something in the byte_array, that seems to 
change fairly ok accordingly to the callback, but the filetime and quality 
is not reported, which makes me thing that my VARIANT mapping is wrong?

Any suggestions?

/Bj�rn
  



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

* Re: Mapping c++ type VARIANT to Ada
  2002-10-06 18:27 Mapping c++ type VARIANT to Ada Björn Lundin
@ 2002-10-06 19:16 ` Jeffrey Carter
  2002-10-07 16:10   ` Björn Lundin
  2002-10-07  3:21 ` SteveD
  1 sibling, 1 reply; 5+ messages in thread
From: Jeffrey Carter @ 2002-10-06 19:16 UTC (permalink / raw)


Bj�rn Lundin wrote:
>   package C renames Interfaces.C;
> 
>   type Byte is range 0..255;
>   for Byte'Size use 8;
>   type Byte_Array is array (Natural range <>) of aliased Byte;
> 
>   type Byte_Array_Ptr is access all Byte_Array(1..16);
> 
>   type Server_Handle_Type is new C.Long;
>   type Group_Handle_Type  is new C.Long;
>   type Item_Handle_Type   is new C.Long;
>   type D_Word is range 0 .. 2**16 -1;
>   for D_Word'Size use 16;
> 
>   type File_Time_Type is record
>       Low_Date_Time  : C.Long := C.Long'First;
>       High_Date_Time : C.Long := C.Long'First;
>   end record;
> 
> I get the Group and the Item and something in the byte_array, that seems to 
> change fairly ok accordingly to the callback, but the filetime and quality 
> is not reported, which makes me thing that my VARIANT mapping is wrong?
> 
> Any suggestions?

I could be more helpful if you included the definition of VARIANT.

I would suggest that you use the types from Interfaces.C whenever 
possible. These are guaranteed to be C compatible. Instead of 
Byte_Array_Ptr, you should use Interfaces.C.Strings.Chars_Ptr. If you 
can't use Interfaces.C.Int, Interfaces.C.Short, or Interfaces.C.Long for 
D_Word, define it as

type D_Word is mod 2 ** 16;
for D_Word'Size use 16;
pragma Convention (C, D_Word);

Similarly, apply

pragma Convention (C, File_Time_Type);

Note that time stamp and quality are both passed as pointers. I'm not 
sure about a record, but an integer will be passed by copy. You need one of

Quality : access D_Word);

or

type D_Word_Ptr is access all D_Word;
pragma Convention (C, D_Word_Ptr);

Quality : D_Word_Ptr);

You may need something similar for the time stamp.

Finally, C knows nothing about protected procedures. You probably should 
  remove the "protected" from the type definition, and apply

pragma Convention (C, Opc_Callback_Handler_Type);

Finally, any procedure you pass using this type should be either Export 
or Convention C.

Some of these may not be strictly necessary, but it's better to be safe 
than sorry.

-- 
Jeff Carter
"It's all right, Taggart. Just a man and a horse being hung out there."
Blazing Saddles




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

* Re: Mapping c++ type VARIANT to Ada
  2002-10-06 18:27 Mapping c++ type VARIANT to Ada Björn Lundin
  2002-10-06 19:16 ` Jeffrey Carter
@ 2002-10-07  3:21 ` SteveD
  2002-10-07 16:00   ` Björn Lundin
  1 sibling, 1 reply; 5+ messages in thread
From: SteveD @ 2002-10-07  3:21 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 668 bytes --]


"Bj�rn Lundin" <bjorn.lundin.nospam@swipnet.se> wrote in message
news:cf%n9.1730$hT3.5616@nntpserver.swip.net...
[snip]
>
> I get the Group and the Item and something in the byte_array, that seems
to
> change fairly ok accordingly to the callback, but the filetime and quality
> is not reported, which makes me thing that my VARIANT mapping is wrong?
>
> Any suggestions?

Go to:
  http://www.adapower.com/gnatcom/index.html

GNATCOM defines a type GNATCOM.Variant.

If you're using GNAT you can use GNATCOM, if you're using a different
compiler you may be able to use GNATCOM as well, but you can certainly look
for examples that work.

SteveD

>
> /Bj�rn
>





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

* Re: Mapping c++ type VARIANT to Ada
  2002-10-07  3:21 ` SteveD
@ 2002-10-07 16:00   ` Björn Lundin
  0 siblings, 0 replies; 5+ messages in thread
From: Björn Lundin @ 2002-10-07 16:00 UTC (permalink / raw)


SteveD wrote:

> 
> GNATCOM defines a type GNATCOM.Variant.
> 
> If you're using GNAT you can use GNATCOM, if you're using a different
> compiler you may be able to use GNATCOM as well, but you can certainly
> look for examples that work.
> 
/thanks, just what I was looking for

/Bj�rn



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

* Re: Mapping c++ type VARIANT to Ada
  2002-10-06 19:16 ` Jeffrey Carter
@ 2002-10-07 16:10   ` Björn Lundin
  0 siblings, 0 replies; 5+ messages in thread
From: Björn Lundin @ 2002-10-07 16:10 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset=us-ascii, Size: 1202 bytes --]

Jeffrey Carter wrote:


> 
> I could be more helpful if you included the definition of VARIANT.

That was my question, although not very clear. 
Steve D pointed me to gnatcom

> 
> I would suggest that you use the types from Interfaces.C whenever
> possible. 

I'll do that

> Note that time stamp and quality are both passed as pointers. I'm not
> sure about a record, but an integer will be passed by copy. You need one
> of

Well I wrote the spec as pointers, but in the dll's documetation they are 
not, however in all other similar calls/callbacks they are pointers, which 
now makes me now belive that the documentation is wrong.
I'll try with pointers  
> 
> Finally, C knows nothing about protected procedures. You probably should
>   remove the "protected" from the type definition, and apply
> 
> pragma Convention (C, Opc_Callback_Handler_Type);
>

This construct was taken from 'Ada as a second language'. 
I tried whitout it just an ordinary procedure, and it worked.
However, the point with protected types was to enable some sort
of tasking, since the callbacks can come anytime, always asychronic.
I do get the callbacks now too.

Thanks for the tips about the pragmas.

/Bj�rn







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

end of thread, other threads:[~2002-10-07 16:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-06 18:27 Mapping c++ type VARIANT to Ada Björn Lundin
2002-10-06 19:16 ` Jeffrey Carter
2002-10-07 16:10   ` Björn Lundin
2002-10-07  3:21 ` SteveD
2002-10-07 16:00   ` Björn Lundin

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