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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 WINAPI ERRORINFO_SysAllocString(const OLECHAR* in)
55 * Find the lenth 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.
87 * Since it is valid to pass a NULL pointer here, we'll initialize the
88 * buffer to nul if it is the case.
91 memcpy(newBuffer, in, bufferSize);
93 memset(newBuffer, 0, bufferSize);
96 * Make sure that there is a nul character at the end of the
99 stringBuffer = (WCHAR*)newBuffer;
100 stringBuffer[len] = 0;
102 return (LPWSTR)stringBuffer;
105 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
106 static VOID WINAPI ERRORINFO_SysFreeString(BSTR in)
108 DWORD* bufferPointer;
110 /* NULL is a valid parameter */
114 * We have to be careful when we free a BSTR pointer, it points to
115 * the beginning of the string but it skips the byte count contained
118 bufferPointer = (DWORD*)in;
123 * Free the memory from it's "real" origin.
125 HeapFree(GetProcessHeap(), 0, bufferPointer);
129 typedef struct ErrorInfoImpl
131 IErrorInfoVtbl *lpvtei;
132 ICreateErrorInfoVtbl *lpvtcei;
133 ISupportErrorInfoVtbl *lpvtsei;
138 BSTR bstrDescription;
140 DWORD m_dwHelpContext;
143 static IErrorInfoVtbl IErrorInfoImpl_VTable;
144 static ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable;
145 static ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable;
148 converts an object pointer to This
150 #define _IErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtei)))
151 #define _ICOM_THIS_From_IErrorInfo(class, name) class* This = (class*)(((char*)name)-_IErrorInfo_Offset)
153 #define _ICreateErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtcei)))
154 #define _ICOM_THIS_From_ICreateErrorInfo(class, name) class* This = (class*)(((char*)name)-_ICreateErrorInfo_Offset)
156 #define _ISupportErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtsei)))
157 #define _ICOM_THIS_From_ISupportErrorInfo(class, name) class* This = (class*)(((char*)name)-_ISupportErrorInfo_Offset)
160 converts This to an object pointer
162 #define _IErrorInfo_(This) (IErrorInfo*)&(This->lpvtei)
163 #define _ICreateErrorInfo_(This) (ICreateErrorInfo*)&(This->lpvtcei)
164 #define _ISupportErrorInfo_(This) (ISupportErrorInfo*)&(This->lpvtsei)
166 IErrorInfo * IErrorInfoImpl_Constructor()
168 ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
171 ei->lpvtei = &IErrorInfoImpl_VTable;
172 ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
173 ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
175 ei->bstrSource = NULL;
176 ei->bstrDescription = NULL;
177 ei->bstrHelpFile = NULL;
178 ei->m_dwHelpContext = 0;
180 return (IErrorInfo *)ei;
184 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
189 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
190 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);
194 if(IsEqualIID(riid, &IID_IErrorInfo))
196 *ppvoid = _IErrorInfo_(This);
198 else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
200 *ppvoid = _ICreateErrorInfo_(This);
202 else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
204 *ppvoid = _ISupportErrorInfo_(This);
209 IUnknown_AddRef( (IUnknown*)*ppvoid );
210 TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
213 TRACE("-- Interface: E_NOINTERFACE\n");
214 return E_NOINTERFACE;
217 static ULONG WINAPI IErrorInfoImpl_AddRef(
220 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
221 TRACE("(%p)->(count=%lu)\n",This,This->ref);
222 return InterlockedIncrement(&This->ref);
225 static ULONG WINAPI IErrorInfoImpl_Release(
228 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
229 ULONG ref = InterlockedDecrement(&This->ref);
231 TRACE("(%p)->(count=%lu)\n",This,ref+1);
235 TRACE("-- destroying IErrorInfo(%p)\n",This);
236 HeapFree(GetProcessHeap(),0,This);
242 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
246 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
247 TRACE("(%p)->(count=%lu)\n",This,This->ref);
248 if(!pGUID )return E_INVALIDARG;
249 memcpy(pGUID, &This->m_Guid, sizeof(GUID));
253 static HRESULT WINAPI IErrorInfoImpl_GetSource(
257 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
258 TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
259 if (pBstrSource == NULL)
261 *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
265 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
267 BSTR *pBstrDescription)
269 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
271 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
272 if (pBstrDescription == NULL)
274 *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
279 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
283 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
285 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
286 if (pBstrHelpFile == NULL)
288 *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
293 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
295 DWORD *pdwHelpContext)
297 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
298 TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
299 if (pdwHelpContext == NULL)
301 *pdwHelpContext = This->m_dwHelpContext;
306 static IErrorInfoVtbl IErrorInfoImpl_VTable =
308 IErrorInfoImpl_QueryInterface,
309 IErrorInfoImpl_AddRef,
310 IErrorInfoImpl_Release,
312 IErrorInfoImpl_GetGUID,
313 IErrorInfoImpl_GetSource,
314 IErrorInfoImpl_GetDescription,
315 IErrorInfoImpl_GetHelpFile,
316 IErrorInfoImpl_GetHelpContext
320 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
321 ICreateErrorInfo* iface,
325 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
326 TRACE("(%p)\n", This);
327 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
330 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
331 ICreateErrorInfo* iface)
333 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
334 TRACE("(%p)\n", This);
335 return IErrorInfo_AddRef(_IErrorInfo_(This));
338 static ULONG WINAPI ICreateErrorInfoImpl_Release(
339 ICreateErrorInfo* iface)
341 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
342 TRACE("(%p)\n", This);
343 return IErrorInfo_Release(_IErrorInfo_(This));
347 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
348 ICreateErrorInfo* iface,
351 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
352 TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
353 memcpy(&This->m_Guid, rguid, sizeof(GUID));
357 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
358 ICreateErrorInfo* iface,
361 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
362 TRACE("(%p): %s\n",This, debugstr_w(szSource));
363 if (This->bstrSource != NULL)
364 ERRORINFO_SysFreeString(This->bstrSource);
365 This->bstrSource = ERRORINFO_SysAllocString(szSource);
370 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
371 ICreateErrorInfo* iface,
372 LPOLESTR szDescription)
374 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
375 TRACE("(%p): %s\n",This, debugstr_w(szDescription));
376 if (This->bstrDescription != NULL)
377 ERRORINFO_SysFreeString(This->bstrDescription);
378 This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
383 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
384 ICreateErrorInfo* iface,
387 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
388 TRACE("(%p,%s)\n",This,debugstr_w(szHelpFile));
389 if (This->bstrHelpFile != NULL)
390 ERRORINFO_SysFreeString(This->bstrHelpFile);
391 This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
395 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
396 ICreateErrorInfo* iface,
399 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
400 TRACE("(%p,%ld)\n",This,dwHelpContext);
401 This->m_dwHelpContext = dwHelpContext;
405 static ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable =
407 ICreateErrorInfoImpl_QueryInterface,
408 ICreateErrorInfoImpl_AddRef,
409 ICreateErrorInfoImpl_Release,
411 ICreateErrorInfoImpl_SetGUID,
412 ICreateErrorInfoImpl_SetSource,
413 ICreateErrorInfoImpl_SetDescription,
414 ICreateErrorInfoImpl_SetHelpFile,
415 ICreateErrorInfoImpl_SetHelpContext
418 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
419 ISupportErrorInfo* iface,
423 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
424 TRACE("(%p)\n", This);
426 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
429 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
430 ISupportErrorInfo* iface)
432 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
433 TRACE("(%p)\n", This);
434 return IErrorInfo_AddRef(_IErrorInfo_(This));
437 static ULONG WINAPI ISupportErrorInfoImpl_Release(
438 ISupportErrorInfo* iface)
440 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
441 TRACE("(%p)\n", This);
442 return IErrorInfo_Release(_IErrorInfo_(This));
446 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
447 ISupportErrorInfo* iface,
450 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
451 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
452 return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
455 static ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable =
457 ISupportErrorInfoImpl_QueryInterface,
458 ISupportErrorInfoImpl_AddRef,
459 ISupportErrorInfoImpl_Release,
462 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
464 /***********************************************************************
465 * CreateErrorInfo (OLE32.@)
467 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
471 TRACE("(%p): stub:\n", pperrinfo);
472 if(! pperrinfo ) return E_INVALIDARG;
473 if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
475 res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
476 IErrorInfo_Release(pei);
480 /***********************************************************************
481 * GetErrorInfo (OLE32.@)
483 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
485 TRACE("(%ld, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->errorinfo);
487 if(!pperrinfo) return E_INVALIDARG;
489 if (!COM_CurrentInfo()->errorinfo)
495 *pperrinfo = COM_CurrentInfo()->errorinfo;
497 /* clear thread error state */
498 COM_CurrentInfo()->errorinfo = NULL;
502 /***********************************************************************
503 * SetErrorInfo (OLE32.@)
505 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
509 TRACE("(%ld, %p)\n", dwReserved, perrinfo);
511 /* release old errorinfo */
512 pei = COM_CurrentInfo()->errorinfo;
513 if (pei) IErrorInfo_Release(pei);
515 /* set to new value */
516 COM_CurrentInfo()->errorinfo = perrinfo;
517 if (perrinfo) IErrorInfo_AddRef(perrinfo);