tlbinfo.h

Go to the documentation of this file.
00001 00004 /* 00005 * Copyright © 2002 Michael Geddes. 00006 * 00007 * This material is provided "as is", with absolutely no warranty 00008 * expressed or implied. Any use is at your own risk. Permission to 00009 * use or copy this software for any purpose is hereby granted without 00010 * fee, provided the above notices are retained on all copies. 00011 * Permission to modify the code and to distribute modified code is 00012 * granted, provided the above notices are retained, and a notice that 00013 * the code was modified is included with the above copyright notice. 00014 * 00015 * This header is part of comet. 00016 * http://www.lambdasoft.dk/comet 00017 */ 00018 00019 #ifndef COMET_TLIBINFO_H 00020 #define COMET_TLIBINFO_H 00021 00022 #ifdef _SHOW_INC 00023 #pragma message(" #Include " __FILE__) 00024 #endif 00025 00026 #include <oaidl.h> 00027 #include <comet/ptr.h> 00028 #include <comet/uuid.h> 00029 00030 namespace comet 00031 { 00032 template<> struct comtype<ITypeLib> 00033 { 00034 static const IID& uuid() throw() { return IID_IPictureDisp; } 00035 typedef ::IUnknown base; 00036 }; 00037 template<> struct comtype<ITypeInfo> 00038 { 00039 static const IID& uuid() throw() { return IID_ITypeInfo; } 00040 typedef ::IUnknown base; 00041 }; 00042 template<> struct comtype<ITypeInfo2> 00043 { 00044 static const IID& uuid() throw() { return IID_ITypeInfo2; } 00045 typedef ::ITypeInfo base; 00046 }; 00050 namespace impl 00051 { 00053 template< typename B, typename T> 00054 struct tlib_info 00055 { 00056 typedef void (__stdcall B::*RelFunc)(T*); 00057 template< RelFunc RELEASE> 00058 struct typeinfo_attr_base 00059 { 00060 public: 00061 typeinfo_attr_base() : info_(NULL), attrib_(NULL), rc_(NULL) {} 00062 typeinfo_attr_base( const com_ptr<B> &info, T *attrib ) : info_(info),attrib_(attrib),rc_(NULL) 00063 { 00064 if (attrib != NULL) 00065 { 00066 rc_ = new long; 00067 (*rc_)=1; 00068 } 00069 } 00070 typeinfo_attr_base( const typeinfo_attr_base &base) 00071 : info_(base.info_), attrib_(base.attrib_), rc_(base.rc_) 00072 { 00073 ++*rc_; 00074 } 00075 ~typeinfo_attr_base() 00076 { 00077 release(); 00078 } 00079 typeinfo_attr_base &operator =( const typeinfo_attr_base &base) 00080 { 00081 release(); 00082 info_= base.info_; 00083 attrib_= base.attrib_; 00084 rc_ = base.rc_; 00085 ++*rc_; 00086 return *this; 00087 } 00088 void release() 00089 { 00090 if(attrib_ != NULL && rc_ != NULL ) 00091 { 00092 if (0 == --*rc_) 00093 { 00094 (info_.raw()->*RELEASE)( attrib_); 00095 delete rc_; 00096 } 00097 rc_ = NULL; 00098 attrib_ = NULL; 00099 info_ = NULL; 00100 } 00101 } 00102 00103 00104 T *operator->() const 00105 { 00106 if( attrib_ ==NULL ) throw com_error(E_POINTER); 00107 return attrib_; 00108 } 00109 bool is_null() const { return attrib_ == NULL; } 00110 protected: 00111 long *rc_; 00112 T *attrib_; 00113 com_ptr<B> info_; 00114 }; 00115 }; 00116 }; 00117 00119 typedef impl::tlib_info<ITypeInfo, TYPEATTR>::typeinfo_attr_base< &ITypeInfo::ReleaseTypeAttr > type_attr_t; 00121 typedef impl::tlib_info<ITypeInfo, FUNCDESC>::typeinfo_attr_base< &ITypeInfo::ReleaseFuncDesc > func_desc_t; 00123 typedef impl::tlib_info<ITypeInfo, VARDESC>::typeinfo_attr_base< &ITypeInfo::ReleaseVarDesc > var_desc_t; 00124 00126 typedef impl::tlib_info<ITypeLib, TLIBATTR>::typeinfo_attr_base< &ITypeLib::ReleaseTLibAttr > tlibattr_t; 00127 00128 00132 enum impl_type_flag 00133 { 00134 eft_default=0x1, 00135 eft_source =0x2, 00136 eft_restricted=0x4, 00137 eft_defaultvtable=0x8 00138 }; 00139 00145 template<> 00146 struct wrap_t<ITypeInfo> 00147 { 00148 private: 00149 inline ITypeInfo *raw_(){ return reinterpret_cast<ITypeInfo *>(this); } 00150 public: 00151 00155 type_attr_t GetTypeAttr() 00156 { 00157 TYPEATTR *pTypeAttr; 00158 raw_()->GetTypeAttr( &pTypeAttr) | raise_exception ; 00159 return type_attr_t( raw_(), pTypeAttr); 00160 } 00161 00163 com_ptr<ITypeComp> GetTypeComp() 00164 { 00165 com_ptr<ITypeComp> ppTComp; 00166 raw_()->GetTypeComp( ppTComp.out()) | raise_exception; 00167 return ppTComp; 00168 } 00169 00173 func_desc_t GetFuncDesc( unsigned int index) 00174 { 00175 FUNCDESC *pFuncDesc ; 00176 raw_()->GetFuncDesc(index, &pFuncDesc) | raise_exception ; 00177 return func_desc_t( raw_(), pFuncDesc); 00178 } 00179 00183 var_desc_t GetVarDesc( unsigned int index ) 00184 { 00185 VARDESC *pVarDesc; 00186 raw_()->GetVarDesc( index,&pVarDesc) | raise_exception; 00187 return var_desc_t(raw_(), pVarDesc); 00188 } 00189 00190 00191 #ifdef NOT_YET 00192 std::list<std::string> GetNames( MEMBERID memid ) 00193 { 00194 /* [local] */ HRESULT GetNames( /* [in] */ MEMBERID memid, /* [length_is][size_is][out] */ BSTR *rgBstrNames, 00195 /* [in] */ UINT cMaxNames, /* [out] */ UINT *pcNames) ; 00196 } 00197 #endif // 00198 00200 HREFTYPE GetRefTypeOfImplType( unsigned int index) 00201 { 00202 HREFTYPE reftype; 00203 raw_()->GetRefTypeOfImplType(index, &reftype) | raise_exception; 00204 return reftype; 00205 } 00206 00208 long GetImplTypeFlags( int index) 00209 { 00210 INT implTypeFlags; 00211 raw_()->GetImplTypeFlags( index, &implTypeFlags) |raise_exception; 00212 return implTypeFlags; 00213 } 00214 00215 #ifdef NOT_YET 00216 std::vector<MEMBERID> GetIDsOfNames( const std::vector<bstr_t> &rgsNames); 00217 { 00218 /* [local] */ HRESULT GetIDsOfNames( 00219 /* [size_is][in] */ LPOLESTR *rgszNames, 00220 /* [in] */ UINT cNames, 00221 /* [size_is][out] */ MEMBERID *pMemId) ; 00222 } 00223 00224 /* [local] */ HRESULT Invoke( 00225 /* [in] */ PVOID pvInstance, 00226 /* [in] */ MEMBERID memid, 00227 /* [in] */ WORD wFlags, 00228 /* [out][in] */ DISPPARAMS *pDispParams, 00229 /* [out] */ VARIANT *pVarResult, 00230 /* [out] */ EXCEPINFO *pExcepInfo, 00231 /* [out] */ UINT *puArgErr) ; 00232 #endif // NOT_YET 00233 00234 HRESULT GetIDsOfNames( LPOLESTR *rgszNames, UINT cNames, 00235 MEMBERID *pMemId) 00236 { 00237 return raw_()->GetIDsOfNames( rgszNames, cNames, pMemId); 00238 } 00239 00241 HRESULT Invoke( PVOID pvInstance, MEMBERID memid, WORD wFlags, 00242 DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, 00243 UINT *puArgErr) 00244 { 00245 return raw_()->Invoke( pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr); 00246 } 00247 00248 bool GetIDOfName( const wchar_t *name, MEMBERID *id) 00249 { 00250 wchar_t *ucname = const_cast<wchar_t *>(name); 00251 return SUCCEEDED(raw_()->GetIDsOfNames( &ucname, 1, id)); 00252 } 00253 00254 00256 void GetDocumentation(MEMBERID memid, bstr_t *name, bstr_t *docString, DWORD *helpcontext, bstr_t *helpfile) 00257 { 00258 raw_()->GetDocumentation(memid, name?name->out():NULL, docString?docString->out():NULL, 00259 helpcontext, helpfile? helpfile->out():NULL) | raise_exception; 00260 } 00265 bstr_t GetMemberName(MEMBERID memid) 00266 { 00267 bstr_t name; 00268 raw_()->GetDocumentation(memid, name.out(), NULL, NULL, NULL) | raise_exception; 00269 return name; 00270 } 00271 bstr_t GetName() 00272 { 00273 return GetMemberName(-1); 00274 } 00275 00276 #if NOT_YET 00277 /* [local] */ HRESULT GetDllEntry( 00278 /* [in] */ MEMBERID memid, 00279 /* [in] */ INVOKEKIND invKind, 00280 /* [out] */ BSTR *pBstrDllName, 00281 /* [out] */ BSTR *pBstrName, 00282 /* [out] */ WORD *pwOrdinal) ; 00283 #endif // NOT_YET 00284 00287 com_ptr<ITypeInfo> GetRefTypeInfo( HREFTYPE refType) 00288 { 00289 com_ptr<ITypeInfo> refinfo; 00290 raw_()->GetRefTypeInfo( refType, refinfo.out()) | raise_exception; 00291 return refinfo; 00292 } 00293 00294 #if NOT_YET 00295 /* [local] */ HRESULT AddressOfMember( /* [in] */ MEMBERID memid, /* [in] */ INVOKEKIND invKind, 00296 /* [out] */ PVOID *ppv) ; 00297 00298 /* [local] */ HRESULT CreateInstance( /* [in] */ IUnknown *pUnkOuter, /* [in] */ REFIID riid, 00299 /* [iid_is][out] */ PVOID *ppvObj) ; 00300 00301 HRESULT GetMops( /* [in] */ MEMBERID memid, /* [out] */ BSTR *pBstrMops) ; 00302 #endif // NOT_YET 00303 00307 std::pair< com_ptr<ITypeLib>, UINT > GetContainingTypeLib() 00308 { 00309 std::pair< com_ptr<ITypeLib>, UINT> result; 00310 raw_()->GetContainingTypeLib( result.first.out(), &(result.second) ) | raise_exception; 00311 return result; 00312 } 00313 }; 00315 00316 template<> 00317 struct wrap_t<ITypeInfo2> : wrap_t<ITypeInfo> 00318 { 00319 TYPEKIND GetTypeKind() 00320 { 00321 TYPEKIND tkind; 00322 raw_()->GetTypeKind(&tkind) | raise_exception; 00323 return tkind; 00324 } 00325 private: 00326 inline ITypeInfo2 *raw_(){ return reinterpret_cast<ITypeInfo2 *>(this); } 00327 }; 00328 00329 00333 00335 template<> 00336 struct wrap_t<ITypeLib> 00337 { 00338 inline ITypeLib *raw_(){ return reinterpret_cast<ITypeLib *>(this); } 00339 unsigned int GetTypeInfoCount() 00340 { 00341 return raw_()->GetTypeInfoCount(); 00342 } 00343 00345 com_ptr<ITypeInfo> GetTypeInfo( UINT index) 00346 { 00347 com_ptr<ITypeInfo> tinfo; 00348 raw_()->GetTypeInfo( index, tinfo.out()) | raise_exception; 00349 return tinfo; 00350 } 00351 00353 TYPEKIND GetTypeInfoType( UINT index) 00354 { 00355 TYPEKIND retval; 00356 raw_()->GetTypeInfoType( index, &retval) | raise_exception; 00357 return retval; 00358 } 00359 00361 com_ptr<ITypeInfo> GetTypeInfoOfGuid( const uuid_t &guid ) 00362 { 00363 com_ptr<ITypeInfo> tinfo; 00364 raw_()->GetTypeInfoOfGuid( guid, tinfo.out()) | raise_exception; 00365 return tinfo; 00366 } 00368 HRESULT GetTypeInfoOfGuid( REFGUID guid, ITypeInfo **ppTinfo) 00369 { 00370 return raw_()->GetTypeInfoOfGuid( guid,ppTinfo); 00371 } 00372 00374 tlibattr_t GetLibAttr() 00375 { 00376 TLIBATTR *attr; 00377 raw_()->GetLibAttr( &attr) | raise_exception; 00378 return tlibattr_t( raw_(), attr); 00379 } 00380 00381 com_ptr<ITypeComp> GetTypeComp() 00382 { 00383 com_ptr<ITypeComp> typecomp; 00384 raw_()->GetTypeComp( typecomp.out() ) | raise_exception; 00385 return typecomp; 00386 } 00387 00388 void GetDocumentation(int index, bstr_t *name, bstr_t *docString, DWORD *helpcontext, bstr_t *helpfile) 00389 { 00390 raw_()->GetDocumentation(index, name?name->out():NULL, docString?docString->out():NULL, 00391 helpcontext, helpfile? helpfile->out():NULL) | raise_exception; 00392 } 00393 bstr_t GetItemName(int index) 00394 { 00395 bstr_t name; 00396 raw_()->GetDocumentation(index, name.out(), NULL, NULL, NULL) | raise_exception; 00397 return name; 00398 } 00399 bstr_t GetName() 00400 { 00401 return GetItemName(-1); 00402 } 00403 00404 bool IsName( const bstr_t &name, unsigned long hashval ) 00405 { 00406 BOOL ret; 00407 raw_()->IsName( name.in(), hashval, &ret) | raise_exception; 00408 return ret!=0; 00409 } 00410 00411 std::pair<com_ptr<ITypeInfo>, MEMBERID> FindName( const bstr_t &name) 00412 { 00413 std::pair<com_ptr<ITypeInfo>, MEMBERID> result; 00414 USHORT tofind = 1; 00415 raw_()->FindName(name.in(), 0, result.first.out(), &result.second, &tofind) | raise_exception; 00416 return result; 00417 } 00418 #ifdef NOT_YET 00419 std::list<std::pair<com_ptr<ITypeInfo>, MEMBERID> >FindName( const bstr_t &name, int max) 00420 {} 00421 [local] 00422 HRESULT FindName( 00423 [in, out] LPOLESTR szNameBuf, 00424 [in] ULONG lHashVal, 00425 [out,size_is(*pcFound),length_is(*pcFound)] ITypeInfo **ppTInfo, 00426 [out,size_is(*pcFound),length_is(*pcFound)] MEMBERID * rgMemId, 00427 [in, out] USHORT * pcFound 00428 ); 00429 #endif // LATER 00430 00431 00432 }; 00433 00435 namespace typeinfo 00436 { 00440 enum regkind_t { reg_default = REGKIND_DEFAULT, reg_register = REGKIND_REGISTER, reg_none = REGKIND_NONE } ; 00442 static inline com_ptr<ITypeLib> LoadTypeLib( const bstr_t &filename, regkind_t regkind= reg_default) 00443 { 00444 com_ptr<ITypeLib> tlib; 00445 LoadTypeLibEx(filename.in(), (REGKIND)(regkind), tlib.out()) | raise_exception; 00446 return tlib; 00447 } 00448 } 00450 } // namespace comet 00451 #endif /* COMET_TLIBINFO_H */