comutil.h

Go to the documentation of this file.
00001 /*
00002  * Copyright 2005 Martin Fuchs
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2.1 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 
00020  //
00021  // ROS Internet Web Browser
00022  // 
00023  // comutil.h
00024  //
00025  // C++ wrapper classes for COM interfaces
00026  //
00027  // Martin Fuchs, 25.01.2005
00028  //
00029 
00030 
00031  // windows shell headers
00032 #include <shellapi.h>
00033 #include <shlobj.h>
00034 
00035 /*@@
00036 #if _MSC_VER>=1300  // VS.Net
00037 #include <comdefsp.h>
00038 using namespace _com_util;
00039 #endif
00040 */
00041 
00042 #ifndef _INC_COMUTIL    // is comutil.h of MS headers not available?
00043 #ifndef _NO_COMUTIL
00044 #define _NO_COMUTIL
00045 #endif
00046 #endif
00047 
00048 
00049  // Exception Handling
00050 
00051 #ifndef _NO_COMUTIL
00052 
00053 #define COMExceptionBase _com_error
00054 
00055 #else
00056 
00057  /// COM ExceptionBase class as replacement for _com_error
00058 struct COMExceptionBase
00059 {
00060     COMExceptionBase(HRESULT hr)
00061      :  _hr(hr)
00062     {
00063     }
00064 
00065     HRESULT Error() const
00066     {
00067         return _hr;
00068     }
00069 
00070     LPCTSTR ErrorMessage() const
00071     {
00072         if (_msg.empty()) {
00073             LPTSTR pBuf;
00074 
00075             if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
00076                 0, _hr, MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT), (LPTSTR)&pBuf, 0, NULL)) {
00077                 _msg = pBuf;
00078                 LocalFree(pBuf);
00079              } else {
00080                 TCHAR buffer[128];
00081                 _stprintf(buffer, TEXT("unknown Exception: 0x%08lX"), _hr);
00082                 _msg = buffer;
00083              }
00084         }
00085 
00086         return _msg;
00087     }
00088 
00089 protected:
00090     HRESULT _hr;
00091     mutable String _msg;
00092 };
00093 
00094 #endif
00095 
00096 
00097  /// Exception with context information
00098 
00099 struct COMException : public COMExceptionBase
00100 {
00101     typedef COMExceptionBase super;
00102 
00103     COMException(HRESULT hr)
00104      :  super(hr),
00105         _context(CURRENT_CONTEXT),
00106         _file(NULL), _line(0)
00107     {
00108         LOG(toString());
00109         LOG(CURRENT_CONTEXT.getStackTrace());
00110     }
00111 
00112     COMException(HRESULT hr, const char* file, int line)
00113      :  super(hr),
00114         _context(CURRENT_CONTEXT),
00115         _file(file), _line(line)
00116     {
00117         LOG(toString());
00118         LOG(CURRENT_CONTEXT.getStackTrace());
00119     }
00120 
00121     COMException(HRESULT hr, const String& obj)
00122      :  super(hr),
00123         _context(CURRENT_CONTEXT),
00124         _file(NULL), _line(0)
00125     {
00126         LOG(toString());
00127         LOG(CURRENT_CONTEXT.getStackTrace());
00128     }
00129 
00130     COMException(HRESULT hr, const String& obj, const char* file, int line)
00131      :  super(hr),
00132         _context(CURRENT_CONTEXT),
00133         _file(file), _line(line)
00134     {
00135         LOG(toString());
00136         LOG(CURRENT_CONTEXT.getStackTrace());
00137     }
00138 
00139     String toString() const;
00140 
00141     Context _context;
00142 
00143     const char* _file;
00144     int _line;
00145 };
00146 
00147 #define THROW_EXCEPTION(hr) throw COMException(hr, __FILE__, __LINE__)
00148 #define CHECKERROR(hr) ((void)(FAILED(hr)? THROW_EXCEPTION(hr): 0))
00149 
00150 
00151 #ifdef _NO_COMUTIL
00152 
00153 inline void CheckError(HRESULT hr)
00154 {
00155     if (FAILED(hr))
00156         throw COMException(hr);
00157 }
00158 
00159 #endif
00160 
00161 
00162  /// COM Initialisation
00163 
00164 struct ComInit
00165 {
00166     ComInit()
00167     {
00168         CHECKERROR(CoInitialize(0));
00169     }
00170 
00171 #if (_WIN32_WINNT>=0x0400) || defined(_WIN32_DCOM)
00172     ComInit(DWORD flag)
00173     {
00174         CHECKERROR(CoInitializeEx(0, flag));
00175     }
00176 #endif
00177 
00178     ~ComInit()
00179     {
00180         CoUninitialize();
00181     }
00182 };
00183 
00184 
00185  /// OLE initialisation for drag drop support
00186 
00187 struct OleInit
00188 {
00189     OleInit()
00190     {
00191         CHECKERROR(OleInitialize(0));
00192     }
00193 
00194     ~OleInit()
00195     {
00196         OleUninitialize();
00197     }
00198 };
00199 
00200 
00201  /// Exception Handler for COM exceptions
00202 
00203 extern void HandleException(COMException& e, HWND hwnd);
00204 
00205 
00206  /// wrapper class for COM interface pointers
00207 
00208 template<typename T> struct SIfacePtr
00209 {
00210     SIfacePtr()
00211      :  _p(0)
00212     {
00213     }
00214 
00215     SIfacePtr(T* p)
00216      :  _p(p)
00217     {
00218         if (p)
00219             p->AddRef();
00220     }
00221 
00222     SIfacePtr(IUnknown* unknown, REFIID riid)
00223     {
00224         CHECKERROR(unknown->QueryInterface(riid, (LPVOID*)&_p));
00225     }
00226 
00227     ~SIfacePtr()
00228     {
00229         Free();
00230     }
00231 
00232     T* operator->()
00233     {
00234         return _p;
00235     }
00236 
00237     const T* operator->() const
00238     {
00239         return _p;
00240     }
00241 
00242 /* not GCC compatible
00243     operator const T*() const
00244     {
00245         return _p;
00246     } */
00247 
00248     operator T*()
00249     {
00250         return _p;
00251     }
00252 
00253     T** operator&()
00254     {
00255         return &_p;
00256     }
00257 
00258     bool empty() const  //NOTE: GCC seems not to work correctly when defining operator bool() AND operator T*() at one time
00259     {
00260         return !_p;
00261     }
00262 
00263     SIfacePtr& operator=(T* p)
00264     {
00265         Free();
00266 
00267         if (p) {
00268             p->AddRef();
00269             _p = p;
00270         }
00271 
00272         return *this;
00273     }
00274 
00275     void operator=(SIfacePtr const& o)
00276     {
00277         T* h = _p;
00278 
00279         if (o._p)
00280             o._p->AddRef();
00281 
00282         _p = o._p;
00283 
00284         if (h)
00285             h->Release();
00286     }
00287 
00288     HRESULT CreateInstance(REFIID clsid, REFIID riid)
00289     {
00290         return CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, riid, (LPVOID*)&_p);
00291     }
00292 
00293     template<typename I> HRESULT QueryInterface(REFIID riid, I* p)
00294     {
00295         return _p->QueryInterface(riid, (LPVOID*)p);
00296     }
00297 
00298     T* get()
00299     {
00300         return _p;
00301     }
00302 
00303     void Free()
00304     {
00305         T* h = _p;
00306         _p = NULL;
00307 
00308         if (h)
00309             h->Release();
00310     }
00311 
00312 protected:
00313     SIfacePtr(const SIfacePtr& o)
00314      :  _p(o._p)
00315     {
00316         if (_p)
00317             _p->AddRef();
00318     }
00319 
00320     T* _p;
00321 };

IBrowser Source Code Documentation
generated on 18.09.2008 by doxygen

Homepage