Solved

Problem returning a VARIANT with a SAFEARRAY of VARIANTs

Posted on 2000-04-10
21
1,422 Views
Last Modified: 2013-11-20
Hello all!
    I'm having trouble returning a VARIANT holding a SAFEARRAY of VARIANTs, and I suspect the
problem is with the ATL macros that define the array. Its like this:

in idl file:
HRESULT RV( [in] ULONG dwCount, [out, retval] VARIANT* pValues);

in server side:
 SAFEARRAY * pRetSA;
 pRetSA = SafeArrayCreate(VT_VARIANT, 2, bounds);
...
 SafeArrayPutElement(pRetSA, arr, &varData);
 ...
 VariantClear(pValues);
 V_VT(pValues) = VT_ARRAY | VT_VARIANT;
 V_ARRAY(pValues) = pRetSA;

and in client:
VARIANT pVariant;
pVariant.vt = VT_BYREF | VT_VARIANT;
hr = pIMME->RV(n, &pVariant);

When I execute this, the server side receives the variant as VT_EMPTY, and
as this it returns it, with error "Invalid access to memory location". What
am I doing wrong?

Thanks in advance,
careta


0
Comment
Question by:careta
  • 9
  • 5
  • 2
  • +3
21 Comments
 
LVL 11

Expert Comment

by:mikeblas
ID: 2702392
Are you calling SafeArrayUnaccessData()?

 > the ATL macros that define the array

What macros are you talking about?

..B ekiM
0
 

Author Comment

by:careta
ID: 2703459
I'm not calling SafeArrayAccessData() or SafeArrayUnaccessData(), since I'm accessing the array contents with safeArrayGet/PutElement.

As for the macros, I meant to
V_VT(pValues) = VT_ARRAY | VT_VARIANT;
V_ARRAY(pValues) = pRetSA;

By now, I removed the line
pVariant.vt = VT_BYREF | VT_VARIANT;
from client code, and changed in the idl file the definition of the VARIANT*
to be [in,out] (instead of [out,retval]), but now, on server return, client error is "Memory is locked".

careta
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 2704105
> By now, I removed the line

Oh. Those aren't ATL macros. They predate ATL by about six years.

 > I'm accessing the array contents with safeArrayGet/PutElement.

Oh!  Indeed, you are. I misread your code.

..B ekiM
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 5

Accepted Solution

by:
vachooho earned 100 total points
ID: 2704865
you call
VariantClear(pValues);
at server side

and you pass
VARIANT pVariant;
// <<<<<<<<<<<<<<<<!!!!!!!!!!
pVariant.vt = VT_BYREF | VT_VARIANT;
hr = pIMME->RV(n, &pVariant);

at client side

you need to clear VARIANT parameter before passing it to function
I use the following code before passing VARIANTs to functions

VARIANT pValues;
::ZeroMemory(&pValues, sizeof(VARIANT));
hr = pIMME->RV(COUNT, &pVariant);


or you can try
to remove VariantClear() from server side
It is not neccessary if you specify [out] variable

Hope this helps



0
 

Author Comment

by:careta
ID: 2704915
Adjusted points from 100 to 200
0
 

Author Comment

by:careta
ID: 2704916
I'm afraid it doesn't help. I already removed the call to VariantClear at server side, and changed in idl file to [in,out] VARIANT*. So now the error I get at client side (on server return) is that the "Memory is locked". Since I'm passing back a safearray, where it should be allocated, at the client side to be filled by the server or created and filled by the server?

thanks,
careta
0
 
LVL 5

Expert Comment

by:vachooho
ID: 2705359
in IDL it is OK to have SAFEARRAY packed in VARIANt as [out, retval] parameter

in client
you need only to supply the space for output
thus

VARIANT var;
Zeromemory(&var, sizeof(var));
pInterface->CallFunction(&var);

in server side
try to use COleSafeArray MFC class
it make all neccessary preparation work for you
HRESULT CallFunction(VARIANT * pVar)
{
 COleSafeArray arr;
 arr.Create(...);
 arr.PutElement(...);
 *Var = arr.Detach();
}

hope this helps

also be sure you do not place in safe array any locked data.
What is in VARIANTs in safearray?

0
 
LVL 4

Expert Comment

by:inpras
ID: 2705368
Hi try using
hres = VariantCopy(pValues, pRetSA);
check for hres
Hope this helps
inpras
0
 
LVL 5

Expert Comment

by:vachooho
ID: 2705380
Since I'm passing back a safearray, where it should be allocated, at the client side to be filled by the server or created and filled by the server?


===================
it depends on your needs and the architecture you select for your application.

I prefer to allocate arrays at server side and free them at client side
0
 

Author Comment

by:careta
ID: 2705602
For this code
HRESULT CallFunction(VARIANT * pVar)
{
 COleSafeArray arr;
 arr.Create(...);
 arr.PutElement(...);
 *Var = arr.Detach();
}

