Solved

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

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
haveThree challenge 22 123
fix34  challenge 9 168
I could not set window to top 4 45
Problem to Office 1 45
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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