Solved

how do I return an array (COleSafeArray?) using automation?

Posted on 2002-04-29
2
549 Views
Last Modified: 2013-11-20
I am writing two programs which interact with automation, and the client wants to get an array (of floats, actually) back from the server. I set up the server method (GetData) to return a VARIANT, and have tried the following, just as a test for returning a CByteArray:

VARIANT CServerDlgAutoProxy::GetData()
{
#if 0
// ignore VStudio generated stuff
     VARIANT vaResult;
     VariantInit(&vaResult);
     // TODO: Add your dispatch handler code here
#endif

     CByteArray byteArray;
     char c;
     for (c=0; c<10; c++)
          byteArray.Add(c);
     COleVariant v(byteArray);
     return v;
}

My client code looks like this

void CTestAutoDlg::OnGetData()
{
     VARIANT ret;
     ret=m_KlustaDispatch.GetPartitionData();
}

However, this doesn't work. I get exceptions and kernel page faults, and various nasty messages.

Can someone tell me what is the correct way to return an array using automation? What I would really like to return is a COleSafeArray.

Also, if I do this, which program owns the memory, and how do I displose of it? Where should I use Detach(), if at all?


0
Comment
Question by:wjh021797
2 Comments
 
LVL 3

Accepted Solution

by:
job_s earned 200 total points
ID: 6977240
STDMETHODIMP get_WaveData(VARIANT *pVariant)
{
     
     HRESULT hReturn  = S_OK;
     VariantInit (pVariant);
     pVariant->vt = VT_EMPTY;
        VariantInit (pVariant);
        pVariant->vt = VT_ARRAY | VT_UI1;
      // Array of integers
      SAFEARRAY* psa;
        //number of elements
        SAFEARRAYBOUND bound = { dwFileLenthHigh , 0 };
        psa = SafeArrayCreate ( VT_UI1, 1, &bound);
        if (psa == NULL)
           return E_OUTOFMEMORY;
         for (long i=0; i<(long)dwFileLenthHigh ; i++)
             SafeArrayPutElement( psa, &i,m_pchBuffer+i );
         pVariant->parray = psa;
     return hReturn ;
}

Add function with varient pointer parameter.
code as the above source.

if ur using script U can call
 
WaveData = get_WaveData()

will return the data to WaveData
0
 

Expert Comment

by:xzxzzxw
ID: 9572972
While the above is a perfectly good answer, there is actually an easy way to return / use a COleSafeArray, when you're trying to pass back a byte array, and it's as easy as this:

VARIANT MyClass::MyFunc( void )
{
      COleSafeArray vaRes;
      
      char *sString = ; // Insert string here
      vaRes.vt = (VT_ARRAY | VT_UI1);
      vaRes.CreateOneDim( VT_UI1, (DWORD) strlen( sString ), sString );

      return vaRes.Detach();
}
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now