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
34 #include "wine/unicode.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41 /* this code is from SysAllocStringLen (ole2disp.c in oleaut32) */
42 static BSTR WINAPI ERRORINFO_SysAllocString(const OLECHAR* in)
52 * Find the lenth of the buffer passed-in in bytes.
55 bufferSize = len * sizeof (WCHAR);
58 * Allocate a new buffer to hold the string.
59 * dont't forget to keep an empty spot at the beginning of the
60 * buffer for the character count and an extra character at the
63 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
65 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
68 * If the memory allocation failed, return a null pointer.
74 * Copy the length of the string in the placeholder.
76 *newBuffer = bufferSize;
79 * Skip the byte count.
84 * Copy the information in the buffer.
85 * Since it is valid to pass a NULL pointer here, we'll initialize the
86 * buffer to nul if it is the case.
89 memcpy(newBuffer, in, bufferSize);
91 memset(newBuffer, 0, bufferSize);
94 * Make sure that there is a nul character at the end of the
97 stringBuffer = (WCHAR*)newBuffer;
98 stringBuffer[len] = 0;
100 return (LPWSTR)stringBuffer;
103 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
104 static VOID WINAPI ERRORINFO_SysFreeString(BSTR in)
106 DWORD* bufferPointer;
108 /* NULL is a valid parameter */
112 * We have to be careful when we free a BSTR pointer, it points to
113 * the beginning of the string but it skips the byte count contained
116 bufferPointer = (DWORD*)in;
121 * Free the memory from it's "real" origin.
123 HeapFree(GetProcessHeap(), 0, bufferPointer);
127 typedef struct ErrorInfoImpl
129 ICOM_VTABLE(IErrorInfo) *lpvtei;
130 ICOM_VTABLE(ICreateErrorInfo) *lpvtcei;
131 ICOM_VTABLE(ISupportErrorInfo) *lpvtsei;
136 BSTR bstrDescription;
138 DWORD m_dwHelpContext;
141 static ICOM_VTABLE(IErrorInfo) IErrorInfoImpl_VTable;
142 static ICOM_VTABLE(ICreateErrorInfo) ICreateErrorInfoImpl_VTable;
143 static ICOM_VTABLE(ISupportErrorInfo) ISupportErrorInfoImpl_VTable;
146 converts a objectpointer to This
148 #define _IErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtei)))
149 #define _ICOM_THIS_From_IErrorInfo(class, name) class* This = (class*)(((char*)name)-_IErrorInfo_Offset);
151 #define _ICreateErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtcei)))
152 #define _ICOM_THIS_From_ICreateErrorInfo(class, name) class* This = (class*)(((char*)name)-_ICreateErrorInfo_Offset);
154 #define _ISupportErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtsei)))
155 #define _ICOM_THIS_From_ISupportErrorInfo(class, name) class* This = (class*)(((char*)name)-_ISupportErrorInfo_Offset);
158 converts This to a objectpointer
160 #define _IErrorInfo_(This) (IErrorInfo*)&(This->lpvtei)
161 #define _ICreateErrorInfo_(This) (ICreateErrorInfo*)&(This->lpvtcei)
162 #define _ISupportErrorInfo_(This) (ISupportErrorInfo*)&(This->lpvtsei)
164 IErrorInfo * IErrorInfoImpl_Constructor()
166 ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
169 ei->lpvtei = &IErrorInfoImpl_VTable;
170 ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
171 ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
173 ei->bstrSource = NULL;
174 ei->bstrDescription = NULL;
175 ei->bstrHelpFile = NULL;
176 ei->m_dwHelpContext = 0;
178 return (IErrorInfo *)ei;
182 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
187 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
188 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);
192 if(IsEqualIID(riid, &IID_IErrorInfo))
194 *ppvoid = _IErrorInfo_(This);
196 else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
198 *ppvoid = _ICreateErrorInfo_(This);
200 else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
202 *ppvoid = _ISupportErrorInfo_(This);
207 IUnknown_AddRef( (IUnknown*)*ppvoid );
208 TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
211 TRACE("-- Interface: E_NOINTERFACE\n");
212 return E_NOINTERFACE;
215 static ULONG WINAPI IErrorInfoImpl_AddRef(
218 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
219 TRACE("(%p)->(count=%lu)\n",This,This->ref);
220 return InterlockedIncrement(&This->ref);
223 static ULONG WINAPI IErrorInfoImpl_Release(
226 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
227 TRACE("(%p)->(count=%lu)\n",This,This->ref);
229 if (!InterlockedDecrement(&This->ref))
231 TRACE("-- destroying IErrorInfo(%p)\n",This);
232 HeapFree(GetProcessHeap(),0,This);
238 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
242 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
243 TRACE("(%p)->(count=%lu)\n",This,This->ref);
244 if(!pGUID )return E_INVALIDARG;
245 memcpy(pGUID, &This->m_Guid, sizeof(GUID));
249 static HRESULT WINAPI IErrorInfoImpl_GetSource(
253 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
254 TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
255 if (pBstrSource == NULL)
257 *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
261 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
263 BSTR *pBstrDescription)
265 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
267 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
268 if (pBstrDescription == NULL)
270 *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
275 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
279 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
281 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
282 if (pBstrHelpFile == NULL)
284 *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
289 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
291 DWORD *pdwHelpContext)
293 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
294 TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
295 if (pdwHelpContext == NULL)
297 *pdwHelpContext = This->m_dwHelpContext;
302 static ICOM_VTABLE(IErrorInfo) IErrorInfoImpl_VTable =
304 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
305 IErrorInfoImpl_QueryInterface,
306 IErrorInfoImpl_AddRef,
307 IErrorInfoImpl_Release,
309 IErrorInfoImpl_GetGUID,
310 IErrorInfoImpl_GetSource,
311 IErrorInfoImpl_GetDescription,
312 IErrorInfoImpl_GetHelpFile,
313 IErrorInfoImpl_GetHelpContext
317 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
318 ICreateErrorInfo* iface,
322 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
323 TRACE("(%p)\n", This);
324 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
327 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
328 ICreateErrorInfo* iface)
330 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
331 TRACE("(%p)\n", This);
332 return IErrorInfo_AddRef(_IErrorInfo_(This));
335 static ULONG WINAPI ICreateErrorInfoImpl_Release(
336 ICreateErrorInfo* iface)
338 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
339 TRACE("(%p)\n", This);
340 return IErrorInfo_Release(_IErrorInfo_(This));
344 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
345 ICreateErrorInfo* iface,
348 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
349 TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
350 memcpy(&This->m_Guid, rguid, sizeof(GUID));
354 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
355 ICreateErrorInfo* iface,
358 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
359 TRACE("(%p)\n",This);
360 if (This->bstrSource != NULL)
361 ERRORINFO_SysFreeString(This->bstrSource);
362 This->bstrSource = ERRORINFO_SysAllocString(szSource);
367 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
368 ICreateErrorInfo* iface,
369 LPOLESTR szDescription)
371 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
372 TRACE("(%p)\n",This);
373 if (This->bstrDescription != NULL)
374 ERRORINFO_SysFreeString(This->bstrDescription);
375 This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
380 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
381 ICreateErrorInfo* iface,
384 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
385 TRACE("(%p)\n",This);
386 if (This->bstrHelpFile != NULL)
387 ERRORINFO_SysFreeString(This->bstrHelpFile);
388 This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
393 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
394 ICreateErrorInfo* iface,
397 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
398 TRACE("(%p)\n",This);
399 This->m_dwHelpContext = dwHelpContext;
404 static ICOM_VTABLE(ICreateErrorInfo) ICreateErrorInfoImpl_VTable =
406 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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 ICOM_VTABLE(ISupportErrorInfo) ISupportErrorInfoImpl_VTable =
457 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
458 ISupportErrorInfoImpl_QueryInterface,
459 ISupportErrorInfoImpl_AddRef,
460 ISupportErrorInfoImpl_Release,
463 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
465 /***********************************************************************
466 * CreateErrorInfo (OLE32.192)
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.196)
484 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
486 TRACE("(%ld, %p, %p): stub:\n", dwReserved, pperrinfo, NtCurrentTeb()->ErrorInfo);
488 if(! pperrinfo ) return E_INVALIDARG;
489 if(!(*pperrinfo = (IErrorInfo*)(NtCurrentTeb()->ErrorInfo))) return S_FALSE;
491 /* clear thread error state */
492 NtCurrentTeb()->ErrorInfo = NULL;
496 /***********************************************************************
497 * SetErrorInfo (OLE32.255)
499 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
502 TRACE("(%ld, %p): stub:\n", dwReserved, perrinfo);
504 /* release old errorinfo */
505 pei = (IErrorInfo*)NtCurrentTeb()->ErrorInfo;
506 if(pei) IErrorInfo_Release(pei);
508 /* set to new value */
509 NtCurrentTeb()->ErrorInfo = perrinfo;
510 if(perrinfo) IErrorInfo_AddRef(perrinfo);