Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

ComObject.cpp

Go to the documentation of this file.
00001 //
00002 // ComObject.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "ComObject.h"
00025 #include "ShareazaOM.h"
00026 
00027 #ifdef _DEBUG
00028 #define new DEBUG_NEW
00029 #undef THIS_FILE
00030 static char THIS_FILE[] = __FILE__;
00031 #endif
00032 
00033 IMPLEMENT_DYNCREATE(CComObject, CCmdTarget)
00034 IMPLEMENT_OLETYPELIB(CComObject, LIBID_Shareaza, 1, 0);
00035 
00036 
00038 // CComObject construction
00039 
00040 CComObject::CComObject()
00041 {
00042         EnableTypeLib();
00043         m_pCLSID = NULL;
00044 }
00045 
00046 CComObject::~CComObject()
00047 {
00048 }
00049 
00051 // CComObject operations
00052 
00053 BOOL CComObject::EnableDispatch(REFIID pIID)
00054 {
00055         LPUNKNOWN pUnknown = CCmdTarget::GetInterface( &pIID );
00056 
00057         if ( !pUnknown ) return FALSE;
00058 
00059         m_pDispatchMap.SetAt( pUnknown, (LPVOID)&pIID );
00060 
00061         return TRUE;
00062 }
00063 
00064 LPUNKNOWN CComObject::GetInterface(REFIID pIID, BOOL bAddRef)
00065 {
00066         LPUNKNOWN pInterface = CCmdTarget::GetInterface( &pIID );
00067 
00068         if ( !pInterface && pIID == IID_IDispatch )
00069         {
00070                 if ( POSITION pos = m_pDispatchMap.GetStartPosition() )
00071                 {
00072                         IID* pDispIID;
00073                         m_pDispatchMap.GetNextAssoc( pos, (void*&)pInterface, (void*&)pDispIID );
00074                 }
00075         }
00076 
00077         if ( pInterface && bAddRef ) pInterface->AddRef();
00078         return pInterface;
00079 }
00080 
00081 LPDISPATCH CComObject::GetDispatch(BOOL bAddRef)
00082 {
00083         return (LPDISPATCH)CComObject::GetInterface( IID_IDispatch, bAddRef );
00084 }
00085 
00087 // CComObject IUnknown implementation
00088 
00089 STDMETHODIMP_(ULONG) CComObject::ComAddRef(LPUNKNOWN pUnk)
00090 {
00091         return ExternalAddRef();
00092 }
00093 
00094 STDMETHODIMP_(ULONG) CComObject::ComRelease(LPUNKNOWN pUnk)
00095 {
00096         return ExternalRelease();
00097 }
00098 
00099 STDMETHODIMP CComObject::ComQueryInterface(LPUNKNOWN pUnk, REFIID iid, LPVOID* ppvObj)
00100 {
00101         if ( iid == IID_IDispatch )
00102         {
00103                 IID* pIID;
00104                 if ( m_pDispatchMap.Lookup( pUnk, (void*&)pIID ) )
00105                 {
00106                         *ppvObj = pUnk;
00107                         ComAddRef( pUnk );
00108                         return S_OK;
00109                 }
00110         }
00111 
00112         return ExternalQueryInterface( &iid, ppvObj );
00113 }
00114 
00116 // CComObject IDispatch implementation
00117 
00118 STDMETHODIMP CComObject::ComGetTypeInfoCount(LPUNKNOWN pUnk, UINT FAR* pctinfo)
00119 {
00120         if ( !pctinfo ) return E_INVALIDARG;
00121         *pctinfo = GetTypeInfoCount();
00122         return NOERROR;
00123 }
00124 
00125 STDMETHODIMP CComObject::ComGetTypeInfo(LPUNKNOWN pUnk, UINT itinfo, LCID lcid,
00126                                                                          ITypeInfo FAR* FAR* pptinfo)
00127 {
00128         if ( !pptinfo ) return E_INVALIDARG;
00129         if ( itinfo != 0 ) return DISP_E_BADINDEX;
00130 
00131         IID* pIID;
00132         if ( !m_pDispatchMap.Lookup( pUnk, (void*&)pIID ) ) return E_INVALIDARG;
00133 
00134         return GetTypeInfoOfGuid( lcid, *pIID, pptinfo );
00135 }
00136 
00137 STDMETHODIMP CComObject::ComGetIDsOfNames(      LPUNKNOWN pUnk, REFIID riid,
00138                                                                                         OLECHAR FAR* FAR* rgszNames,
00139                                                                                         UINT cNames, LCID lcid,
00140                                                                                         DISPID FAR* rgdispid)
00141 {
00142         if ( riid != IID_NULL ) return DISP_E_UNKNOWNINTERFACE;
00143         if ( !rgszNames || cNames < 1 ) return E_INVALIDARG;
00144         if ( !rgdispid ) return E_INVALIDARG;
00145 
00146         LPTYPEINFO pTypeInfo;
00147         SCODE sc;
00148 
00149         sc = ComGetTypeInfo( pUnk, 0, lcid, &pTypeInfo );
00150 
00151         if ( SUCCEEDED( sc ) )
00152         {
00153                 sc = pTypeInfo->GetIDsOfNames( rgszNames, cNames, rgdispid );
00154                 if ( sc == TYPE_E_ELEMENTNOTFOUND ) sc = DISP_E_UNKNOWNNAME;
00155                 pTypeInfo->Release();
00156         }
00157 
00158         return sc;
00159 }
00160 
00161 STDMETHODIMP CComObject::ComInvoke(     LPUNKNOWN pUnk, DISPID dispidMember, REFIID riid,
00162                                                                         LCID lcid, WORD wFlags, DISPPARAMS FAR* pdispparams,
00163                                                                         VARIANT FAR* pvarResult, EXCEPINFO FAR* pexcepinfo,
00164                                                                         UINT FAR* puArgErr)
00165 {
00166         if ( pdispparams == NULL ) return E_INVALIDARG;
00167         if ( riid != IID_NULL ) return DISP_E_UNKNOWNINTERFACE;
00168 
00169         if ( !IsInvokeAllowed( dispidMember ) ) return E_UNEXPECTED;
00170 
00171         LPTYPEINFO pTypeInfo;
00172         HRESULT hr;
00173         SCODE sc;
00174 
00175         sc = ComGetTypeInfo( pUnk, 0, lcid, &pTypeInfo );
00176         if ( FAILED( sc ) ) return sc;
00177 
00178         hr = DispInvoke(        pUnk, pTypeInfo, dispidMember, wFlags, pdispparams,
00179                                                 pvarResult, pexcepinfo, puArgErr );
00180 
00181         pTypeInfo->Release();
00182 
00183         return hr;
00184 }
00185 
00187 // GUID tools
00188 
00189 CString GUIDX::Encode(const void * pInGUID)
00190 {
00191         const GUID* pGUID = reinterpret_cast<const GUID*>(pInGUID);
00192         CString str;
00193         str.Format( _T("{%.8X-%.4X-%.4X-%.2X%.2X-%.2X%.2X%.2X%.2X%.2X%.2X}"),
00194                 pGUID->Data1, pGUID->Data2, pGUID->Data3,
00195                 pGUID->Data4[0], pGUID->Data4[1], pGUID->Data4[2], pGUID->Data4[3],
00196                 pGUID->Data4[4], pGUID->Data4[5], pGUID->Data4[6], pGUID->Data4[7] );
00197         return str;
00198 }
00199 
00200 bool GUIDX::Decode(LPCTSTR pszIn, LPVOID pOutGUID)
00201 {
00202         ASSERT( pOutGUID != NULL );
00203 
00204         if ( pszIn == NULL ) return false;
00205 
00206         if ( _tcslen(pszIn) == 38 )
00207         {
00208                 if ( pszIn[0] != '{' || pszIn[37] != '}' ) return false;
00209         }
00210         else if ( _tcslen(pszIn) == 36 )
00211         {
00212                 pszIn --;
00213         }
00214         else return false;
00215 
00216         BYTE* pGUID = reinterpret_cast<BYTE*>(pOutGUID);
00217 
00218         if ( ! Unhex( pszIn + 1, pGUID + 3 ) ) return false;
00219         if ( ! Unhex( pszIn + 3, pGUID + 2 ) ) return false;
00220         if ( ! Unhex( pszIn + 5, pGUID + 1 ) ) return false;
00221         if ( ! Unhex( pszIn + 7, pGUID + 0 ) ) return false;
00222         if ( ! Unhex( pszIn + 10, pGUID + 5 ) ) return false;
00223         if ( ! Unhex( pszIn + 12, pGUID + 4 ) ) return false;
00224         if ( ! Unhex( pszIn + 15, pGUID + 7 ) ) return false;
00225         if ( ! Unhex( pszIn + 17, pGUID + 6 ) ) return false;
00226         if ( ! Unhex( pszIn + 20, pGUID + 8 ) ) return false;
00227         if ( ! Unhex( pszIn + 22, pGUID + 9 ) ) return false;
00228         if ( ! Unhex( pszIn + 25, pGUID + 10 ) ) return false;
00229         if ( ! Unhex( pszIn + 27, pGUID + 11 ) ) return false;
00230         if ( ! Unhex( pszIn + 29, pGUID + 12 ) ) return false;
00231         if ( ! Unhex( pszIn + 31, pGUID + 13 ) ) return false;
00232         if ( ! Unhex( pszIn + 33, pGUID + 14 ) ) return false;
00233         if ( ! Unhex( pszIn + 35, pGUID + 15 ) ) return false;
00234 
00235         return true;
00236 }
00237 
00238 bool GUIDX::Unhex(LPCTSTR psz, LPBYTE pOut)
00239 {
00240         register TCHAR c = *psz++;
00241         if ( c >= '0' && c <= '9' )
00242                 *pOut = ( c - '0' ) << 4;
00243         else if ( c >= 'A' && c <= 'F' )
00244                 *pOut = ( c - 'A' + 10 ) << 4;
00245         else if ( c >= 'a' && c <= 'f' )
00246                 *pOut = ( c - 'a' + 10 ) << 4;
00247         else
00248                 return false;
00249         c = *psz;
00250         if ( c >= '0' && c <= '9' )
00251                 *pOut |= ( c - '0' );
00252         else if ( c >= 'A' && c <= 'F' )
00253                 *pOut |= ( c - 'A' + 10 );
00254         else if ( c >= 'a' && c <= 'f' )
00255                 *pOut |= ( c - 'a' + 10 );
00256         else
00257                 return false;
00258         return true;
00259 }

Generated on Thu Dec 15 10:39:34 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2