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
33 #include "wine/obj_base.h"
34 #include "wine/obj_oleaut.h"
35 #include "wine/obj_errorinfo.h"
36 #include "wine/unicode.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole
);
43 /* this code is from SysAllocStringLen (ole2disp.c in oleaut32) */
44 static BSTR WINAPI
ERRORINFO_SysAllocString(const OLECHAR
* in
)
54 * Find the lenth of the buffer passed-in in bytes.
57 bufferSize
= len
* sizeof (WCHAR
);
60 * Allocate a new buffer to hold the string.
61 * dont't forget to keep an empty spot at the beginning of the
62 * buffer for the character count and an extra character at the
65 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
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 ICOM_VTABLE(IErrorInfo
) *lpvtei
;
132 ICOM_VTABLE(ICreateErrorInfo
) *lpvtcei
;
133 ICOM_VTABLE(ISupportErrorInfo
) *lpvtsei
;
138 BSTR bstrDescription
;
140 DWORD m_dwHelpContext
;
143 static ICOM_VTABLE(IErrorInfo
) IErrorInfoImpl_VTable
;
144 static ICOM_VTABLE(ICreateErrorInfo
) ICreateErrorInfoImpl_VTable
;
145 static ICOM_VTABLE(ISupportErrorInfo
) ISupportErrorInfoImpl_VTable
;
148 converts a objectpointer 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 a objectpointer
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 TRACE("(%p)->(count=%lu)\n",This
,This
->ref
);
231 if (!InterlockedDecrement(&This
->ref
))
233 TRACE("-- destroying IErrorInfo(%p)\n",This
);
234 HeapFree(GetProcessHeap(),0,This
);
240 static HRESULT WINAPI
IErrorInfoImpl_GetGUID(
244 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl
, iface
);
245 TRACE("(%p)->(count=%lu)\n",This
,This
->ref
);
246 if(!pGUID
)return E_INVALIDARG
;
247 memcpy(pGUID
, &This
->m_Guid
, sizeof(GUID
));
251 static HRESULT WINAPI
IErrorInfoImpl_GetSource(
255 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl
, iface
);
256 TRACE("(%p)->(pBstrSource=%p)\n",This
,pBstrSource
);
257 if (pBstrSource
== NULL
)
259 *pBstrSource
= ERRORINFO_SysAllocString(This
->bstrSource
);
263 static HRESULT WINAPI
IErrorInfoImpl_GetDescription(
265 BSTR
*pBstrDescription
)
267 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl
, iface
);
269 TRACE("(%p)->(pBstrDescription=%p)\n",This
,pBstrDescription
);
270 if (pBstrDescription
== NULL
)
272 *pBstrDescription
= ERRORINFO_SysAllocString(This
->bstrDescription
);
277 static HRESULT WINAPI
IErrorInfoImpl_GetHelpFile(
281 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl
, iface
);
283 TRACE("(%p)->(pBstrHelpFile=%p)\n",This
, pBstrHelpFile
);
284 if (pBstrHelpFile
== NULL
)
286 *pBstrHelpFile
= ERRORINFO_SysAllocString(This
->bstrHelpFile
);
291 static HRESULT WINAPI
IErrorInfoImpl_GetHelpContext(
293 DWORD
*pdwHelpContext
)
295 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl
, iface
);
296 TRACE("(%p)->(pdwHelpContext=%p)\n",This
, pdwHelpContext
);
297 if (pdwHelpContext
== NULL
)
299 *pdwHelpContext
= This
->m_dwHelpContext
;
304 static ICOM_VTABLE(IErrorInfo
) IErrorInfoImpl_VTable
=
306 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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 ICOM_VTABLE(ICreateErrorInfo
) ICreateErrorInfoImpl_VTable
=
408 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
409 ICreateErrorInfoImpl_QueryInterface
,
410 ICreateErrorInfoImpl_AddRef
,
411 ICreateErrorInfoImpl_Release
,
413 ICreateErrorInfoImpl_SetGUID
,
414 ICreateErrorInfoImpl_SetSource
,
415 ICreateErrorInfoImpl_SetDescription
,
416 ICreateErrorInfoImpl_SetHelpFile
,
417 ICreateErrorInfoImpl_SetHelpContext
420 static HRESULT WINAPI
ISupportErrorInfoImpl_QueryInterface(
421 ISupportErrorInfo
* iface
,
425 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl
, iface
);
426 TRACE("(%p)\n", This
);
428 return IErrorInfo_QueryInterface(_IErrorInfo_(This
), riid
, ppvoid
);
431 static ULONG WINAPI
ISupportErrorInfoImpl_AddRef(
432 ISupportErrorInfo
* iface
)
434 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl
, iface
);
435 TRACE("(%p)\n", This
);
436 return IErrorInfo_AddRef(_IErrorInfo_(This
));
439 static ULONG WINAPI
ISupportErrorInfoImpl_Release(
440 ISupportErrorInfo
* iface
)
442 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl
, iface
);
443 TRACE("(%p)\n", This
);
444 return IErrorInfo_Release(_IErrorInfo_(This
));
448 static HRESULT WINAPI
ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
449 ISupportErrorInfo
* iface
,
452 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl
, iface
);
453 TRACE("(%p)->(%s)\n", This
, debugstr_guid(riid
));
454 return (IsEqualIID(riid
, &This
->m_Guid
)) ? S_OK
: S_FALSE
;
457 static ICOM_VTABLE(ISupportErrorInfo
) ISupportErrorInfoImpl_VTable
=
459 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
460 ISupportErrorInfoImpl_QueryInterface
,
461 ISupportErrorInfoImpl_AddRef
,
462 ISupportErrorInfoImpl_Release
,
465 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
467 /***********************************************************************
468 * CreateErrorInfo (OLE32.192)
470 HRESULT WINAPI
CreateErrorInfo(ICreateErrorInfo
**pperrinfo
)
474 TRACE("(%p): stub:\n", pperrinfo
);
475 if(! pperrinfo
) return E_INVALIDARG
;
476 if(!(pei
=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY
;
478 res
= IErrorInfo_QueryInterface(pei
, &IID_ICreateErrorInfo
, (LPVOID
*)pperrinfo
);
479 IErrorInfo_Release(pei
);
483 /***********************************************************************
484 * GetErrorInfo (OLE32.196)
486 HRESULT WINAPI
GetErrorInfo(ULONG dwReserved
, IErrorInfo
**pperrinfo
)
488 TRACE("(%ld, %p, %p): stub:\n", dwReserved
, pperrinfo
, NtCurrentTeb()->ErrorInfo
);
490 if(! pperrinfo
) return E_INVALIDARG
;
491 if(!(*pperrinfo
= (IErrorInfo
*)(NtCurrentTeb()->ErrorInfo
))) return S_FALSE
;
493 /* clear thread error state */
494 NtCurrentTeb()->ErrorInfo
= NULL
;
498 /***********************************************************************
499 * SetErrorInfo (OLE32.255)
501 HRESULT WINAPI
SetErrorInfo(ULONG dwReserved
, IErrorInfo
*perrinfo
)
504 TRACE("(%ld, %p): stub:\n", dwReserved
, perrinfo
);
506 /* release old errorinfo */
507 pei
= (IErrorInfo
*)NtCurrentTeb()->ErrorInfo
;
508 if(pei
) IErrorInfo_Release(pei
);
510 /* set to new value */
511 NtCurrentTeb()->ErrorInfo
= perrinfo
;
512 if(perrinfo
) IErrorInfo_AddRef(perrinfo
);