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
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 = (DWORD*)HeapAlloc(GetProcessHeap(),
68 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
71 * If the memory allocation failed, return a null pointer.
77 * Copy the length of the string in the placeholder.
79 *newBuffer = bufferSize;
82 * Skip the byte count.
87 * Copy the information in the buffer.
88 * Since it is valid to pass a NULL pointer here, we'll initialize the
89 * buffer to nul if it is the case.
92 memcpy(newBuffer, in, bufferSize);
94 memset(newBuffer, 0, bufferSize);
97 * Make sure that there is a nul character at the end of the
100 stringBuffer = (WCHAR*)newBuffer;
101 stringBuffer[len] = 0;
103 return (LPWSTR)stringBuffer;
106 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
107 static VOID WINAPI ERRORINFO_SysFreeString(BSTR in)
109 DWORD* bufferPointer;
111 /* NULL is a valid parameter */
115 * We have to be careful when we free a BSTR pointer, it points to
116 * the beginning of the string but it skips the byte count contained
119 bufferPointer = (DWORD*)in;
124 * Free the memory from it's "real" origin.
126 HeapFree(GetProcessHeap(), 0, bufferPointer);
130 typedef struct ErrorInfoImpl
132 IErrorInfoVtbl *lpvtei;
133 ICreateErrorInfoVtbl *lpvtcei;
134 ISupportErrorInfoVtbl *lpvtsei;
139 BSTR bstrDescription;
141 DWORD m_dwHelpContext;
144 static IErrorInfoVtbl IErrorInfoImpl_VTable;
145 static ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable;
146 static ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable;
149 converts a objectpointer to This
151 #define _IErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtei)))
152 #define _ICOM_THIS_From_IErrorInfo(class, name) class* This = (class*)(((char*)name)-_IErrorInfo_Offset);
154 #define _ICreateErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtcei)))
155 #define _ICOM_THIS_From_ICreateErrorInfo(class, name) class* This = (class*)(((char*)name)-_ICreateErrorInfo_Offset);
157 #define _ISupportErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtsei)))
158 #define _ICOM_THIS_From_ISupportErrorInfo(class, name) class* This = (class*)(((char*)name)-_ISupportErrorInfo_Offset);
161 converts This to a objectpointer
163 #define _IErrorInfo_(This) (IErrorInfo*)&(This->lpvtei)
164 #define _ICreateErrorInfo_(This) (ICreateErrorInfo*)&(This->lpvtcei)
165 #define _ISupportErrorInfo_(This) (ISupportErrorInfo*)&(This->lpvtsei)
167 IErrorInfo * IErrorInfoImpl_Constructor()
169 ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
172 ei->lpvtei = &IErrorInfoImpl_VTable;
173 ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
174 ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
176 ei->bstrSource = NULL;
177 ei->bstrDescription = NULL;
178 ei->bstrHelpFile = NULL;
179 ei->m_dwHelpContext = 0;
181 return (IErrorInfo *)ei;
185 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
190 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
191 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);
195 if(IsEqualIID(riid, &IID_IErrorInfo))
197 *ppvoid = _IErrorInfo_(This);
199 else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
201 *ppvoid = _ICreateErrorInfo_(This);
203 else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
205 *ppvoid = _ISupportErrorInfo_(This);
210 IUnknown_AddRef( (IUnknown*)*ppvoid );
211 TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
214 TRACE("-- Interface: E_NOINTERFACE\n");
215 return E_NOINTERFACE;
218 static ULONG WINAPI IErrorInfoImpl_AddRef(
221 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
222 TRACE("(%p)->(count=%lu)\n",This,This->ref);
223 return InterlockedIncrement(&This->ref);
226 static ULONG WINAPI IErrorInfoImpl_Release(
229 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
230 TRACE("(%p)->(count=%lu)\n",This,This->ref);
232 if (!InterlockedDecrement(&This->ref))
234 TRACE("-- destroying IErrorInfo(%p)\n",This);
235 HeapFree(GetProcessHeap(),0,This);
241 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
245 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
246 TRACE("(%p)->(count=%lu)\n",This,This->ref);
247 if(!pGUID )return E_INVALIDARG;
248 memcpy(pGUID, &This->m_Guid, sizeof(GUID));
252 static HRESULT WINAPI IErrorInfoImpl_GetSource(
256 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
257 TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
258 if (pBstrSource == NULL)
260 *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
264 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
266 BSTR *pBstrDescription)
268 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
270 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
271 if (pBstrDescription == NULL)
273 *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
278 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
282 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
284 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
285 if (pBstrHelpFile == NULL)
287 *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
292 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
294 DWORD *pdwHelpContext)
296 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
297 TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
298 if (pdwHelpContext == NULL)
300 *pdwHelpContext = This->m_dwHelpContext;
305 static IErrorInfoVtbl IErrorInfoImpl_VTable =
307 IErrorInfoImpl_QueryInterface,
308 IErrorInfoImpl_AddRef,
309 IErrorInfoImpl_Release,
311 IErrorInfoImpl_GetGUID,
312 IErrorInfoImpl_GetSource,
313 IErrorInfoImpl_GetDescription,
314 IErrorInfoImpl_GetHelpFile,
315 IErrorInfoImpl_GetHelpContext
319 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
320 ICreateErrorInfo* iface,
324 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
325 TRACE("(%p)\n", This);
326 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
329 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
330 ICreateErrorInfo* iface)
332 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
333 TRACE("(%p)\n", This);
334 return IErrorInfo_AddRef(_IErrorInfo_(This));
337 static ULONG WINAPI ICreateErrorInfoImpl_Release(
338 ICreateErrorInfo* iface)
340 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
341 TRACE("(%p)\n", This);
342 return IErrorInfo_Release(_IErrorInfo_(This));
346 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
347 ICreateErrorInfo* iface,
350 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
351 TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
352 memcpy(&This->m_Guid, rguid, sizeof(GUID));
356 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
357 ICreateErrorInfo* iface,
360 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
361 TRACE("(%p)\n",This);
362 if (This->bstrSource != NULL)
363 ERRORINFO_SysFreeString(This->bstrSource);
364 This->bstrSource = ERRORINFO_SysAllocString(szSource);
369 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
370 ICreateErrorInfo* iface,
371 LPOLESTR szDescription)
373 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
374 TRACE("(%p)\n",This);
375 if (This->bstrDescription != NULL)
376 ERRORINFO_SysFreeString(This->bstrDescription);
377 This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
382 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
383 ICreateErrorInfo* iface,
386 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
387 TRACE("(%p)\n",This);
388 if (This->bstrHelpFile != NULL)
389 ERRORINFO_SysFreeString(This->bstrHelpFile);
390 This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
395 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
396 ICreateErrorInfo* iface,
399 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
400 TRACE("(%p)\n",This);
401 This->m_dwHelpContext = dwHelpContext;
406 static ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable =
408 ICreateErrorInfoImpl_QueryInterface,
409 ICreateErrorInfoImpl_AddRef,
410 ICreateErrorInfoImpl_Release,
412 ICreateErrorInfoImpl_SetGUID,
413 ICreateErrorInfoImpl_SetSource,
414 ICreateErrorInfoImpl_SetDescription,
415 ICreateErrorInfoImpl_SetHelpFile,
416 ICreateErrorInfoImpl_SetHelpContext
419 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
420 ISupportErrorInfo* iface,
424 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
425 TRACE("(%p)\n", This);
427 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
430 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
431 ISupportErrorInfo* iface)
433 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
434 TRACE("(%p)\n", This);
435 return IErrorInfo_AddRef(_IErrorInfo_(This));
438 static ULONG WINAPI ISupportErrorInfoImpl_Release(
439 ISupportErrorInfo* iface)
441 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
442 TRACE("(%p)\n", This);
443 return IErrorInfo_Release(_IErrorInfo_(This));
447 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
448 ISupportErrorInfo* iface,
451 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
452 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
453 return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
456 static ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable =
458 ISupportErrorInfoImpl_QueryInterface,
459 ISupportErrorInfoImpl_AddRef,
460 ISupportErrorInfoImpl_Release,
463 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
465 /***********************************************************************
466 * CreateErrorInfo (OLE32.@)
468 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
472 TRACE("(%p): stub:\n", pperrinfo);
473 if(! pperrinfo ) return E_INVALIDARG;
474 if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
476 res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
477 IErrorInfo_Release(pei);
481 /***********************************************************************
482 * GetErrorInfo (OLE32.@)
484 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
486 APARTMENT * apt = COM_CurrentInfo();
488 TRACE("(%ld, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->ErrorInfo);
490 if(!pperrinfo) return E_INVALIDARG;
491 if (!apt || !apt->ErrorInfo)
497 *pperrinfo = (IErrorInfo*)(apt->ErrorInfo);
498 /* clear thread error state */
499 apt->ErrorInfo = NULL;
503 /***********************************************************************
504 * SetErrorInfo (OLE32.@)
506 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
509 APARTMENT * apt = COM_CurrentInfo();
511 TRACE("(%ld, %p)\n", dwReserved, perrinfo);
513 if (!apt) apt = COM_CreateApartment(COINIT_UNINITIALIZED);
515 /* release old errorinfo */
516 pei = (IErrorInfo*)apt->ErrorInfo;
517 if(pei) IErrorInfo_Release(pei);
519 /* set to new value */
520 apt->ErrorInfo = perrinfo;
521 if(perrinfo) IErrorInfo_AddRef(perrinfo);