C++类的动态组件化技术(6)
作者:佚名; 更新时间:2014-12-05

  3.   C++参数类型的自动化包装

  在本文的技术方案中,C++类的public方法与ATL组件接口中的方法一一对应;相应的,C++类中方法的参数类型也要转换为COM规范所允许的数据类型。

  在基于COM的自动化(Automation)技术中,Microsoft提供了一套自动化兼容的数据类型VARIANT,定义如下:

  typedef struct FARSTRUCT tagVARIANT VARIANT;

  typedef struct FARSTRUCT tagVARIANT VARIANTARG;

  typedef struct tagVARIANT {

       VARTYPE                        vt;

       unsigned short                  wReserved1;

       unsigned short                  wReserved2;

       unsigned short                  wReserved3;

       union {

              Byte                         bVal;                               // VT_UI1.

              Short                        iVal;                                // VT_I2.

              long                          lVal;                                // VT_I4.

              float                         fltVal;                              // VT_R4.

              double                      dblVal;                            // VT_R8.

              VARIANT_BOOL      boolVal;                           // VT_BOOL.

              SCODE                    scode;                            // VT_ERROR.

              CY                           cyVal;                             // VT_CY.

              DATE                       date;                               // VT_DATE.

              BSTR                       bstrVal;                           // VT_BSTR.

              DECIMAL                FAR* pdecVal;                 // VT_BYREF|VT_DECIMAL.

              IUnknown                 FAR* punkVal;                 // VT_UNKNOWN.

              IDispatch                  FAR* pdispVal;                // VT_DISPATCH.

              SAFEARRAY            FAR* parray;                   // VT_ARRAY|*.

              Byte                         FAR* pbVal;                    // VT_BYREF|VT_UI1.

              short                        FAR* piVal;                     // VT_BYREF|VT_I2.

              long                          FAR* plVal;                     // VT_BYREF|VT_I4.

              float                         FAR* pfltVal;                   // VT_BYREF|VT_R4.

              double                      FAR* pdblVal;                  // VT_BYREF|VT_R8.

              VARIANT_BOOL      FAR* pboolVal;                // VT_BYREF|VT_BOOL.

              SCODE                    FAR* pscode;                  // VT_BYREF|VT_ERROR.

              CY                           FAR* pcyVal;                  // VT_BYREF|VT_CY.

              DATE                       FAR* pdate;                    // VT_BYREF|VT_DATE.

              BSTR                       FAR* pbstrVal;                // VT_BYREF|VT_BSTR.

              IUnknown                 FAR* FAR* ppunkVal;      // VT_BYREF|VT_UNKNOWN.

              IDispatch                  FAR* FAR* ppdispVal;     // VT_BYREF|VT_DISPATCH.

              SAFEARRAY            FAR* FAR* pparray         // VT_ARRAY|*.

              VARIANT                 FAR* pvarVal;                 // VT_BYREF|VT_VARIANT.

              void                          FAR* byref;                    // Generic ByRef.

              char                         cVal;                               // VT_I1.

              unsigned short           uiVal;                              // VT_UI2.

              unsigned long            ulVal;                              // VT_UI4.

              int                            intVal;                             // VT_INT.

              unsigned int               uintVal;                           // VT_UINT.

              char FAR *               pcVal;                             // VT_BYREF|VT_I1.

              unsigned short FAR * puiVal;                            // VT_BYREF|VT_UI2.

              unsigned long FAR *  pulVal;                            // VT_BYREF|VT_UI4.

              int FAR *                  pintVal;                           // VT_BYREF|VT_INT.

              unsigned int FAR *     puintVal;                          // VT_BYREF|VT_UINT.

       };

};

  我们看到,所有简单数据类型都可以在VARIANT中找到对应的定义,但是,在多数的基于C++的系统设计中,方法参数不会仅仅出现简单数据类型,类对象、对象引用、对象指针被频繁的作为参数来传递。

  以类对象、对象引用或对象指针形式存在的参数,我们称为复杂类型参数。在技术方案中,所有复杂类型参数在ATL接口方法中一律对应接口指针,我们需要提供C++对象(或引用、指针)和ATL接口指针之间的动态转换功能。下文就复杂类型作为传入、传出参数分别进行讨论。

核心期刊快速发表
Copyright@2000-2030 论文期刊网 Corporation All Rights Reserved.
《中华人民共和国信息产业部》备案号:ICP备07016076号;《公安部》备案号:33010402003207
本网站专业、正规提供职称论文发表和写作指导服务,并收录了海量免费论文和数百个经国家新闻出版总署审批过的具有国内统一CN刊号与国际标准ISSN刊号的合作期刊,供诸位正确选择和阅读参考,免费论文版权归原作者所有,谨防侵权。联系邮箱:256081@163.com