Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages
util.h
Go to the documentation of this file.00001 00004 /* 00005 * Copyright © 2000, 2001 Sofus Mortensen 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_UTIL_H 00020 #define COMET_UTIL_H 00021 00022 #include <comet/config.h> 00023 00024 #include <stdexcept> 00025 00026 #include <comet/error.h> 00027 #include <comet/server.h> 00028 00029 namespace comet { 00030 00034 00035 class auto_coinit { 00036 public: 00037 #if (_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM) // DCOM 00038 explicit auto_coinit(COINIT ci) { 00039 ::CoInitializeEx(0, ci) | raise_exception; 00040 } 00041 #endif 00042 00043 auto_coinit() { ::CoInitialize(0) | raise_exception; } 00044 00045 ~auto_coinit() { ::CoUninitialize(); } 00046 00047 private: 00048 auto_coinit& operator=(const auto_coinit&); 00049 auto_coinit(const auto_coinit&); 00050 }; 00051 00052 // For backward compatibility. 00053 typedef auto_coinit auto_CoInitialize; 00054 00056 inline CLSID get_clsid_of_ftm() 00057 { 00058 CLSID rv; 00059 class Dummy : public static_object<> {} dummy; 00060 00061 IUnknown* pUnk = 0; 00062 ::CoCreateFreeThreadedMarshaler(&dummy, &pUnk) | raise_exception; 00063 00064 com_ptr<IMarshal> marshal = try_cast(pUnk); 00065 00066 marshal.raw()->GetUnmarshalClass(IID_IUnknown, &dummy, MSHCTX_INPROC,0,MSHLFLAGS_NORMAL, &rv) | raise_exception; 00067 return rv; 00068 } 00069 00073 inline bool is_object_aggregating_ftm(const com_ptr<IUnknown>& p) 00074 { 00075 com_ptr<IMarshal> marshal = com_cast(p); 00076 if (marshal == 0) return false; 00077 00078 class Dummy : public static_object<> {} dummy; 00079 00080 CLSID clsid = CLSID_NULL; 00081 marshal.raw()->GetUnmarshalClass(IID_IUnknown, &dummy, MSHCTX_INPROC,0,MSHLFLAGS_NORMAL, &clsid); 00082 00083 CLSID ftm_clsid = get_clsid_of_ftm(); 00084 return clsid == ftm_clsid ? true : false; 00085 } 00086 00087 #if (_WIN32_WINNT >= 0x0400 ) || defined(_WIN32_DCOM) // DCOM 00088 00091 inline bool is_mta() 00092 { 00093 if (SUCCEEDED(::CoInitializeEx(0, COINIT_MULTITHREADED))) 00094 { 00095 ::CoUninitialize(); 00096 return true; 00097 } 00098 return false; 00099 } 00100 #endif 00101 00104 inline bool is_sta() 00105 { 00106 if (SUCCEEDED(::CoInitialize(0))) 00107 { 00108 ::CoUninitialize(); 00109 return true; 00110 } 00111 return false; 00112 } 00113 00117 inline bool is_std_proxy(const com_ptr<IUnknown>& unk) 00118 { 00119 com_ptr<IUnknown> pm; 00120 return SUCCEEDED(unk.raw()->QueryInterface(IID_IProxyManager, reinterpret_cast<void**>(pm.out()))); 00121 } 00122 00123 } // namespace 00124 00125 #endif