Changeset 2672 in vbox for trunk/src/VBox/Main/glue
- Timestamp:
- May 16, 2007 3:31:49 PM (18 years ago)
- Location:
- trunk/src/VBox/Main/glue
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Main/glue/ErrorInfo.cpp
r1959 r2672 20 20 */ 21 21 22 #if defined (__WIN__)23 24 #else // !defined (__WIN__)22 #if !defined (VBOX_WITH_XPCOM) 23 24 #else 25 25 26 26 #include <nsIServiceManager.h> … … 28 28 #include <nsCOMPtr.h> 29 29 30 #include <nsIInterfaceInfo.h> 31 #include <nsIInterfaceInfoManager.h> 32 33 #endif // !defined (__WIN__) 34 30 #endif 35 31 36 32 #include "VBox/com/VirtualBox.h" 37 33 #include "VBox/com/ErrorInfo.h" 38 34 #include "VBox/com/assert.h" 35 #include "VBox/com/com.h" 39 36 40 37 #include <iprt/stream.h> 41 38 #include <iprt/string.h> 39 42 40 #include <VBox/err.h> 43 41 44 /**45 * Resolves a given interface ID to a string containint interface name.46 * If, for some reason, the given IID cannot be resolved to a name,47 * a NULL string is returned. A non-NULL interface name must be freed48 * using SysFreeString().49 */50 static void GetInterfaceNameByIID (const GUID &id, BSTR *name)51 {52 Assert (name);53 if (!name)54 return;55 56 *name = NULL;57 58 #if defined (__WIN__)59 60 LONG rc;61 LPOLESTR iidStr = NULL;62 if (StringFromIID (id, &iidStr) == S_OK)63 {64 HKEY ifaceKey;65 rc = RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Interface", 0, KEY_QUERY_VALUE, &ifaceKey);66 if (rc == ERROR_SUCCESS)67 {68 HKEY iidKey;69 rc = RegOpenKeyExW (ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey);70 if (rc == ERROR_SUCCESS)71 {72 // determine the size and type73 DWORD sz, type;74 rc = RegQueryValueExW (iidKey, NULL, NULL, &type, NULL, &sz);75 if (rc == ERROR_SUCCESS && type == REG_SZ)76 {77 // query the value to BSTR78 *name = SysAllocStringLen (NULL, (sz + 1) / sizeof (TCHAR) + 1);79 rc = RegQueryValueExW (iidKey, NULL, NULL, NULL, (LPBYTE) *name, &sz);80 if (rc != ERROR_SUCCESS)81 {82 SysFreeString (*name);83 name = NULL;84 }85 }86 RegCloseKey (iidKey);87 }88 RegCloseKey (ifaceKey);89 }90 CoTaskMemFree (iidStr);91 }92 93 #else94 95 nsresult rv;96 nsCOMPtr <nsIInterfaceInfoManager> iim =97 do_GetService (NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv);98 if (NS_SUCCEEDED (rv))99 {100 nsCOMPtr <nsIInterfaceInfo> iinfo;101 rv = iim->GetInfoForIID (&id, getter_AddRefs (iinfo));102 if (NS_SUCCEEDED (rv))103 {104 const char *iname = NULL;105 iinfo->GetNameShared (&iname);106 char *utf8IName = NULL;107 if (VBOX_SUCCESS (RTStrCurrentCPToUtf8 (&utf8IName, iname)))108 {109 PRTUCS2 ucs2IName = NULL;110 if (VBOX_SUCCESS (RTStrUtf8ToUcs2 (&ucs2IName, utf8IName)))111 {112 *name = SysAllocString ((OLECHAR *) ucs2IName);113 RTStrUcs2Free (ucs2IName);114 }115 RTStrFree (utf8IName);116 }117 }118 }119 120 #endif121 }122 123 124 42 namespace com 125 43 { 126 44 127 // IErrorInfo class45 // ErrorInfo class 128 46 //////////////////////////////////////////////////////////////////////////////// 129 47 130 void ErrorInfo::init ( )48 void ErrorInfo::init (bool aKeepObj /* = false */) 131 49 { 132 50 HRESULT rc = E_FAIL; 133 51 134 #if defined (__WIN__)52 #if !defined (VBOX_WITH_XPCOM) 135 53 136 54 ComPtr <IErrorInfo> err; … … 138 56 if (rc == S_OK && err) 139 57 { 58 if (aKeepObj) 59 mErrorInfo = err; 60 140 61 ComPtr <IVirtualBoxErrorInfo> info; 141 62 rc = err.queryInterfaceTo (info.asOutParam()); … … 165 86 } 166 87 167 #else // !defined ( __WIN__)88 #else // !defined (VBOX_WITH_XPCOM) 168 89 169 90 nsCOMPtr <nsIExceptionService> es; … … 179 100 if (NS_SUCCEEDED (rc) && ex) 180 101 { 102 if (aKeepObj) 103 mErrorInfo = ex; 104 181 105 ComPtr <IVirtualBoxErrorInfo> info; 182 106 rc = ex.queryInterfaceTo (info.asOutParam()); … … 213 137 AssertComRC (rc); 214 138 215 #endif // !defined ( __WIN__)216 } 217 218 void ErrorInfo::init (IUnknown * i, const GUID &iid)219 { 220 Assert ( i);221 if (! i)139 #endif // !defined (VBOX_WITH_XPCOM) 140 } 141 142 void ErrorInfo::init (IUnknown *aI, const GUID &aIID, bool aKeepObj /* = false */) 143 { 144 Assert (aI); 145 if (!aI) 222 146 return; 223 147 224 #if defined (__WIN__)225 226 ComPtr <IUnknown> iface = i;148 #if !defined (VBOX_WITH_XPCOM) 149 150 ComPtr <IUnknown> iface = aI; 227 151 ComPtr <ISupportErrorInfo> serr; 228 152 HRESULT rc = iface.queryInterfaceTo (serr.asOutParam()); 229 153 if (SUCCEEDED (rc)) 230 154 { 231 rc = serr->InterfaceSupportsErrorInfo ( iid);155 rc = serr->InterfaceSupportsErrorInfo (aIID); 232 156 if (SUCCEEDED (rc)) 233 init ();234 } 235 236 #else // !defined (__WIN__)237 238 init ();239 240 #endif // !defined (__WIN__)157 init (aKeepObj); 158 } 159 160 #else 161 162 init (aKeepObj); 163 164 #endif 241 165 242 166 if (mIsBasicAvailable) 243 167 { 244 mCalleeIID = iid;245 GetInterfaceNameByIID ( iid, mCalleeName.asOutParam());168 mCalleeIID = aIID; 169 GetInterfaceNameByIID (aIID, mCalleeName.asOutParam()); 246 170 } 247 171 } … … 270 194 271 195 rc = info->COMGETTER(Text) (mText.asOutParam()); 196 gotSomething |= SUCCEEDED (rc); 197 gotAll &= SUCCEEDED (rc); 198 199 ComPtr <IVirtualBoxErrorInfo> next; 200 rc = info->COMGETTER(Next) (next.asOutParam()); 201 if (SUCCEEDED (rc) && !next.isNull()) 202 { 203 mNext.reset (new ErrorInfo (next)); 204 Assert (mNext.get()); 205 if (!mNext.get()) 206 rc = E_OUTOFMEMORY; 207 } 208 else 209 mNext.reset(); 272 210 gotSomething |= SUCCEEDED (rc); 273 211 gotAll &= SUCCEEDED (rc); … … 300 238 } 301 239 302 // IErrorInfo class 240 /** 241 * Sets the given error info object for the current thread. If @a aPreserve 242 * is @c true, then the current error info set on the thread before this 243 * method is called will be preserved in the IVirtualBoxErrorInfo::next 244 * attribute of the new error info object that will be then set as the 245 * current error info object. 246 */ 247 248 //static 249 HRESULT setError (IVirtualBoxErrorInfo *aInfo); 250 251 // ProgressErrorInfo class 303 252 //////////////////////////////////////////////////////////////////////////////// 304 253 305 254 ProgressErrorInfo::ProgressErrorInfo (IProgress *progress) : 306 ErrorInfo ( true)255 ErrorInfo (false /* aDummy */) 307 256 { 308 257 Assert (progress); … … 316 265 } 317 266 267 // ErrorInfoKeeper class 268 //////////////////////////////////////////////////////////////////////////////// 269 270 HRESULT ErrorInfoKeeper::restore() 271 { 272 if (mForgot) 273 return S_OK; 274 275 HRESULT rc = S_OK; 276 277 #if !defined (VBOX_WITH_XPCOM) 278 279 ComPtr <IErrorInfo> err; 280 if (!mErrorInfo.isNull()) 281 { 282 rc = mErrorInfo.queryInterfaceTo (err.asOutParam()); 283 AssertComRC (rc); 284 } 285 rc = ::SetErrorInfo (0, err); 286 287 #else // !defined (VBOX_WITH_XPCOM) 288 289 nsCOMPtr <nsIExceptionService> es; 290 es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc); 291 if (NS_SUCCEEDED (rc)) 292 { 293 nsCOMPtr <nsIExceptionManager> em; 294 rc = es->GetCurrentExceptionManager (getter_AddRefs (em)); 295 if (NS_SUCCEEDED (rc)) 296 { 297 ComPtr <nsIException> ex; 298 if (!mErrorInfo.isNull()) 299 { 300 rc = mErrorInfo.queryInterfaceTo (ex.asOutParam()); 301 AssertComRC (rc); 302 } 303 rc = em->SetCurrentException (ex); 304 } 305 } 306 307 #endif // !defined (VBOX_WITH_XPCOM) 308 309 if (SUCCEEDED (rc)) 310 { 311 mErrorInfo.setNull(); 312 mForgot = true; 313 } 314 315 return rc; 316 } 317 318 318 }; // namespace com 319 319 -
trunk/src/VBox/Main/glue/com.cpp
r1 r2672 20 20 */ 21 21 22 #if defined (__WIN__)22 #if !defined (VBOX_WITH_XPCOM) 23 23 24 24 #include <objbase.h> 25 25 26 #else // !defined (__WIN__)26 #else 27 27 28 28 #include <stdlib.h> … … 36 36 #include <nsEventQueueUtils.h> 37 37 38 #endif // !defined (__WIN__) 38 #include <nsIInterfaceInfo.h> 39 #include <nsIInterfaceInfoManager.h> 40 41 #endif 42 43 #include <iprt/string.h> 44 #include <VBox/err.h> 39 45 40 46 #include "VBox/com/com.h" … … 48 54 HRESULT rc = E_FAIL; 49 55 50 #if defined (__WIN__) 56 #if !defined (VBOX_WITH_XPCOM) 57 51 58 rc = CoInitializeEx (NULL, COINIT_MULTITHREADED | 52 59 COINIT_DISABLE_OLE1DDE | 53 60 COINIT_SPEED_OVER_MEMORY); 54 #else 55 /* 56 * Set VBOX_XPCOM_HOME if not present 57 */61 62 #else 63 64 /* Set VBOX_XPCOM_HOME if not present */ 58 65 if (!getenv("VBOX_XPCOM_HOME")) 59 66 { … … 82 89 } 83 90 } 91 84 92 #endif 85 93 … … 91 99 void Shutdown() 92 100 { 93 #if defined (__WIN__) 101 #if !defined (VBOX_WITH_XPCOM) 102 94 103 CoUninitialize(); 95 #else 104 105 #else 106 96 107 nsCOMPtr <nsIEventQueue> eventQ; 97 108 nsresult rc = NS_GetMainEventQ (getter_AddRefs (eventQ)); … … 100 111 BOOL isOnMainThread = FALSE; 101 112 eventQ->IsOnCurrentThread (&isOnMainThread); 102 eventQ = nsnull; / / early release113 eventQ = nsnull; /* early release */ 103 114 if (isOnMainThread) 104 115 { 105 / / only the main thread needs to uninitialize XPCOM116 /* only the main thread needs to uninitialize XPCOM */ 106 117 NS_ShutdownXPCOM (nsnull); 107 118 XPCOMGlueShutdown(); 108 119 } 109 120 } 121 110 122 #endif 111 123 } 112 124 125 void GetInterfaceNameByIID (const GUID &aIID, BSTR *aName) 126 { 127 Assert (aName); 128 if (!aName) 129 return; 130 131 *aName = NULL; 132 133 #if !defined (VBOX_WITH_XPCOM) 134 135 LONG rc; 136 LPOLESTR iidStr = NULL; 137 if (StringFromIID (aIID, &iidStr) == S_OK) 138 { 139 HKEY ifaceKey; 140 rc = RegOpenKeyExW (HKEY_CLASSES_ROOT, L"Interface", 141 0, KEY_QUERY_VALUE, &ifaceKey); 142 if (rc == ERROR_SUCCESS) 143 { 144 HKEY iidKey; 145 rc = RegOpenKeyExW (ifaceKey, iidStr, 0, KEY_QUERY_VALUE, &iidKey); 146 if (rc == ERROR_SUCCESS) 147 { 148 /* determine the size and type */ 149 DWORD sz, type; 150 rc = RegQueryValueExW (iidKey, NULL, NULL, &type, NULL, &sz); 151 if (rc == ERROR_SUCCESS && type == REG_SZ) 152 { 153 /* query the value to BSTR */ 154 *aName = SysAllocStringLen (NULL, (sz + 1) / 155 sizeof (TCHAR) + 1); 156 rc = RegQueryValueExW (iidKey, NULL, NULL, NULL, 157 (LPBYTE) *aName, &sz); 158 if (rc != ERROR_SUCCESS) 159 { 160 SysFreeString (*aName); 161 aName = NULL; 162 } 163 } 164 RegCloseKey (iidKey); 165 } 166 RegCloseKey (ifaceKey); 167 } 168 CoTaskMemFree (iidStr); 169 } 170 171 #else 172 173 nsresult rv; 174 nsCOMPtr <nsIInterfaceInfoManager> iim = 175 do_GetService (NS_INTERFACEINFOMANAGER_SERVICE_CONTRACTID, &rv); 176 if (NS_SUCCEEDED (rv)) 177 { 178 nsCOMPtr <nsIInterfaceInfo> iinfo; 179 rv = iim->GetInfoForIID (&aIID, getter_AddRefs (iinfo)); 180 if (NS_SUCCEEDED (rv)) 181 { 182 const char *iname = NULL; 183 iinfo->GetNameShared (&iname); 184 char *utf8IName = NULL; 185 if (VBOX_SUCCESS (RTStrCurrentCPToUtf8 (&utf8IName, iname))) 186 { 187 PRTUCS2 ucs2IName = NULL; 188 if (VBOX_SUCCESS (RTStrUtf8ToUcs2 (&ucs2IName, utf8IName))) 189 { 190 *aName = SysAllocString ((OLECHAR *) ucs2IName); 191 RTStrUcs2Free (ucs2IName); 192 } 193 RTStrFree (utf8IName); 194 } 195 } 196 } 197 198 #endif 199 } 200 113 201 }; // namespace com
Note:
See TracChangeset
for help on using the changeset viewer.