Release 20030408.
[wine/gsoc-2012-control.git] / dlls / ole32 / errorinfo.c
blobef45e96008c583115b0d439f3a6cbb03bd6abc55
1 /*
2 * ErrorInfo API
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
20 * NOTES:
22 * The errorinfo is a per-thread object. The reference is stored in the
23 * TEB at offset 0xf80
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "oleauto.h"
31 #include "winerror.h"
33 #include "objbase.h"
34 #include "wine/unicode.h"
35 #include "thread.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)
44 DWORD bufferSize;
45 DWORD* newBuffer;
46 WCHAR* stringBuffer;
47 DWORD len;
49 if (in == NULL)
50 return NULL;
52 * Find the lenth of the buffer passed-in in bytes.
54 len = strlenW(in);
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
61 * end for the '\0'.
63 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
65 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
68 * If the memory allocation failed, return a null pointer.
70 if (newBuffer==0)
71 return 0;
74 * Copy the length of the string in the placeholder.
76 *newBuffer = bufferSize;
79 * Skip the byte count.
81 newBuffer++;
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.
88 if (in != 0)
89 memcpy(newBuffer, in, bufferSize);
90 else
91 memset(newBuffer, 0, bufferSize);
94 * Make sure that there is a nul character at the end of the
95 * string.
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 */
109 if(!in) return;
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
114 * before the string.
116 bufferPointer = (DWORD*)in;
118 bufferPointer--;
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;
132 DWORD ref;
134 GUID m_Guid;
135 BSTR bstrSource;
136 BSTR bstrDescription;
137 BSTR bstrHelpFile;
138 DWORD m_dwHelpContext;
139 } ErrorInfoImpl;
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));
167 if (ei)
169 ei->lpvtei = &IErrorInfoImpl_VTable;
170 ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
171 ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
172 ei->ref = 1;
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(
183 IErrorInfo* iface,
184 REFIID riid,
185 VOID** ppvoid)
187 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
188 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);
190 *ppvoid = NULL;
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);
205 if(*ppvoid)
207 IUnknown_AddRef( (IUnknown*)*ppvoid );
208 TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
209 return S_OK;
211 TRACE("-- Interface: E_NOINTERFACE\n");
212 return E_NOINTERFACE;
215 static ULONG WINAPI IErrorInfoImpl_AddRef(
216 IErrorInfo* iface)
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(
224 IErrorInfo* iface)
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);
233 return 0;
235 return This->ref;
238 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
239 IErrorInfo* iface,
240 GUID * pGUID)
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));
246 return S_OK;
249 static HRESULT WINAPI IErrorInfoImpl_GetSource(
250 IErrorInfo* iface,
251 BSTR *pBstrSource)
253 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
254 TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
255 if (pBstrSource == NULL)
256 return E_INVALIDARG;
257 *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
258 return S_OK;
261 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
262 IErrorInfo* iface,
263 BSTR *pBstrDescription)
265 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
267 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
268 if (pBstrDescription == NULL)
269 return E_INVALIDARG;
270 *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
272 return S_OK;
275 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
276 IErrorInfo* iface,
277 BSTR *pBstrHelpFile)
279 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
281 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
282 if (pBstrHelpFile == NULL)
283 return E_INVALIDARG;
284 *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
286 return S_OK;
289 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
290 IErrorInfo* iface,
291 DWORD *pdwHelpContext)
293 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
294 TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
295 if (pdwHelpContext == NULL)
296 return E_INVALIDARG;
297 *pdwHelpContext = This->m_dwHelpContext;
299 return S_OK;
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,
319 REFIID riid,
320 VOID** ppvoid)
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,
346 REFGUID rguid)
348 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
349 TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
350 memcpy(&This->m_Guid, rguid, sizeof(GUID));
351 return S_OK;
354 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
355 ICreateErrorInfo* iface,
356 LPOLESTR szSource)
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);
364 return S_OK;
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);
377 return S_OK;
380 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
381 ICreateErrorInfo* iface,
382 LPOLESTR szHelpFile)
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);
390 return S_OK;
393 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
394 ICreateErrorInfo* iface,
395 DWORD dwHelpContext)
397 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
398 TRACE("(%p)\n",This);
399 This->m_dwHelpContext = dwHelpContext;
401 return S_OK;
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,
420 REFIID riid,
421 VOID** ppvoid)
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,
448 REFIID riid)
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)
470 IErrorInfo * pei;
471 HRESULT res;
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);
478 return res;
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;
493 return S_OK;
496 /***********************************************************************
497 * SetErrorInfo (OLE32.255)
499 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
501 IErrorInfo * pei;
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);
511 return S_OK;