Passing an array from a .NET object to an ATEasy callback procedure

Knowledge Base Article # Q200211

Read Prior Article Read Next Article
Summary How to pass an array from a .NET object to an ATEasy procedure that is being used as a callback
An ATEasy procedure can be used as a callback function when passed to an external library such as a .NET assembly or Windows DLL.

This article will describe how an ATEasy callback procedure can be made to accept an array parameter from a calling .NET object.

In this example, a simple .NET class called MyClass will be used to pass an array of bytes to an ATEasy procedure. The MyClass constructor will accept one parameter of type MyCallback which is declared as a delegate accepting one parameter of type Object. The following c# code describes this class:

using System;
using System.Collections.Generic;
using System.Text;

namespace ArrayTest
   public class MyClass
      public delegate void MyCallback(Object msg);

      private MyCallback m_callback;
      private byte[] m_data = new byte[3];

      public MyClass(MyCallback callback)
         m_callback = callback;    // save the ATEasy procedure pointer

      public void Test()
         m_data[0] = 0;
         m_data[1] = 1;
         m_data[2] = 2;
         m_callback(m_data);    // call the ATEasy procedure with array

Using Object as the parameter type for the delegate, instead of an array, allows the .NET object to convert the passed in array to a Variant when invoking the ATEasy callback procedure.

.Net Callback

The ATEasy callback procedure should be created as shown below. The only parameter should be of type Val Variant.

The procedure VarDimSize should be called to determine the number of elements contained within the data parameter.

Procedure MyCallback(data): Void
data: Val Variant
size: Long
    size = VarDimSize(data, 0)

    print "Received " + Format(size, "0") + " bytes"

    ! fails here since no data in array (index out of bounds)
    print "No Data Received"


Finally, the .NET class can be instantiated and invoked from ATEasy as follows:

tester = new ArrayTest.MyClass(MyCallback)

Note that tester is of type ArrayTest.MyClass and MyCallback is the name of the ATEasy callback procedure.
Calling Test() will result in an array being filled by the .NET object, and passed to the ATEasy callback procedure.
Article Date 2/2/2011 5:20:16 PM
Keywords Callback, .NET, c#, vb, array

0 ratings | 0 out of 5
Read Prior Article Read Next Article