VirtualBox

source: vbox/trunk/src/VBox/Main/glue/ErrorInfo.cpp@ 382

Last change on this file since 382 was 1, checked in by vboxsync, 55 years ago

import

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
1/** @file
2 *
3 * ErrorInfo class definition
4 */
5
6/*
7 * Copyright (C) 2006 InnoTek Systemberatung GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22#if defined (__WIN__)
23
24#else // !defined (__WIN__)
25
26#include <nsIServiceManager.h>
27#include <nsIExceptionService.h>
28#include <nsCOMPtr.h>
29
30#include <nsIInterfaceInfo.h>
31#include <nsIInterfaceInfoManager.h>
32
33#endif // !defined (__WIN__)
34
35
36#include "VBox/com/VirtualBox.h"
37#include "VBox/com/ErrorInfo.h"
38#include "VBox/com/assert.h"
39
40#include <iprt/stream.h>
41#include <iprt/string.h>
42#include <VBox/err.h>
43
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 freed
48 * using SysFreeString().
49 */
50static 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 type
73 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 BSTR
78 *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#else
94
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#endif
121}
122
123
124namespace com
125{
126
127// IErrorInfo class
128////////////////////////////////////////////////////////////////////////////////
129
130void ErrorInfo::init ()
131{
132 HRESULT rc = E_FAIL;
133
134#if defined (__WIN__)
135
136 ComPtr <IErrorInfo> err;
137 rc = ::GetErrorInfo (0, err.asOutParam());
138 if (rc == S_OK && err)
139 {
140 ComPtr <IVirtualBoxErrorInfo> info;
141 rc = err.queryInterfaceTo (info.asOutParam());
142 if (SUCCEEDED (rc) && info)
143 init (info);
144
145 if (!mIsFullAvailable)
146 {
147 bool gotSomething = false;
148
149 rc = err->GetGUID (mInterfaceID.asOutParam());
150 gotSomething |= SUCCEEDED (rc);
151 if (SUCCEEDED (rc))
152 GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
153
154 rc = err->GetSource (mComponent.asOutParam());
155 gotSomething |= SUCCEEDED (rc);
156
157 rc = err->GetDescription (mText.asOutParam());
158 gotSomething |= SUCCEEDED (rc);
159
160 if (gotSomething)
161 mIsBasicAvailable = true;
162
163 AssertMsg (gotSomething, ("Nothing to fetch!\n"));
164 }
165 }
166
167#else // !defined (__WIN__)
168
169 nsCOMPtr <nsIExceptionService> es;
170 es = do_GetService (NS_EXCEPTIONSERVICE_CONTRACTID, &rc);
171 if (NS_SUCCEEDED (rc))
172 {
173 nsCOMPtr <nsIExceptionManager> em;
174 rc = es->GetCurrentExceptionManager (getter_AddRefs (em));
175 if (NS_SUCCEEDED (rc))
176 {
177 ComPtr <nsIException> ex;
178 rc = em->GetCurrentException (ex.asOutParam());
179 if (NS_SUCCEEDED (rc) && ex)
180 {
181 ComPtr <IVirtualBoxErrorInfo> info;
182 rc = ex.queryInterfaceTo (info.asOutParam());
183 if (NS_SUCCEEDED (rc) && info)
184 init (info);
185
186 if (!mIsFullAvailable)
187 {
188 bool gotSomething = false;
189
190 rc = ex->GetResult (&mResultCode);
191 gotSomething |= NS_SUCCEEDED (rc);
192
193 Utf8Str message;
194 rc = ex->GetMessage (message.asOutParam());
195 gotSomething |= NS_SUCCEEDED (rc);
196 if (NS_SUCCEEDED (rc))
197 mText = message;
198
199 if (gotSomething)
200 mIsBasicAvailable = true;
201
202 AssertMsg (gotSomething, ("Nothing to fetch!\n"));
203 }
204
205 // set the exception to NULL (to emulate Win32 behavior)
206 em->SetCurrentException (NULL);
207
208 rc = NS_OK;
209 }
210 }
211 }
212
213 AssertComRC (rc);
214
215#endif // !defined (__WIN__)
216}
217
218void ErrorInfo::init (IUnknown *i, const GUID &iid)
219{
220 Assert (i);
221 if (!i)
222 return;
223
224#if defined (__WIN__)
225
226 ComPtr <IUnknown> iface = i;
227 ComPtr <ISupportErrorInfo> serr;
228 HRESULT rc = iface.queryInterfaceTo (serr.asOutParam());
229 if (SUCCEEDED (rc))
230 {
231 rc = serr->InterfaceSupportsErrorInfo (iid);
232 if (SUCCEEDED (rc))
233 init();
234 }
235
236#else // !defined (__WIN__)
237
238 init();
239
240#endif // !defined (__WIN__)
241
242 if (mIsBasicAvailable)
243 {
244 mCalleeIID = iid;
245 GetInterfaceNameByIID (iid, mCalleeName.asOutParam());
246 }
247}
248
249void ErrorInfo::init (IVirtualBoxErrorInfo *info)
250{
251 Assert (info);
252 if (!info)
253 return;
254
255 HRESULT rc = E_FAIL;
256 bool gotSomething = false;
257
258 rc = info->COMGETTER(ResultCode) (&mResultCode);
259 gotSomething |= SUCCEEDED (rc);
260
261 rc = info->COMGETTER(InterfaceID) (mInterfaceID.asOutParam());
262 gotSomething |= SUCCEEDED (rc);
263 if (SUCCEEDED (rc))
264 GetInterfaceNameByIID (mInterfaceID, mInterfaceName.asOutParam());
265
266 rc = info->COMGETTER(Component) (mComponent.asOutParam());
267 gotSomething |= SUCCEEDED (rc);
268
269 rc = info->COMGETTER(Text) (mText.asOutParam());
270 gotSomething |= SUCCEEDED (rc);
271
272 if (gotSomething)
273 mIsFullAvailable = mIsBasicAvailable = true;
274
275 AssertMsg (gotSomething, ("Nothing to fetch!\n"));
276}
277
278ErrorInfo::~ErrorInfo()
279{
280}
281
282void ErrorInfo::print (const char *aPrefix /* = NULL */)
283{
284 if (aPrefix == NULL)
285 aPrefix = "";
286
287 RTPrintf ("%sFull error info available = %RTbool\n", aPrefix, mIsFullAvailable);
288 RTPrintf ("%sBasic error info available = %RTbool\n", aPrefix, mIsBasicAvailable);
289 if (mIsFullAvailable || mIsBasicAvailable)
290 {
291 RTPrintf ("%sResult Code = %Rwa\n", aPrefix, mResultCode);
292 RTPrintf ("%sText = {%ls}\n", aPrefix, mText.raw());
293 RTPrintf ("%sComponent = {%ls}\n", aPrefix, mComponent.raw());
294 RTPrintf ("%sInterface = {%ls}\n", aPrefix, mInterfaceName.raw());
295 RTPrintf ("%sIID = {%s}\n", aPrefix, mInterfaceID.toString().raw());
296 RTPrintf ("%sCallee Name = {%ls}\n", aPrefix, mCalleeName.raw());
297 RTPrintf ("%sCallee IID = {%s}\n", aPrefix, mCalleeIID.toString().raw());
298 }
299}
300
301// IErrorInfo class
302////////////////////////////////////////////////////////////////////////////////
303
304ProgressErrorInfo::ProgressErrorInfo (IProgress *progress) :
305 ErrorInfo (true)
306{
307 Assert (progress);
308 if (!progress)
309 return;
310
311 ComPtr <IVirtualBoxErrorInfo> info;
312 HRESULT rc = progress->COMGETTER(ErrorInfo) (info.asOutParam());
313 if (SUCCEEDED (rc) && info)
314 init (info);
315}
316
317}; // namespace com
318
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette