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
22 * The errorinfo is a per-thread object. The reference is stored in the
23 * TEB at offset 0xf80.
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
)
55 * Find the length of the buffer passed-in, in bytes.
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
66 newBuffer
= HeapAlloc(GetProcessHeap(), 0,
67 bufferSize
+ sizeof(WCHAR
) + sizeof(DWORD
));
70 * If the memory allocation failed, return a null pointer.
76 * Copy the length of the string in the placeholder.
78 *newBuffer
= bufferSize
;
81 * Skip the byte count.
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
95 stringBuffer
= (WCHAR
*)newBuffer
;
96 stringBuffer
[len
] = 0;
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 */
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
114 bufferPointer
= (DWORD
*)in
;
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
;
133 BSTR bstrDescription
;
135 DWORD m_dwHelpContext
;
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(
158 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
159 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(riid
),ppvoid
);
163 if (IsEqualIID(riid
, &IID_IUnknown
) || 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
;
178 IUnknown_AddRef( (IUnknown
*)*ppvoid
);
179 TRACE("-- Interface: (%p)->(%p)\n",ppvoid
,*ppvoid
);
182 TRACE("-- Interface: E_NOINTERFACE\n");
183 return E_NOINTERFACE
;
186 static ULONG WINAPI
IErrorInfoImpl_AddRef(
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(
197 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
198 ULONG ref
= InterlockedDecrement(&This
->ref
);
200 TRACE("(%p)->(count=%u)\n",This
,ref
+1);
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
);
215 static HRESULT WINAPI
IErrorInfoImpl_GetGUID(
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
;
226 static HRESULT WINAPI
IErrorInfoImpl_GetSource(
230 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
231 TRACE("(%p)->(pBstrSource=%p)\n",This
,pBstrSource
);
232 if (pBstrSource
== NULL
)
234 *pBstrSource
= ERRORINFO_SysAllocString(This
->bstrSource
);
238 static HRESULT WINAPI
IErrorInfoImpl_GetDescription(
240 BSTR
*pBstrDescription
)
242 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
244 TRACE("(%p)->(pBstrDescription=%p)\n",This
,pBstrDescription
);
245 if (pBstrDescription
== NULL
)
247 *pBstrDescription
= ERRORINFO_SysAllocString(This
->bstrDescription
);
252 static HRESULT WINAPI
IErrorInfoImpl_GetHelpFile(
256 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
258 TRACE("(%p)->(pBstrHelpFile=%p)\n",This
, pBstrHelpFile
);
259 if (pBstrHelpFile
== NULL
)
261 *pBstrHelpFile
= ERRORINFO_SysAllocString(This
->bstrHelpFile
);
266 static HRESULT WINAPI
IErrorInfoImpl_GetHelpContext(
268 DWORD
*pdwHelpContext
)
270 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
271 TRACE("(%p)->(pdwHelpContext=%p)\n",This
, pdwHelpContext
);
272 if (pdwHelpContext
== NULL
)
274 *pdwHelpContext
= This
->m_dwHelpContext
;
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
,
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
,
320 ErrorInfoImpl
*This
= impl_from_ICreateErrorInfo(iface
);
321 TRACE("(%p)->(%s)\n", This
, debugstr_guid(rguid
));
322 This
->m_Guid
= *rguid
;
326 static HRESULT WINAPI
ICreateErrorInfoImpl_SetSource(
327 ICreateErrorInfo
* iface
,
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
);
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
);
352 static HRESULT WINAPI
ICreateErrorInfoImpl_SetHelpFile(
353 ICreateErrorInfo
* iface
,
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
);
364 static HRESULT WINAPI
ICreateErrorInfoImpl_SetHelpContext(
365 ICreateErrorInfo
* iface
,
368 ErrorInfoImpl
*This
= impl_from_ICreateErrorInfo(iface
);
369 TRACE("(%p,%d)\n",This
,dwHelpContext
);
370 This
->m_dwHelpContext
= dwHelpContext
;
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
,
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
,
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
;
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.
448 * pperrinfo [O]. Address where error info creation object will be stored.
452 * Failure: HRESULT code.
454 HRESULT WINAPI
CreateErrorInfo(ICreateErrorInfo
**pperrinfo
)
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
);
467 /***********************************************************************
468 * GetErrorInfo (OLE32.@)
470 * Retrieves the error information object for the current thread.
473 * dwReserved [I]. Reserved. Must be zero.
474 * pperrinfo [O]. Address where error information object will be stored on return.
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.
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
);
491 ERR("dwReserved (0x%x) != 0\n", dwReserved
);
495 if(!pperrinfo
) return E_INVALIDARG
;
497 if (!COM_CurrentInfo()->errorinfo
)
503 *pperrinfo
= COM_CurrentInfo()->errorinfo
;
505 /* clear thread error state */
506 COM_CurrentInfo()->errorinfo
= NULL
;
510 /***********************************************************************
511 * SetErrorInfo (OLE32.@)
513 * Sets the error information object for the current thread.
516 * dwReserved [I] Reserved. Must be zero.
517 * perrinfo [I] Error info object.
521 * Failure: E_INVALIDARG if dwReserved is not zero.
523 HRESULT WINAPI
SetErrorInfo(ULONG dwReserved
, IErrorInfo
*perrinfo
)
527 TRACE("(%d, %p)\n", dwReserved
, perrinfo
);
531 ERR("dwReserved (0x%x) != 0\n", dwReserved
);
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
);