Solved

How to pass a Safe Array to an Automation server?

Posted on 1998-02-10
3
705 Views
Last Modified: 2013-11-19
I added an Automation object to the application I am working on.  This allows our clients using Visual Basic to access our application's functionality easily.  Everything works fine except when I want to pass arrays of data between a Visual Basic application and my MFC application.  The type of the Variant is VT_ARRAY as expected but the dimension and the element size are random values.

I added a method to my CCmdTarget-derived class using Class Wizard.  This gives...

BOOL CApplication::GetDoubleArray(const VARIANT FAR& varArray)
{
// Make sure the input value in an array
VARTYPE vType=V_VT(&varArray);
ASSERT(vType & VT_ARRAY);
if (vType & VT_ARRAY)
{
   // Things do not work as expected starting from here...

   // lDim and lElemSize are never OK (They seem to be random.)
   long lDim=SafeArrayGetDim(V_ARRAY(&varArray));
   long lElemSize=SafeArrayGetElemsize(V_ARRAY(&varArray));

   // Show that lDim and lElemSize are random values
   CString strMsg;
   strMsg.Format("lDim=%d  lElemSize=%d", lDim, lElemSize);
   AfxMessageBox(strMsg);
}

return TRUE;
}

and...

[id(3)] boolean GetDoubleArray(VARIANT varArray);

in the ODL file.

On Visual Basic side, the code is...

Dim MyObj As Object
Set MyObj = CreateObject("MyObj.Application")
Dim dArray() As Long
ReDim dArray(0 To 10)
MyObj.GetDoubleArray dArray


I have the same problem with in-process and out-of-process Automation servers.
If I add the same method in an ActiveX Control, everything works fine!

Any help will be appreciated.

0
Comment
Question by:blarivie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 80 total points
ID: 1315897
My guess is that you should check the type of the array to see if it has VT_BYREF. I'm betting the type is different in the two cases.
0
 

Author Comment

by:blarivie
ID: 1315898
You are right, I have VT_BYREF in the Automation server and not in the ActiveX Control.
Now, what should I do to make it work in my Automation server?
0
 

Author Comment

by:blarivie
ID: 1315899
Finally, your answer led me to a solution: use "VARIANT FAR*" instead of "const VARIANT FAR&"...  (The other solution is to use the pparray field of the input const VARIANT FAR&. - not discussed here)

BOOL CApplication::GetDoubleArrayPointer(VARIANT FAR* pvarArray)
{
   ASSERT(pvarArray!=NULL);
   if (pvarArray!=NULL)
   {
      VARTYPE vType=V_VT(pvarArray);

      ASSERT((vType&VT_ARRAY) && (vType&VT_R8));
      if ((vType&VT_ARRAY) && (vType&VT_R8))
      {
         SAFEARRAY FAR* psa=V_ARRAY(pvarArray);

         long lDim=SafeArrayGetDim(psa);
         long lElemSize=SafeArrayGetElemsize(psa);

         ASSERT((lDim==1) && (lElemSize==sizeof(double)));
         if ((lDim==1) && (lElemSize==sizeof(double)))
         {
            long lUpper, lLower;
            SafeArrayGetLBound(psa, 1, &lLower);
            SafeArrayGetUBound(psa, 1, &lUpper);
            long lNumItems=lUpper - lLower + 1;

            HRESULT hres;
            if (hres=SafeArrayLock(psa))
               throw hres;

            for (long lIndex=0; lIndex<lNumItems; lIndex++)
            {
               // Testing: return square of index
               ((double*)psa->pvData)[lIndex]=(double)lIndex*lIndex;
            }

            if (hres=SafeArrayUnlock(psa))
               throw hres;
         }
      }
   }

   return TRUE;
}

In Dispatch Map:
DISP_FUNCTION(CApplication, "GetDoubleArrayPointer", GetDoubleArrayPointer, VT_BOOL, VTS_PVARIANT)

In ODL file:
[id(3)] boolean GetDoubleArrayPointer(VARIANT* pvarArray);

In Visual Basic:
Dim MyObj As Object
Set MyObj=CreateObject("MyApp.Application")
Dim v As Variant
ReDim v(0 To 10) As Double
Script.GetDoubleArrayPointer(v)
For i=0 To 10
   MsgBox Str(v(i)), vbOkOnly, "GetDoubleArrayPointer()"
Next i

I found this solution in a Microsoft Knowledge Base Article: "HOWTO: Pass a SafeArray of Strings in a VARIANT*" (Q167668)

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Pressing cancel button with C# UI Automation 3 104
Generic progress indicator 6 121
Work with App store 7 122
Bartender label printing - switch on and off graphics 3 94
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question