?
Solved

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

Posted on 2002-04-29
2
Medium Priority
?
584 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
[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 Comments
 
LVL 3

Accepted Solution

by:
job_s earned 800 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

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

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 …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

770 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