Solved

Problem returning a VARIANT with a SAFEARRAY of VARIANTs

Posted on 2000-04-10
21
1,408 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
Comment Utility
Are you calling SafeArrayUnaccessData()?

 > the ATL macros that define the array

What macros are you talking about?

..B ekiM
0
 

Author Comment

by:careta
Comment Utility
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
Comment Utility
> 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
 
LVL 5

Accepted Solution

by:
vachooho earned 100 total points
Comment Utility
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
Comment Utility
Adjusted points from 100 to 200
0
 

Author Comment

by:careta
Comment Utility
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
Comment Utility
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
Comment Utility
Hi try using
hres = VariantCopy(pValues, pRetSA);
check for hres
Hope this helps
inpras
0
 
LVL 5

Expert Comment

by:vachooho
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 5

Expert Comment

by:vachooho
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
> 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Community Support has reduced points from 200 to 100
0
 
LVL 3

Expert Comment

by:darinw
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now