How To Enumerate OLE and VB Controls from an OLE Control

转载 2006年05月26日 12:34:00
This article was previously published under Q141414

SUMMARY

For an OLE control to communicate with another control placed on the same Visual Basic form, the OLE control needs access to at least one interface pointer of the other control. This article illustrates a technique you can use to enumerate both OLE and Visual Basic controls present on a particular form and retrieve an interface pointer to these controls. Note that the same technique could be applied to enumerate controls placed on other control containers, provided that container exposes the functionality required to implement the technique.

MORE INFORMATION

Given a pointer to IOleClientSite, it is possible to enumerate through all of the other controls on a form by making use of the following interfaces:
IOleClientSite
IOleContainer
IEnumUnknown
IUnknown
IOleObject
IOleClientSite
IOleControlSite
Note that most of these interfaces are container-side interfaces, so the technique mentioned here is container dependent. For this method to work, the container must provide support for IOleContainer, which is currently defined as a mandatory interface in the OLE control container guidelines. Both MFC version 4.0 and greater and Visual Basic version 4.0 and greater OLE control containers provide support for this interface. Before using this method with another control container, ensure that it provides support for IOleContainer.

The method itself is illustrated by the sample code listed in this article. You can incorporate the code into an OLE control generated using ControlWizard. Use the code to enumerate all the controls, internal Visual Basic controls as well OLE controls, by calling IOleContainer::EnumObjects, and passing the following flags as its first parameter:
   OLECONTF_EMBEDDINGS: is used to retrieve OLE Controls.
   OLECONTF_OTHERS    : is used to retrieve other objects such as Visual
                        Basic internal controls.

   hr = lpContainer->EnumObjects(OLECONTF_EMBEDDINGS | OLECONTF_OTHERS,
                              &lpEnumUnk);
				
The differentiating aspect between OLE controls and other objects such as internal Visual Basic controls is that only OLE controls support the IOleObject interface. Hence, if a QueryInterface for IID_IOleObject fails for an object, then it is a different type of object. Also, if the control container provides support for Extended controls as does Visual Basic 4.0, the Extended control for a particular OLE control can also be retrieved using the method illustrated by the sample code.

Note that most of the functionality provided by the following sample code depends solely on the extent of the functionality exposed by the control container itself.

Also note that the LPOLECLIENTSITE pointer can be obtained through a call to COleControl::GetClientSite().

Sample Code

void EnumAllControlNames(LPOLECLIENTSITE lpSite)
{
   LPOLECONTAINER lpContainer;
   LPENUMUNKNOWN lpEnumUnk;

   // Note that the IOleContainer interface is currently defined as
   // mandatory. It must be implemented by control containers,
   // in the OLE Control Containers Guidelines.
   HRESULT hr = lpSite->GetContainer(&lpContainer);
   if(FAILED(hr)) {
      OutputDebugString(_T("Unable to get to the container./n"));
      return;
   }

   // OLECONTF_EMBEDDINGS is used to retrieve OLE Controls.
   // OLECONTF_OTHERS is used to retrieve other objects such as
   // Visual Basic internal controls
   hr = lpContainer->EnumObjects(
                       OLECONTF_EMBEDDINGS | OLECONTF_OTHERS,
                       &lpEnumUnk);
   if(FAILED(hr)) {
      lpContainer->Release();
      return;
   }

   LPUNKNOWN lpUnk;
   while (lpEnumUnk->Next(1, &lpUnk, NULL) == S_OK) {
      LPOLEOBJECT lpObject = NULL;
      LPOLECONTROLSITE lpTargetSite = NULL;
      LPOLECLIENTSITE lpClientSite = NULL;
      LPDISPATCH lpDisp;

      hr = lpUnk->QueryInterface(
                    IID_IOleObject, (LPVOID*)&lpObject);
      if(SUCCEEDED(hr)) {
         // This is an OLE control.
         // Navigate to the Extended Control because Visual Basic 4.0 uses
         // Extended controls.
         hr = lpObject->GetClientSite(&lpClientSite);
         if(SUCCEEDED(hr)) {
            // You have the IOleClientSite interface
            hr = lpClientSite->QueryInterface(
                       IID_IOleControlSite, (LPVOID*)&lpTargetSite);
            if(SUCCEEDED(hr)) {
               // You have the IOleControlSite interface
               // Get the IDispatch for the extended control.
               // Note that Extended controls are optional in the OLE
               // specifications for OLE Control Containers.
               hr = lpTargetSite->GetExtendedControl(&lpDisp);
            }
         }
      }
      else {
         // This is either an internal VB control or the
         // VB form itself.
         hr = lpUnk->QueryInterface(
                       IID_IDispatch, (LPVOID*)&lpDisp);
      }

      if(SUCCEEDED(hr)) {
         VARIANT va;
         VariantInit(&va);
         DISPID dispid;
         DISPPARAMS dispParams = { NULL, NULL, 0, 0 };

         // Get the names of all the controls present in a VB form.
         LPWSTR lpName[1] = { (WCHAR *)L"Name" };
         hr = lpDisp->GetIDsOfNames(IID_NULL, lpName, 1,
                                      LOCALE_SYSTEM_DEFAULT, &dispid);

         if(SUCCEEDED(hr)) {
            hr = lpDisp->Invoke(dispid/*0x80010000*/, IID_NULL,
                                  LOCALE_SYSTEM_DEFAULT,
                                  DISPATCH_PROPERTYGET |
                                  DISPATCH_METHOD,
                                  &dispParams, &va, NULL, NULL);
            if(SUCCEEDED(hr)) {
               CString szTmp((LPCWSTR)va.bstrVal);
               // szTmp now has the name.
               OutputDebugString(_T("And the name is ... ") + szTmp +
                                 _T("/n"));
            }
         }
         lpDisp->Release();
      }

      // Release interface pointers.
      if(lpObject)     lpObject->Release();
      if(lpTargetSite) lpTargetSite->Release();
      if(lpClientSite) lpClientSite->Release();

      lpUnk->Release();
   }    // End of While statement

   // Final clean up
   lpEnumUnk->Release();
   lpContainer->Release();
}
				