since I'm not using MFC (I hope people in this forum don't mind :-) ), what is the Detach() equivalent using SAFEARRAY?

careta
0
 
LVL 5

Expert Comment

by:vachooho
ID: 2705610
since COleSafeArray is the wrapper for SAFEARRAY Detach() return VARIANT with SAFEARRAY packed into it.
You do not need Detach() for Pure API calls.

0
 

Author Comment

by:careta
ID: 2705614
For this code
HRESULT CallFunction(VARIANT * pVar)
{
 COleSafeArray arr;
 arr.Create(...);
 arr.PutElement(...);
 *Var = arr.Detach();
}

since I'm not using MFC (I hope people in this forum don't mind :-) ), what is the Detach() equivalent using SAFEARRAY?

careta
0
 
LVL 5

Expert Comment

by:vachooho
ID: 2705615
could you send the complete implementation of server function and the piece of code from client where that function called?
0
 

Author Comment

by:careta
ID: 2705643
> since COleSafeArray is the wrapper for SAFEARRAY Detach() return VARIANT with SAFEARRAY packed into it.

Then I really don't see what am I doing wrong and what is locking the memory. All I need is 4 lines of atl code for server side and 4 lines of atl code for client side to show how to return a variant with a safearray of variants (doubles).

Thanks,
Aaron

0
 

Author Comment

by:careta
ID: 2705666
Sure, I'll be glad to, but just tomorrow (I'm at home now, without access to the files).
Aaron
0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2706887
okay - I knocked up a quick example - based on your information

and I had no trouble I will post the relevent bits of code for you to compare
- it may help - its okay using the MFC wrappers but as you may know MFC drags along a lot of other garbage with it as well and is not conducive to compact code - NOTE my array is 1D with 1 element see bounds definition

server code

in my idl I declare

      interface IIMME : IDispatch
      {
            [id(1), helpstring("method RV")] HRESULT RV([in]ULONG dwCount, [in, out] VARIANT * var);
      };

in my .h file

      STDMETHOD(RV)(/*[in]*/ULONG dwCount, /*[in,out]*/ VARIANT * var);

and in my .cpp file

STDMETHODIMP CIMME::RV(ULONG dwCount, VARIANT *pValues)
{
      SAFEARRAY * pRetSA;
      SAFEARRAYBOUND bounds[]={ {1, 0} };
      long ar[]= {0};

      // create our array
      pRetSA = SafeArrayCreate(VT_VARIANT, 1, bounds);

      // insert our data
      VARIANT data;
      VariantInit(&data);
      V_VT(&data) = VT_I4;
      V_I4(&data) = 2000;

      HRESULT hr=SafeArrayPutElement(pRetSA,ar,&data); // 0x8002000b DISP_E_BADINDEX
      
      // add to our out value
      VariantClear(pValues);
      V_VT(pValues) = VT_ARRAY | VT_VARIANT;
      V_ARRAY(pValues) = pRetSA;




      return S_OK;
}

for the client side

      IIMMEPtr ptrIMME;

      if (S_OK==ptrIMME.CreateInstance(CLSID_IMME))
      {
            // create value to store data
            VARIANT var;
            VariantInit(&var);

            // call our method
            ptrIMME->RV(22,&var);

            // extract our returned data
            SAFEARRAY *psa=V_ARRAY(&var);

            long ar[]={0};
            VARIANT data;
            VariantInit(&data);

            HRESULT hr=SafeArrayGetElement(psa,ar,&data);

            // destroy the array
            SafeArrayDestroy(psa);

            ptrIMME.Release();

      }

Okay it isn't perfect as it requires error checking - (and I my use of [in,out] is open to dubious practice charges)- I can send the whole sample project if you want.

If you are only returning the safearray then maybe you only want an [out] or [out,retval] parameter.

May 1999 MSJ Wicked Code article by Jeff Prosise covers just this topic

0
 

Author Comment

by:careta
ID: 2707596
Ok, I solved my problem, thanks to vachooho and ShaunWilde.
 
Hmmm... I'm not so familiar with points distribution. Who should get them? Certainly both answers helped me a lot.






0
 
LVL 9

Expert Comment

by:ShaunWilde
ID: 2707694
post you problem on the http://www1.experts-exchange.com/Customer_Service/Experts_Exchange/ forum and they will hopefully sort it out

say how you want the split done

because there has been a lot in this topic they may wish for you to post what you final answer was.

0
 
LVL 3

Expert Comment

by:darinw
ID: 2714467
Community Support has reduced points from 200 to 100
0
 
LVL 3

Expert Comment

by:darinw
ID: 2714468
Hi careta,

I have reduced the points on this question to one half. Please accept one of the Experts comments as an answer. Remember, the Accept Comment as Answer button is in the header of the comment.

For the second Expert, post a question in this topic area. The new question title should be 'For ExpertName - 10325852' and it should be for 100 points.

