Solved

How to pass a Safe Array to an Automation server?

Posted on 1998-02-10
3
703 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 138
conditional code and condition difference 9 85
firstChar challenge 13 132
moto 4g phone enable finger print 4 46
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.

749 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