Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to pass a Safe Array to an Automation server?

Posted on 1998-02-10
3
Medium Priority
?
732 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
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Tommy Hui earned 320 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

Industry Leaders: 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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month7 days, 8 hours left to enroll

824 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