winepulse v18: Latency and compilation improvements
[wine/multimedia.git] / dlls / ole32 / errorinfo.c
blobb71ee2d947c9c5c450459dc04489e0c89f52e04e
1 /*
2 * ErrorInfo API
4 * Copyright 2000 Patrik Stridvall, Juergen Schmied
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * NOTES:
22 * The errorinfo is a per-thread object. The reference is stored in the
23 * TEB at offset 0xf80.
26 #include <stdarg.h>
27 #include <string.h>
29 #define COBJMACROS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "objbase.h"
34 #include "oleauto.h"
35 #include "winerror.h"
37 #include "wine/unicode.h"
38 #include "compobj_private.h"
40 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 static inline void *heap_alloc(size_t len)
46 return HeapAlloc(GetProcessHeap(), 0, len);
49 static inline BOOL heap_free(void *mem)
51 return HeapFree(GetProcessHeap(), 0, mem);
54 static inline WCHAR *heap_strdupW(const WCHAR *str)
56 WCHAR *ret = NULL;
58 if(str) {
59 size_t size;
61 size = (strlenW(str)+1)*sizeof(WCHAR);
62 ret = heap_alloc(size);
63 if(ret)
64 memcpy(ret, str, size);
67 return ret;
70 typedef struct ErrorInfoImpl
72 IErrorInfo IErrorInfo_iface;
73 ICreateErrorInfo ICreateErrorInfo_iface;
74 ISupportErrorInfo ISupportErrorInfo_iface;
75 LONG ref;
77 GUID m_Guid;
78 WCHAR *source;
79 WCHAR *description;
80 WCHAR *help_file;
81 DWORD m_dwHelpContext;
82 } ErrorInfoImpl;
84 static inline ErrorInfoImpl *impl_from_IErrorInfo( IErrorInfo *iface )
86 return CONTAINING_RECORD(iface, ErrorInfoImpl, IErrorInfo_iface);
89 static inline ErrorInfoImpl *impl_from_ICreateErrorInfo( ICreateErrorInfo *iface )
91 return CONTAINING_RECORD(iface, ErrorInfoImpl, ICreateErrorInfo_iface);
94 static inline ErrorInfoImpl *impl_from_ISupportErrorInfo( ISupportErrorInfo *iface )
96 return CONTAINING_RECORD(iface, ErrorInfoImpl, ISupportErrorInfo_iface);
99 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
100 IErrorInfo* iface,
101 REFIID riid,
102 void** ppvoid)
104 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
105 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid),ppvoid);
107 *ppvoid = NULL;
109 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IErrorInfo))
111 *ppvoid = &This->IErrorInfo_iface;
113 else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
115 *ppvoid = &This->ICreateErrorInfo_iface;
117 else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
119 *ppvoid = &This->ISupportErrorInfo_iface;
122 if(*ppvoid)
124 IUnknown_AddRef( (IUnknown*)*ppvoid );
125 TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
126 return S_OK;
128 TRACE("-- Interface: E_NOINTERFACE\n");
129 return E_NOINTERFACE;
132 static ULONG WINAPI IErrorInfoImpl_AddRef(
133 IErrorInfo* iface)
135 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
136 TRACE("(%p)->(count=%u)\n",This,This->ref);
137 return InterlockedIncrement(&This->ref);
140 static ULONG WINAPI IErrorInfoImpl_Release(
141 IErrorInfo* iface)
143 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
144 ULONG ref = InterlockedDecrement(&This->ref);
146 TRACE("(%p)->(count=%u)\n",This,ref+1);
148 if (!ref)
150 TRACE("-- destroying IErrorInfo(%p)\n",This);
152 heap_free(This->source);
153 heap_free(This->description);
154 heap_free(This->help_file);
155 heap_free(This);
157 return ref;
160 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
161 IErrorInfo* iface,
162 GUID * pGUID)
164 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
165 TRACE("(%p)->(count=%u)\n",This,This->ref);
166 if(!pGUID )return E_INVALIDARG;
167 *pGUID = This->m_Guid;
168 return S_OK;
171 static HRESULT WINAPI IErrorInfoImpl_GetSource(
172 IErrorInfo* iface,
173 BSTR *pBstrSource)
175 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
176 TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
177 if (pBstrSource == NULL)
178 return E_INVALIDARG;
179 *pBstrSource = SysAllocString(This->source);
180 return S_OK;
183 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
184 IErrorInfo* iface,
185 BSTR *pBstrDescription)
187 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
189 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
190 if (pBstrDescription == NULL)
191 return E_INVALIDARG;
192 *pBstrDescription = SysAllocString(This->description);
194 return S_OK;
197 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
198 IErrorInfo* iface,
199 BSTR *pBstrHelpFile)
201 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
203 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
204 if (pBstrHelpFile == NULL)
205 return E_INVALIDARG;
206 *pBstrHelpFile = SysAllocString(This->help_file);
208 return S_OK;
211 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
212 IErrorInfo* iface,
213 DWORD *pdwHelpContext)
215 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
216 TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
217 if (pdwHelpContext == NULL)
218 return E_INVALIDARG;
219 *pdwHelpContext = This->m_dwHelpContext;
221 return S_OK;
224 static const IErrorInfoVtbl ErrorInfoVtbl =
226 IErrorInfoImpl_QueryInterface,
227 IErrorInfoImpl_AddRef,
228 IErrorInfoImpl_Release,
229 IErrorInfoImpl_GetGUID,
230 IErrorInfoImpl_GetSource,
231 IErrorInfoImpl_GetDescription,
232 IErrorInfoImpl_GetHelpFile,
233 IErrorInfoImpl_GetHelpContext
237 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
238 ICreateErrorInfo* iface,
239 REFIID riid,
240 VOID** ppvoid)
242 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
243 return IErrorInfo_QueryInterface(&This->IErrorInfo_iface, riid, ppvoid);
246 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
247 ICreateErrorInfo* iface)
249 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
250 return IErrorInfo_AddRef(&This->IErrorInfo_iface);
253 static ULONG WINAPI ICreateErrorInfoImpl_Release(
254 ICreateErrorInfo* iface)
256 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
257 return IErrorInfo_Release(&This->IErrorInfo_iface);
261 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
262 ICreateErrorInfo* iface,
263 REFGUID rguid)
265 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
266 TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
267 This->m_Guid = *rguid;
268 return S_OK;
271 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
272 ICreateErrorInfo* iface,
273 LPOLESTR szSource)
275 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
276 TRACE("(%p): %s\n",This, debugstr_w(szSource));
278 heap_free(This->source);
279 This->source = heap_strdupW(szSource);
281 return S_OK;
284 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
285 ICreateErrorInfo* iface,
286 LPOLESTR szDescription)
288 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
289 TRACE("(%p): %s\n",This, debugstr_w(szDescription));
291 heap_free(This->description);
292 This->description = heap_strdupW(szDescription);
293 return S_OK;
296 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
297 ICreateErrorInfo* iface,
298 LPOLESTR szHelpFile)
300 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
301 TRACE("(%p,%s)\n",This,debugstr_w(szHelpFile));
302 heap_free(This->help_file);
303 This->help_file = heap_strdupW(szHelpFile);
304 return S_OK;
307 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
308 ICreateErrorInfo* iface,
309 DWORD dwHelpContext)
311 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
312 TRACE("(%p,%d)\n",This,dwHelpContext);
313 This->m_dwHelpContext = dwHelpContext;
314 return S_OK;
317 static const ICreateErrorInfoVtbl CreateErrorInfoVtbl =
319 ICreateErrorInfoImpl_QueryInterface,
320 ICreateErrorInfoImpl_AddRef,
321 ICreateErrorInfoImpl_Release,
322 ICreateErrorInfoImpl_SetGUID,
323 ICreateErrorInfoImpl_SetSource,
324 ICreateErrorInfoImpl_SetDescription,
325 ICreateErrorInfoImpl_SetHelpFile,
326 ICreateErrorInfoImpl_SetHelpContext
329 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
330 ISupportErrorInfo* iface,
331 REFIID riid,
332 VOID** ppvoid)
334 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
335 return IErrorInfo_QueryInterface(&This->IErrorInfo_iface, riid, ppvoid);
338 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(ISupportErrorInfo* iface)
340 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
341 return IErrorInfo_AddRef(&This->IErrorInfo_iface);
344 static ULONG WINAPI ISupportErrorInfoImpl_Release(ISupportErrorInfo* iface)
346 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
347 return IErrorInfo_Release(&This->IErrorInfo_iface);
350 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
351 ISupportErrorInfo* iface,
352 REFIID riid)
354 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
355 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
356 return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
359 static const ISupportErrorInfoVtbl SupportErrorInfoVtbl =
361 ISupportErrorInfoImpl_QueryInterface,
362 ISupportErrorInfoImpl_AddRef,
363 ISupportErrorInfoImpl_Release,
364 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
367 static IErrorInfo* IErrorInfoImpl_Constructor(void)
369 ErrorInfoImpl *This = heap_alloc(sizeof(ErrorInfoImpl));
371 if (!This) return NULL;
373 This->IErrorInfo_iface.lpVtbl = &ErrorInfoVtbl;
374 This->ICreateErrorInfo_iface.lpVtbl = &CreateErrorInfoVtbl;
375 This->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
376 This->ref = 1;
377 This->source = NULL;
378 This->description = NULL;
379 This->help_file = NULL;
380 This->m_dwHelpContext = 0;
382 return &This->IErrorInfo_iface;
385 /***********************************************************************
386 * CreateErrorInfo (OLE32.@)
388 * Creates an object used to set details for an error info object.
390 * PARAMS
391 * pperrinfo [O]. Address where error info creation object will be stored.
393 * RETURNS
394 * Success: S_OK.
395 * Failure: HRESULT code.
397 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
399 IErrorInfo * pei;
400 HRESULT res;
401 TRACE("(%p)\n", pperrinfo);
402 if(! pperrinfo ) return E_INVALIDARG;
403 if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
405 res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
406 IErrorInfo_Release(pei);
407 return res;
410 /***********************************************************************
411 * GetErrorInfo (OLE32.@)
413 * Retrieves the error information object for the current thread.
415 * PARAMS
416 * dwReserved [I]. Reserved. Must be zero.
417 * pperrinfo [O]. Address where error information object will be stored on return.
419 * RETURNS
420 * Success: S_OK if an error information object was set for the current thread.
421 * S_FALSE if otherwise.
422 * Failure: E_INVALIDARG if dwReserved is not zero.
424 * NOTES
425 * This function causes the current error info object for the thread to be
426 * cleared if one was set beforehand.
428 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
430 TRACE("(%d, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->errorinfo);
432 if (dwReserved)
434 ERR("dwReserved (0x%x) != 0\n", dwReserved);
435 return E_INVALIDARG;
438 if(!pperrinfo) return E_INVALIDARG;
440 if (!COM_CurrentInfo()->errorinfo)
442 *pperrinfo = NULL;
443 return S_FALSE;
446 *pperrinfo = COM_CurrentInfo()->errorinfo;
448 /* clear thread error state */
449 COM_CurrentInfo()->errorinfo = NULL;
450 return S_OK;
453 /***********************************************************************
454 * SetErrorInfo (OLE32.@)
456 * Sets the error information object for the current thread.
458 * PARAMS
459 * dwReserved [I] Reserved. Must be zero.
460 * perrinfo [I] Error info object.
462 * RETURNS
463 * Success: S_OK.
464 * Failure: E_INVALIDARG if dwReserved is not zero.
466 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
468 IErrorInfo * pei;
470 TRACE("(%d, %p)\n", dwReserved, perrinfo);
472 if (dwReserved)
474 ERR("dwReserved (0x%x) != 0\n", dwReserved);
475 return E_INVALIDARG;
478 /* release old errorinfo */
479 pei = COM_CurrentInfo()->errorinfo;
480 if (pei) IErrorInfo_Release(pei);
482 /* set to new value */
483 COM_CurrentInfo()->errorinfo = perrinfo;
484 if (perrinfo) IErrorInfo_AddRef(perrinfo);
486 return S_OK;