困扰了好几天了Fun(CArray<MyStruct>& arr)
{
arr.Add(newStruct);
arr.Add(newStruct);
arr.Add(newStruct);
}调用处
CArray<MyStrunt> arr
Fun(arr);
for (i++)
printf(, arr[i].xxxx)想实现一个等价的(可以增加参数),可以在vb、delphi中使用的方法!因为是在原有代码基础上改的,所以请不要说把接口换成GetCount、GetItem的形势下面是已经看了和试验过的链接,麻烦大家不要再次贴出,先谢过!很棘手,分不过可以再给
  MSDN->periodical->periodical   1996->Microsoft   system   journal   ->November->Q&A   Active   X/COM   http://www.codeguru.com/cpp/com-tech/activex/misc/article.php/c2671/
http://www.codeguru.com/Cpp/COM-Tech/activex/misc/article.php/c2567
http://www.codeguru.com/Cpp/COM-Tech/activex/com/article.php/c2577
http://www.codeguru.com/Cpp/COM-Tech/activex/com/article.php/c2575

解决方案 »

  1.   

    其实关键是怎样在vb、delphi中调用variant方式我试过了,但是在vb、delphi怎么调用就不太清楚了
      

  2.   

    How do I return an array of items from my control?  
     --------------------------------------------------------------------------------
     
    To return an array of items, you can use an OLE Automation SafeArray. There isn't room to discuss all of the features of SafeArrays here, so I'll just briefly cover how to use them in an Automation method. There is a tremendous amount of documentation that comes with VC++ that covers the various SafeArray APIs, etc. Add a method to your control that takes a VARIANT pointer as a parameter. A variant is a generic data type that can hold values or pointers to other more specific OLE automation types. One of the data types that can be contained within a variant is a SafeArray. You can have an array of shorts, longs, BSTRs, Dates, etc. In the method you need to allocate a SAFEARRAY, allocate space for the items, and then populate the array with these values. You then need to initialize the VARIANT structure. Here's some code that creates a SAFEARRAY of BSTR elements As with all automation data, the server allocates the storage and the client (e.g., VB) is responsible for the deallocation. void CMyControl::GetArray( VARIANT FAR* pVariant )
    {
       // Get the number of items
       int nCount = GetCount();   // Create a safe array of type BSTR
       // cElements of the first item indicates the size of the array
       SAFEARRAYBOUND saBound[1];
       SAFEARRAY* pSA;   saBound[0].cElements = nCount;
       saBound[0].lLbound = 0;
       pSA = SafeArrayCreate( VT_BSTR, 1, saBound );   for( long i = 0; i < nCount; i++ )
       {
          BSTR bstr;      // Get the next item, create a BSTR, and
          // stuff it in the array. GetItem returns a CString.
          bstr = GetItem( i ).AllocSysString();
          SafeArrayPutElement( pSA, &i, bstr );
          ::SysFreeString( bstr );
       }   // Init the variant
       VariantInit( pVariant );   // Specify its type and value
       pVariant->vt = VT_ARRAY | VT_BSTR;
       pVariant->parray = pSA;
    }
    The Visual Basic code to access the elements of the array would look something like this:     Dim t As Variant
        Dim i as Integer    MyControl1.GetArray t    For i = 0 To MyControl1.Count - 1
            ListBox.AddItem  t( i )
        Next i
     
     
     
     
     
      

  3.   

    zottff() 
    如果数组里面保存的是个结构体vb怎么写??
      

  4.   

    对于vb我也不熟悉,你试试:Dim t As Variant
    Dim i as Integer
    Dim aa  as MyStructFor i = 0 To MyControl1.Count - 1
        let a = t( i )
    Next i
      

  5.   

    如何向ATL-COM对象传送一个数组?
     
    我想创建一个函数来向ATL-COM对象传送数组.
    如下代码的方法用于ACTIVEX中,可能对ATL-COM也有启发吧.
    CoInitialize(NULL);
     CLSID m_clsid;
     USES_CONVERSION;
     ::CLSIDFromString(T2OLE("ROUNDANALOG.RoundAnlgAARCtrl.1"), &m_clsid);
     IDispatch FAR* pObj = (IDispatch FAR*)NULL;
     CString str = "UpdateControl";
     BSTR bstr = str.AllocSysString();
     HRESULT hr = CoCreateInstance(m_clsid, NULL, CLSCTX_ALL, IID_IDispatch,
    (void**)&pObj);
     SafeArrayAccessData(psa, (void**)&bstrArray);
     bstrArray[0] = str.AllocSysString();
     bstrArray[1] = str.AllocSysString();
     SafeArrayUnaccessData(psa);
    VARIANTARG* pvars = new VARIANTARG[1];
     VariantInit(&pvars[0]);
     pvars[0].vt = VT_ARRAY|VT_BYREF|VT_BSTR;
     pvars[0].pparray = &psa;
     DISPID dispid;
     hr = pObj->GetIDsOfNames(IID_NULL, &bstr, 1,LOCALE_USER_DEFAULT, &dispid);
    DISPPARAMS disp = {pvars, &dispid, 1,1};
     hr = pObj->Invoke(dispid, IID_NULL,
    LOCALE_USER_DEFAULT,DISPATCH_PROPERTYPUT,&disp,NULL, NULL, NULL);
     delete[] pvars;
     pObj->Release();
     CoUninitialize();
    在你的控制中建立如下并变量参考:
    void CRoundAnlgAARCtrl::SaveFunc(const VARIANT FAR& var)
    {
     // TODO: Add your dispatch handler code here
     ASSERT(var.vt == VT_ARRAY | VT_BYREF | VT_BSTR);
     SAFEARRAY* psa = *var.pparray;