Solved

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

Posted on 2002-04-29
2
559 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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 …
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 bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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