ole32: COM cleanup for IErrorInfo.
[wine/multimedia.git] / dlls / ole32 / errorinfo.c
blob6a499fc328705620ea6d682c7c185dc4f8b580c9
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 /* this code is from SysAllocStringLen (ole2disp.c in oleaut32) */
45 static BSTR ERRORINFO_SysAllocString(const OLECHAR* in)
47 DWORD bufferSize;
48 DWORD* newBuffer;
49 WCHAR* stringBuffer;
50 DWORD len;
52 if (in == NULL)
53 return NULL;
55 * Find the length of the buffer passed-in, in bytes.
57 len = strlenW(in);
58 bufferSize = len * sizeof (WCHAR);
61 * Allocate a new buffer to hold the string.
62 * don't forget to keep an empty spot at the beginning of the
63 * buffer for the character count and an extra character at the
64 * end for the '\0'.
66 newBuffer = HeapAlloc(GetProcessHeap(), 0,
67 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
70 * If the memory allocation failed, return a null pointer.
72 if (newBuffer==0)
73 return 0;
76 * Copy the length of the string in the placeholder.
78 *newBuffer = bufferSize;
81 * Skip the byte count.
83 newBuffer++;
86 * Copy the information in the buffer. It is not possible to pass
87 * a NULL pointer here.
89 memcpy(newBuffer, in, bufferSize);
92 * Make sure that there is a nul character at the end of the
93 * string.
95 stringBuffer = (WCHAR*)newBuffer;
96 stringBuffer[len] = 0;
98 return stringBuffer;
101 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
102 static VOID ERRORINFO_SysFreeString(BSTR in)
104 DWORD* bufferPointer;
106 /* NULL is a valid parameter */
107 if(!in) return;
110 * We have to be careful when we free a BSTR pointer, it points to
111 * the beginning of the string but it skips the byte count contained
112 * before the string.
114 bufferPointer = (DWORD*)in;
116 bufferPointer--;
119 * Free the memory from it's "real" origin.
121 HeapFree(GetProcessHeap(), 0, bufferPointer);
124 typedef struct ErrorInfoImpl
126 IErrorInfo IErrorInfo_iface;
127 ICreateErrorInfo ICreateErrorInfo_iface;
128 ISupportErrorInfo ISupportErrorInfo_iface;
129 LONG ref;
131 GUID m_Guid;
132 BSTR bstrSource;
133 BSTR bstrDescription;
134 BSTR bstrHelpFile;
135 DWORD m_dwHelpContext;
136 } ErrorInfoImpl;
138 static inline ErrorInfoImpl *impl_from_IErrorInfo( IErrorInfo *iface )
140 return CONTAINING_RECORD(iface, ErrorInfoImpl, IErrorInfo_iface);
143 static inline ErrorInfoImpl *impl_from_ICreateErrorInfo( ICreateErrorInfo *iface )
145 return CONTAINING_RECORD(iface, ErrorInfoImpl, ICreateErrorInfo_iface);
148 static inline ErrorInfoImpl *impl_from_ISupportErrorInfo( ISupportErrorInfo *iface )
150 return CONTAINING_RECORD(iface, ErrorInfoImpl, ISupportErrorInfo_iface);
153 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
154 IErrorInfo* iface,
155 REFIID riid,
156 void** ppvoid)
158 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
159 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid),ppvoid);
161 *ppvoid = NULL;
163 if(IsEqualIID(riid, &IID_IErrorInfo))
165 *ppvoid = &This->IErrorInfo_iface;
167 else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
169 *ppvoid = &This->ICreateErrorInfo_iface;
171 else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
173 *ppvoid = &This->ISupportErrorInfo_iface;
176 if(*ppvoid)
178 IUnknown_AddRef( (IUnknown*)*ppvoid );
179 TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
180 return S_OK;
182 TRACE("-- Interface: E_NOINTERFACE\n");
183 return E_NOINTERFACE;
186 static ULONG WINAPI IErrorInfoImpl_AddRef(
187 IErrorInfo* iface)
189 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
190 TRACE("(%p)->(count=%u)\n",This,This->ref);
191 return InterlockedIncrement(&This->ref);
194 static ULONG WINAPI IErrorInfoImpl_Release(
195 IErrorInfo* iface)
197 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
198 ULONG ref = InterlockedDecrement(&This->ref);
200 TRACE("(%p)->(count=%u)\n",This,ref+1);
202 if (!ref)
204 TRACE("-- destroying IErrorInfo(%p)\n",This);
206 ERRORINFO_SysFreeString(This->bstrSource);
207 ERRORINFO_SysFreeString(This->bstrDescription);
208 ERRORINFO_SysFreeString(This->bstrHelpFile);
209 HeapFree(GetProcessHeap(),0,This);
210 return 0;
212 return ref;
215 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
216 IErrorInfo* iface,
217 GUID * pGUID)
219 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
220 TRACE("(%p)->(count=%u)\n",This,This->ref);
221 if(!pGUID )return E_INVALIDARG;
222 *pGUID = This->m_Guid;
223 return S_OK;
226 static HRESULT WINAPI IErrorInfoImpl_GetSource(
227 IErrorInfo* iface,
228 BSTR *pBstrSource)
230 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
231 TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
232 if (pBstrSource == NULL)
233 return E_INVALIDARG;
234 *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
235 return S_OK;
238 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
239 IErrorInfo* iface,
240 BSTR *pBstrDescription)
242 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
244 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
245 if (pBstrDescription == NULL)
246 return E_INVALIDARG;
247 *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
249 return S_OK;
252 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
253 IErrorInfo* iface,
254 BSTR *pBstrHelpFile)
256 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
258 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
259 if (pBstrHelpFile == NULL)
260 return E_INVALIDARG;
261 *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
263 return S_OK;
266 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
267 IErrorInfo* iface,
268 DWORD *pdwHelpContext)
270 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
271 TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
272 if (pdwHelpContext == NULL)
273 return E_INVALIDARG;
274 *pdwHelpContext = This->m_dwHelpContext;
276 return S_OK;
279 static const IErrorInfoVtbl ErrorInfoVtbl =
281 IErrorInfoImpl_QueryInterface,
282 IErrorInfoImpl_AddRef,
283 IErrorInfoImpl_Release,
284 IErrorInfoImpl_GetGUID,
285 IErrorInfoImpl_GetSource,
286 IErrorInfoImpl_GetDescription,
287 IErrorInfoImpl_GetHelpFile,
288 IErrorInfoImpl_GetHelpContext
292 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
293 ICreateErrorInfo* iface,
294 REFIID riid,
295 VOID** ppvoid)
297 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
298 return IErrorInfo_QueryInterface(&This->IErrorInfo_iface, riid, ppvoid);
301 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
302 ICreateErrorInfo* iface)
304 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
305 return IErrorInfo_AddRef(&This->IErrorInfo_iface);
308 static ULONG WINAPI ICreateErrorInfoImpl_Release(
309 ICreateErrorInfo* iface)
311 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
312 return IErrorInfo_Release(&This->IErrorInfo_iface);
316 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
317 ICreateErrorInfo* iface,
318 REFGUID rguid)
320 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
321 TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
322 This->m_Guid = *rguid;
323 return S_OK;
326 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
327 ICreateErrorInfo* iface,
328 LPOLESTR szSource)
330 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
331 TRACE("(%p): %s\n",This, debugstr_w(szSource));
332 if (This->bstrSource != NULL)
333 ERRORINFO_SysFreeString(This->bstrSource);
334 This->bstrSource = ERRORINFO_SysAllocString(szSource);
336 return S_OK;
339 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
340 ICreateErrorInfo* iface,
341 LPOLESTR szDescription)
343 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
344 TRACE("(%p): %s\n",This, debugstr_w(szDescription));
345 if (This->bstrDescription != NULL)
346 ERRORINFO_SysFreeString(This->bstrDescription);
347 This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
349 return S_OK;
352 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
353 ICreateErrorInfo* iface,
354 LPOLESTR szHelpFile)
356 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
357 TRACE("(%p,%s)\n",This,debugstr_w(szHelpFile));
358 if (This->bstrHelpFile != NULL)
359 ERRORINFO_SysFreeString(This->bstrHelpFile);
360 This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
361 return S_OK;
364 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
365 ICreateErrorInfo* iface,
366 DWORD dwHelpContext)
368 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
369 TRACE("(%p,%d)\n",This,dwHelpContext);
370 This->m_dwHelpContext = dwHelpContext;
371 return S_OK;
374 static const ICreateErrorInfoVtbl CreateErrorInfoVtbl =
376 ICreateErrorInfoImpl_QueryInterface,
377 ICreateErrorInfoImpl_AddRef,
378 ICreateErrorInfoImpl_Release,
379 ICreateErrorInfoImpl_SetGUID,
380 ICreateErrorInfoImpl_SetSource,
381 ICreateErrorInfoImpl_SetDescription,
382 ICreateErrorInfoImpl_SetHelpFile,
383 ICreateErrorInfoImpl_SetHelpContext
386 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
387 ISupportErrorInfo* iface,
388 REFIID riid,
389 VOID** ppvoid)
391 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
392 return IErrorInfo_QueryInterface(&This->IErrorInfo_iface, riid, ppvoid);
395 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(ISupportErrorInfo* iface)
397 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
398 return IErrorInfo_AddRef(&This->IErrorInfo_iface);
401 static ULONG WINAPI ISupportErrorInfoImpl_Release(ISupportErrorInfo* iface)
403 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
404 return IErrorInfo_Release(&This->IErrorInfo_iface);
407 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
408 ISupportErrorInfo* iface,
409 REFIID riid)
411 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
412 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
413 return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
416 static const ISupportErrorInfoVtbl SupportErrorInfoVtbl =
418 ISupportErrorInfoImpl_QueryInterface,
419 ISupportErrorInfoImpl_AddRef,
420 ISupportErrorInfoImpl_Release,
421 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
424 static IErrorInfo* IErrorInfoImpl_Constructor(void)
426 ErrorInfoImpl *This = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
428 if (!This) return NULL;
430 This->IErrorInfo_iface.lpVtbl = &ErrorInfoVtbl;
431 This->ICreateErrorInfo_iface.lpVtbl = &CreateErrorInfoVtbl;
432 This->ISupportErrorInfo_iface.lpVtbl = &SupportErrorInfoVtbl;
433 This->ref = 1;
434 This->bstrSource = NULL;
435 This->bstrDescription = NULL;
436 This->bstrHelpFile = NULL;
437 This->m_dwHelpContext = 0;
439 return &This->IErrorInfo_iface;
442 /***********************************************************************
443 * CreateErrorInfo (OLE32.@)
445 * Creates an object used to set details for an error info object.
447 * PARAMS
448 * pperrinfo [O]. Address where error info creation object will be stored.
450 * RETURNS
451 * Success: S_OK.
452 * Failure: HRESULT code.
454 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
456 IErrorInfo * pei;
457 HRESULT res;
458 TRACE("(%p)\n", pperrinfo);
459 if(! pperrinfo ) return E_INVALIDARG;
460 if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
462 res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
463 IErrorInfo_Release(pei);
464 return res;
467 /***********************************************************************
468 * GetErrorInfo (OLE32.@)
470 * Retrieves the error information object for the current thread.
472 * PARAMS
473 * dwReserved [I]. Reserved. Must be zero.
474 * pperrinfo [O]. Address where error information object will be stored on return.
476 * RETURNS
477 * Success: S_OK if an error information object was set for the current thread.
478 * S_FALSE if otherwise.
479 * Failure: E_INVALIDARG if dwReserved is not zero.
481 * NOTES
482 * This function causes the current error info object for the thread to be
483 * cleared if one was set beforehand.
485 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
487 TRACE("(%d, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->errorinfo);
489 if (dwReserved)
491 ERR("dwReserved (0x%x) != 0\n", dwReserved);
492 return E_INVALIDARG;
495 if(!pperrinfo) return E_INVALIDARG;
497 if (!COM_CurrentInfo()->errorinfo)
499 *pperrinfo = NULL;
500 return S_FALSE;
503 *pperrinfo = COM_CurrentInfo()->errorinfo;
505 /* clear thread error state */
506 COM_CurrentInfo()->errorinfo = NULL;
507 return S_OK;
510 /***********************************************************************
511 * SetErrorInfo (OLE32.@)
513 * Sets the error information object for the current thread.
515 * PARAMS
516 * dwReserved [I] Reserved. Must be zero.
517 * perrinfo [I] Error info object.
519 * RETURNS
520 * Success: S_OK.
521 * Failure: E_INVALIDARG if dwReserved is not zero.
523 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
525 IErrorInfo * pei;
527 TRACE("(%d, %p)\n", dwReserved, perrinfo);
529 if (dwReserved)
531 ERR("dwReserved (0x%x) != 0\n", dwReserved);
532 return E_INVALIDARG;
535 /* release old errorinfo */
536 pei = COM_CurrentInfo()->errorinfo;
537 if (pei) IErrorInfo_Release(pei);
539 /* set to new value */
540 COM_CurrentInfo()->errorinfo = perrinfo;
541 if (perrinfo) IErrorInfo_AddRef(perrinfo);
543 return S_OK;