Help and Support

How to call a hidden default method of an OLE automation collection in Visual C++

Article ID:152071
Last Review:November 21, 2006
Revision:3.1
This article was previously published under Q152071
On This Page

SUMMARY

A technical article found on the Microsoft Developer Network CD-ROM titled "Implementing OLE Automation Collections," describes the functionality necessary to create an OLE automation collection. This includes creating a method called Item that returns the dispatch interface of the indicated item in the collection.

All OLE automation collection objects must implement the Item method to iterate through the objects of the collection. The implementation of the method might have been created hidden in the .ODL file for some applications. You cannot find these hidden methods by calling IDispatch::GetIdsOfNames(). However, in many situations, this hidden method is the default method of the collection, in which case you can invoke it with an dispatch ID of DISPID_VALUE.

Visual Basic 3.0 depends on this Item method being the default method of the collection object.

Back to the top

MORE INFORMATION

The following code sample illustrates using COleDispatchDriver::InvokeHelper() to call the Item method from the Resources collection class provided by Microsoft Project 4.1. The Item method is hidden and cannot be found in the type library of the object:

Back to the top

Sample Code

   void IterateCollection(void)
   {
      Application App; // wrapper class for the 'Application' object
      Project Proj;    // wrapper class for the 'Project' object
      Resources Res;   // wrapper class for the 'Resources' object
      CString lpszFileName ("c:\\temp\\project2.mpp");

      App.CreateDispatch("MSProject.Application");

      VARIANT vFileName;
      VariantInit(&vFileName);
      V_VT(&vFileName) = VT_BSTR;
      V_BSTR(&vFileName) = lpszFileName.AllocSysString();
      App.FileOpen(vFileName);

      Proj.AttachDispatch(App.GetActiveProject());
      Res.AttachDispatch(Proj.GetResources());

      VARIANT vCount;
      VariantInit(&vCount);
      V_VT(&vCount) = VT_I4;
      vCount = Res.Count();

      // the index of the collection starts with 1, not 0
      for (int i = 1 ; i <= vCount.lVal ; i++)
      {
         Resource r; // wrapper class for the 'Resource' object

         VARIANT vResult, vIndex, vStr;
         VariantInit(&vResult);
         VariantInit(&vIndex);
         V_VT(&vIndex) = VT_I2;
         V_I2(&vIndex) = i;
         static BYTE parms[] = VTS_VARIANT;

         Res.InvokeHelper(DISPID_VALUE, DISPATCH_METHOD, VT_VARIANT,
   (void*)&vResult, parms, &vIndex);
         r.AttachDispatch(vResult.pdispVal);

         vStr = r.GetName();
         // ... 'vStr' now contains the Name property
      }
   }
				

Back to the top


APPLIES TO
Microsoft Foundation Class Library 4.2, when used with:
  Microsoft Visual C++ 2.0 Professional Edition
  Microsoft Visual C++ 2.1
  Microsoft Visual C++ 2.2
  Microsoft Visual C++ 4.0 Standard Edition
  Microsoft Visual C++ 4.1 Subscription
  Microsoft Visual C++ 4.2 Enterprise Edition
  Microsoft Visual C++ 5.0 Enterprise Edition
  Microsoft Visual C++ 6.0 Enterprise Edition
  Microsoft Visual C++ 4.2 Professional Edition
  Microsoft Visual C++ 5.0 Professional Edition
  Microsoft Visual C++ 6.0 Professional Edition
  Microsoft Visual C++, 32-bit Learning Edition 6.0

Back to the top

Keywords: 
kbhowto kbinfo kbautomation kbcode KB152071

Back to the top

Article Translations

 

Other Support Options

  • Contact Microsoft
    Phone Numbers, Support Options and Pricing, Online Help, and more.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.