Additional words: VB VC visualc cdk ocx

MFC OLE拖放实现

最近研究了一下剪贴板,然后根据网上的 http://blog.csdn.net/sendy888/article/details/1747899和http://www.360doc.com/conte...
  • hebeixingxs
  • hebeixingxs
  • 2013年11月08日 12:49
  • 684

OLE、ActiveX、COM、ATL联系与区别

熟悉面向对象编程和网络编程的人一定对ActiveX、OLE和COM/DCOM这些概念不会陌生,但是它们之间究竟是什么样的关系,对许多们还是比较模糊的。在具体介绍它们的关系之间,我们还是先明确组件(Co...
  • lp310018931
  • lp310018931
  • 2015年09月19日 18:09
  • 1019

数据库连接技术之OLE DB

之前的博客介绍了ODBC和JDBC,这次简单的介绍一下OLE DB。ODBC的总结不知道是没贴到博客上还是不在这个博客上,我再找找,没有的话我再补充到时候。好了,开始吧。 回顾     之前呢...
  • zhuojiajin
  • zhuojiajin
  • 2014年07月25日 21:38
  • 1912

以OLE方式读写EXCEL的C++类【转载】

◆C++中的【L"https://msdn.microsoft.com/library"】。 这个是C++标准规定的写法。详见以下链接: http://bbs.csdn.net/topics/3906...
  • u013600225
  • u013600225
  • 2016年03月20日 08:21
  • 1292

从OLE到COM,再到ActiveX,再到.NET

微软从OLE到COM,再到ActiveX,再到.NET的发展历史的简介
  • just0kk
  • just0kk
  • 2016年03月02日 21:31
  • 750

当ABAP遇到OLE

ABAP 编程过程中难免遇到各种和微软EXCEL交互的场景,因此本人将EXCEL操作封装成类,方便调用 *———————————————————————* *&  ZEXCELCLASS *&—...
  • zhongguomao
  • zhongguomao
  • 2013年02月01日 13:31
  • 3433

VS2010 C++ 操作Excel表格的编程实现(OLE/COM)

转载请注明原文网址: http://www.cnblogs.com/xianyunhe/archive/2011/09/25/2190485.html 通过VC实现对Excel表格的操作的方法有多...
  • baidu_37503452
  • baidu_37503452
  • 2017年06月02日 11:18
  • 517

VC用OLE方式读写Excel

(转自:http://blog.csdn.net/qinghezhen/article/details/9906023)   前几天要做一个项目,需要读取Excel中的数据。从网上查资料发现,...
  • Xp275500976
  • Xp275500976
  • 2014年03月25日 10:55
  • 4792

MFC中使用OLE/COM操作EXCEL的方法

使用OLE的方法操作EXCEL,首先计算机必须安装excel,这样才会有接口暴露出来。本次使用大神封装好的类。 excel作为OLE/COM库插件,定义好了各类交互接口,而且这些接口是跨语言的,可以导...
  • lht501692913
  • lht501692913
  • 2015年12月17日 19:25
  • 2301

SQL那些事儿(十一)--ODBC,OLE-DB,ADO.NET区别

一、ODBC ODBC的由来 1992年Microsoft和Sybase、Digital共同制定了ODBC标准接口,以单一的ODBC API来存取各种不同的数据库。随后ODBC便获得了许多数据库厂...
  • xcymorningsun
  • xcymorningsun
  • 2016年11月08日 16:32
  • 1179
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:How To Enumerate OLE and VB Controls from an OLE Control
举报原因:
原因补充:

(最多只允许输入30个字)