For your convenience, you can use this link to create the new question:
http://www1.experts-exchange.com/bin/NewQForm?ta=44

Please take a moment to post your solution as the Expert recommended.

darinw
Customer Service
0
 

Author Comment

by:careta
ID: 2720081
Following is the code I made, after all responses I got from you. I removed error handling and other stuff for simplicity. If somebody still sees a problem, pls tell me.

// idl
[id(3)] HRESULT ReadWriteVariant([in] ULONG dwCount, [in, out] VARIANT* pValues);

// server code
STDMETHODIMP CMyClass::ReadWriteVariant(UINT dwCount, VARIANT* pValues) {
      HRESULT hr = S_OK;
      DWORD dwArgs;
      CComBSTR* pVar = NULL;
      DWORD i, j;
      double dblValue = 0.0;
      CComBSTR bstr;

      // we receieve the variables to read in a safearray, so first of all
      // unpack it and get back the names of the variables
      hr = UnpackSafearray(*pValues, &pVar, &dwArgs);

      // create bidimensional vector
      SAFEARRAY* pRetSA;
    SAFEARRAYBOUND appBounds[] = {{dwArgs, 0}, {dwCount * 2, 0}};
      long ix[2];
    pRetSA = SafeArrayCreate(VT_VARIANT, 2, appBounds);
      VARIANT varData;

      // for dwCount (times to read each variable)
      for(i = 0; i < dwCount; i++) {
            // for each variable
            for(j = 0; j < dwArgs; j++) {
                  // read variable from target
                  hr = ReadSingleVariable(pVar[j], &dblValue, &bstr);

                  ix[0] = j; ix[1] = i;
                  VariantInit(&varData);
                  varData.vt = VT_R8;
                  varData.dblVal = dblValue;
                  hr = SafeArrayPutElement(pRetSA, ix, &varData);

                  ix[0] = j; ix[1] += 1;
                  VariantInit(&varData);
                  varData.vt = VT_BSTR;
                  varData.bstrVal = bstr.Detach();
                  hr = SafeArrayPutElement(pRetSA, ix, &varData);
            }
      }

      delete[] pVar;
      V_VT(pValues) = VT_ARRAY | VT_VARIANT;
      V_ARRAY(pValues) = pRetSA;

      return S_OK;
}

STDMETHODIMP CMyClass::UnpackSafearray(VARIANT aVars, DWORD** pdwVars, DWORD* pdwArgs) {

      long lBound;
      bool bFailed = false;
      DWORD i;
      HRESULT hr;

      if (!(aVars.vt & VT_ARRAY) || ((aVars.vt & VT_TYPEMASK) != VT_R8))
            return E_INVALIDARG;

      SAFEARRAY* pSa = aVars.parray;

    if(SafeArrayGetDim(pSa) != 1) return E_INVALIDARG;

    hr = SafeArrayGetLBound(pSa, 1, &lBound);
    if(lBound != 0) return E_INVALIDARG;

    hr = SafeArrayGetUBound(pSa, 1, &lBound);
    *pdwArgs = lBound + 1;
      DWORD* pDwArr = new DWORD[*pdwArgs];

      for(i = 0; i < *pdwArgs; i++) {
        hr = SafeArrayGetElement(pSa, (long*)(&i), &pDwArr[i]);
        if(FAILED(hr)) {
                  delete[] pDwArr;
                  SafeArrayDestroy(pSa);
                  VariantClear(&aVars);
                  return hr;
            }
      }

      SafeArrayDestroy(pSa);
      VariantClear(&aVars);
      *pdwVars = pDwArr;
      return S_OK;
}


// example client code
            SAFEARRAY FAR* psa;
            SAFEARRAYBOUND rgsabound[1];
            DWORD i, n = 3;

            rgsabound[0].lLbound = 0;
            rgsabound[0].cElements = n;

            psa = SafeArrayCreate(VT_BSTR, 1, rgsabound);
            CComBSTR vars[] = {"LRST", "DSP_FRZB_EN", "AUTO_SYNC_STATE"};

            for(i = 0; i < n; ++i){
                  SafeArrayPutElement(psa, (long*)&i, vars[i]);
                  vars[i].Detach();
            }

            VARIANT pVariant;
            VariantInit(&pVariant);
            pVariant.parray = psa;
            pVariant.vt = VT_ARRAY | VT_BSTR;

            hr = pIMME->ReadWriteVariant(n, &pVariant);

            // extract out returned data
            SAFEARRAY *pra=V_ARRAY(&pVariant);

            long ix[2];
             VARIANT data;
            VariantInit(&data);

            ix[0] = 1; ix[1] = 1;
            hr=SafeArrayGetElement(pra,ix,&data);
            SafeArrayDestroy(pra);
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 54
deburging in oracle form 12 105
Sed question 2 103
Updating statistics with error notification email in SQL server 4 110
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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