Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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
Perl Awk Need Help 3 128
Doc'in system (example?) BA 7 115
either24  challenge 19 101
tripleUp challenge 7 86
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.

839 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