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 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
)
61 size
= (strlenW(str
)+1)*sizeof(WCHAR
);
62 ret
= heap_alloc(size
);
64 memcpy(ret
, str
, size
);
70 typedef struct ErrorInfoImpl
72 IErrorInfo IErrorInfo_iface
;
73 ICreateErrorInfo ICreateErrorInfo_iface
;
74 ISupportErrorInfo ISupportErrorInfo_iface
;
81 DWORD m_dwHelpContext
;
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(
104 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
105 TRACE("(%p)->(%s,%p)\n", This
, debugstr_guid(riid
),ppvoid
);
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
;
124 IUnknown_AddRef( (IUnknown
*)*ppvoid
);
125 TRACE("-- Interface: (%p)->(%p)\n",ppvoid
,*ppvoid
);
128 TRACE("-- Interface: E_NOINTERFACE\n");
129 return E_NOINTERFACE
;
132 static ULONG WINAPI
IErrorInfoImpl_AddRef(
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(
143 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
144 ULONG ref
= InterlockedDecrement(&This
->ref
);
146 TRACE("(%p)->(count=%u)\n",This
,ref
+1);
150 TRACE("-- destroying IErrorInfo(%p)\n",This
);
152 heap_free(This
->source
);
153 heap_free(This
->description
);
154 heap_free(This
->help_file
);
160 static HRESULT WINAPI
IErrorInfoImpl_GetGUID(
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
;
171 static HRESULT WINAPI
IErrorInfoImpl_GetSource(
175 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
176 TRACE("(%p)->(pBstrSource=%p)\n",This
,pBstrSource
);
177 if (pBstrSource
== NULL
)
179 *pBstrSource
= SysAllocString(This
->source
);
183 static HRESULT WINAPI
IErrorInfoImpl_GetDescription(
185 BSTR
*pBstrDescription
)
187 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
189 TRACE("(%p)->(pBstrDescription=%p)\n",This
,pBstrDescription
);
190 if (pBstrDescription
== NULL
)
192 *pBstrDescription
= SysAllocString(This
->description
);
197 static HRESULT WINAPI
IErrorInfoImpl_GetHelpFile(
201 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
203 TRACE("(%p)->(pBstrHelpFile=%p)\n",This
, pBstrHelpFile
);
204 if (pBstrHelpFile
== NULL
)
206 *pBstrHelpFile
= SysAllocString(This
->help_file
);
211 static HRESULT WINAPI
IErrorInfoImpl_GetHelpContext(
213 DWORD
*pdwHelpContext
)
215 ErrorInfoImpl
*This
= impl_from_IErrorInfo(iface
);
216 TRACE("(%p)->(pdwHelpContext=%p)\n",This
, pdwHelpContext
);
217 if (pdwHelpContext
== NULL
)
219 *pdwHelpContext
= This
->m_dwHelpContext
;
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
,
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
,
265 ErrorInfoImpl
*This
= impl_from_ICreateErrorInfo(iface
);
266 TRACE("(%p)->(%s)\n", This
, debugstr_guid(rguid
));
267 This
->m_Guid
= *rguid
;
271 static HRESULT WINAPI
ICreateErrorInfoImpl_SetSource(
272 ICreateErrorInfo
* iface
,
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
);
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
);
296 static HRESULT WINAPI
ICreateErrorInfoImpl_SetHelpFile(
297 ICreateErrorInfo
* iface
,
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
);
307 static HRESULT WINAPI
ICreateErrorInfoImpl_SetHelpContext(
308 ICreateErrorInfo
* iface
,
311 ErrorInfoImpl
*This
= impl_from_ICreateErrorInfo(iface
);
312 TRACE("(%p,%d)\n",This
,dwHelpContext
);
313 This
->m_dwHelpContext
= dwHelpContext
;
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
,
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
,
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
;
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.
391 * pperrinfo [O]. Address where error info creation object will be stored.
395 * Failure: HRESULT code.
397 HRESULT WINAPI
CreateErrorInfo(ICreateErrorInfo
**pperrinfo
)
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
);
410 /***********************************************************************
411 * GetErrorInfo (OLE32.@)
413 * Retrieves the error information object for the current thread.
416 * dwReserved [I]. Reserved. Must be zero.
417 * pperrinfo [O]. Address where error information object will be stored on return.
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.
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
);
434 ERR("dwReserved (0x%x) != 0\n", dwReserved
);
438 if(!pperrinfo
) return E_INVALIDARG
;
440 if (!COM_CurrentInfo()->errorinfo
)
446 *pperrinfo
= COM_CurrentInfo()->errorinfo
;
448 /* clear thread error state */
449 COM_CurrentInfo()->errorinfo
= NULL
;
453 /***********************************************************************
454 * SetErrorInfo (OLE32.@)
456 * Sets the error information object for the current thread.
459 * dwReserved [I] Reserved. Must be zero.
460 * perrinfo [I] Error info object.
464 * Failure: E_INVALIDARG if dwReserved is not zero.
466 HRESULT WINAPI
SetErrorInfo(ULONG dwReserved
, IErrorInfo
*perrinfo
)
470 TRACE("(%d, %p)\n", dwReserved
, perrinfo
);
474 ERR("dwReserved (0x%x) != 0\n", dwReserved
);
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
);