Fixed a couple of header files ordering issues.
[wine.git] / dlls / ole32 / errorinfo.c
bloba946f4516d01fa080fe49c560920f38819b217e0
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 <stdarg.h>
27 #include <string.h>
29 #include "windef.h"
30 #include "winbase.h"
31 #include "oleauto.h"
32 #include "winerror.h"
34 #include "objbase.h"
35 #include "wine/unicode.h"
36 #include "compobj_private.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42 /* this code is from SysAllocStringLen (ole2disp.c in oleaut32) */
43 static BSTR WINAPI ERRORINFO_SysAllocString(const OLECHAR* in)
45 DWORD bufferSize;
46 DWORD* newBuffer;
47 WCHAR* stringBuffer;
48 DWORD len;
50 if (in == NULL)
51 return NULL;
53 * Find the lenth of the buffer passed-in in bytes.
55 len = strlenW(in);
56 bufferSize = len * sizeof (WCHAR);
59 * Allocate a new buffer to hold the string.
60 * don't forget to keep an empty spot at the beginning of the
61 * buffer for the character count and an extra character at the
62 * end for the '\0'.
64 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
66 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
69 * If the memory allocation failed, return a null pointer.
71 if (newBuffer==0)
72 return 0;
75 * Copy the length of the string in the placeholder.
77 *newBuffer = bufferSize;
80 * Skip the byte count.
82 newBuffer++;
85 * Copy the information in the buffer.
86 * Since it is valid to pass a NULL pointer here, we'll initialize the
87 * buffer to nul if it is the case.
89 if (in != 0)
90 memcpy(newBuffer, in, bufferSize);
91 else
92 memset(newBuffer, 0, bufferSize);
95 * Make sure that there is a nul character at the end of the
96 * string.
98 stringBuffer = (WCHAR*)newBuffer;
99 stringBuffer[len] = 0;
101 return (LPWSTR)stringBuffer;
104 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
105 static VOID WINAPI ERRORINFO_SysFreeString(BSTR in)
107 DWORD* bufferPointer;
109 /* NULL is a valid parameter */
110 if(!in) return;
113 * We have to be careful when we free a BSTR pointer, it points to
114 * the beginning of the string but it skips the byte count contained
115 * before the string.
117 bufferPointer = (DWORD*)in;
119 bufferPointer--;
122 * Free the memory from it's "real" origin.
124 HeapFree(GetProcessHeap(), 0, bufferPointer);
128 typedef struct ErrorInfoImpl
130 ICOM_VTABLE(IErrorInfo) *lpvtei;
131 ICOM_VTABLE(ICreateErrorInfo) *lpvtcei;
132 ICOM_VTABLE(ISupportErrorInfo) *lpvtsei;
133 DWORD ref;
135 GUID m_Guid;
136 BSTR bstrSource;
137 BSTR bstrDescription;
138 BSTR bstrHelpFile;
139 DWORD m_dwHelpContext;
140 } ErrorInfoImpl;
142 static ICOM_VTABLE(IErrorInfo) IErrorInfoImpl_VTable;
143 static ICOM_VTABLE(ICreateErrorInfo) ICreateErrorInfoImpl_VTable;
144 static ICOM_VTABLE(ISupportErrorInfo) ISupportErrorInfoImpl_VTable;
147 converts a objectpointer to This
149 #define _IErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtei)))
150 #define _ICOM_THIS_From_IErrorInfo(class, name) class* This = (class*)(((char*)name)-_IErrorInfo_Offset);
152 #define _ICreateErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtcei)))
153 #define _ICOM_THIS_From_ICreateErrorInfo(class, name) class* This = (class*)(((char*)name)-_ICreateErrorInfo_Offset);
155 #define _ISupportErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtsei)))
156 #define _ICOM_THIS_From_ISupportErrorInfo(class, name) class* This = (class*)(((char*)name)-_ISupportErrorInfo_Offset);
159 converts This to a objectpointer
161 #define _IErrorInfo_(This) (IErrorInfo*)&(This->lpvtei)
162 #define _ICreateErrorInfo_(This) (ICreateErrorInfo*)&(This->lpvtcei)
163 #define _ISupportErrorInfo_(This) (ISupportErrorInfo*)&(This->lpvtsei)
165 IErrorInfo * IErrorInfoImpl_Constructor()
167 ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
168 if (ei)
170 ei->lpvtei = &IErrorInfoImpl_VTable;
171 ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
172 ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
173 ei->ref = 1;
174 ei->bstrSource = NULL;
175 ei->bstrDescription = NULL;
176 ei->bstrHelpFile = NULL;
177 ei->m_dwHelpContext = 0;
179 return (IErrorInfo *)ei;
183 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
184 IErrorInfo* iface,
185 REFIID riid,
186 VOID** ppvoid)
188 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
189 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);
191 *ppvoid = NULL;
193 if(IsEqualIID(riid, &IID_IErrorInfo))
195 *ppvoid = _IErrorInfo_(This);
197 else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
199 *ppvoid = _ICreateErrorInfo_(This);
201 else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
203 *ppvoid = _ISupportErrorInfo_(This);
206 if(*ppvoid)
208 IUnknown_AddRef( (IUnknown*)*ppvoid );
209 TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
210 return S_OK;
212 TRACE("-- Interface: E_NOINTERFACE\n");
213 return E_NOINTERFACE;
216 static ULONG WINAPI IErrorInfoImpl_AddRef(
217 IErrorInfo* iface)
219 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
220 TRACE("(%p)->(count=%lu)\n",This,This->ref);
221 return InterlockedIncrement(&This->ref);
224 static ULONG WINAPI IErrorInfoImpl_Release(
225 IErrorInfo* iface)
227 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
228 TRACE("(%p)->(count=%lu)\n",This,This->ref);
230 if (!InterlockedDecrement(&This->ref))
232 TRACE("-- destroying IErrorInfo(%p)\n",This);
233 HeapFree(GetProcessHeap(),0,This);
234 return 0;
236 return This->ref;
239 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
240 IErrorInfo* iface,
241 GUID * pGUID)
243 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
244 TRACE("(%p)->(count=%lu)\n",This,This->ref);
245 if(!pGUID )return E_INVALIDARG;
246 memcpy(pGUID, &This->m_Guid, sizeof(GUID));
247 return S_OK;
250 static HRESULT WINAPI IErrorInfoImpl_GetSource(
251 IErrorInfo* iface,
252 BSTR *pBstrSource)
254 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
255 TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
256 if (pBstrSource == NULL)
257 return E_INVALIDARG;
258 *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
259 return S_OK;
262 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
263 IErrorInfo* iface,
264 BSTR *pBstrDescription)
266 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
268 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
269 if (pBstrDescription == NULL)
270 return E_INVALIDARG;
271 *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
273 return S_OK;
276 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
277 IErrorInfo* iface,
278 BSTR *pBstrHelpFile)
280 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
282 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
283 if (pBstrHelpFile == NULL)
284 return E_INVALIDARG;
285 *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
287 return S_OK;
290 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
291 IErrorInfo* iface,
292 DWORD *pdwHelpContext)
294 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
295 TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
296 if (pdwHelpContext == NULL)
297 return E_INVALIDARG;
298 *pdwHelpContext = This->m_dwHelpContext;
300 return S_OK;
303 static ICOM_VTABLE(IErrorInfo) IErrorInfoImpl_VTable =
305 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
306 IErrorInfoImpl_QueryInterface,
307 IErrorInfoImpl_AddRef,
308 IErrorInfoImpl_Release,
310 IErrorInfoImpl_GetGUID,
311 IErrorInfoImpl_GetSource,
312 IErrorInfoImpl_GetDescription,
313 IErrorInfoImpl_GetHelpFile,
314 IErrorInfoImpl_GetHelpContext
318 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
319 ICreateErrorInfo* iface,
320 REFIID riid,
321 VOID** ppvoid)
323 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
324 TRACE("(%p)\n", This);
325 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
328 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
329 ICreateErrorInfo* iface)
331 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
332 TRACE("(%p)\n", This);
333 return IErrorInfo_AddRef(_IErrorInfo_(This));
336 static ULONG WINAPI ICreateErrorInfoImpl_Release(
337 ICreateErrorInfo* iface)
339 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
340 TRACE("(%p)\n", This);
341 return IErrorInfo_Release(_IErrorInfo_(This));
345 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
346 ICreateErrorInfo* iface,
347 REFGUID rguid)
349 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
350 TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
351 memcpy(&This->m_Guid, rguid, sizeof(GUID));
352 return S_OK;
355 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
356 ICreateErrorInfo* iface,
357 LPOLESTR szSource)
359 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
360 TRACE("(%p)\n",This);
361 if (This->bstrSource != NULL)
362 ERRORINFO_SysFreeString(This->bstrSource);
363 This->bstrSource = ERRORINFO_SysAllocString(szSource);
365 return S_OK;
368 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
369 ICreateErrorInfo* iface,
370 LPOLESTR szDescription)
372 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
373 TRACE("(%p)\n",This);
374 if (This->bstrDescription != NULL)
375 ERRORINFO_SysFreeString(This->bstrDescription);
376 This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
378 return S_OK;
381 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
382 ICreateErrorInfo* iface,
383 LPOLESTR szHelpFile)
385 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
386 TRACE("(%p)\n",This);
387 if (This->bstrHelpFile != NULL)
388 ERRORINFO_SysFreeString(This->bstrHelpFile);
389 This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
391 return S_OK;
394 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
395 ICreateErrorInfo* iface,
396 DWORD dwHelpContext)
398 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
399 TRACE("(%p)\n",This);
400 This->m_dwHelpContext = dwHelpContext;
402 return S_OK;
405 static ICOM_VTABLE(ICreateErrorInfo) ICreateErrorInfoImpl_VTable =
407 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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,
421 REFIID riid,
422 VOID** ppvoid)
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,
449 REFIID riid)
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 ICOM_VTABLE(ISupportErrorInfo) ISupportErrorInfoImpl_VTable =
458 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
459 ISupportErrorInfoImpl_QueryInterface,
460 ISupportErrorInfoImpl_AddRef,
461 ISupportErrorInfoImpl_Release,
464 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
466 /***********************************************************************
467 * CreateErrorInfo (OLE32.@)
469 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
471 IErrorInfo * pei;
472 HRESULT res;
473 TRACE("(%p): stub:\n", pperrinfo);
474 if(! pperrinfo ) return E_INVALIDARG;
475 if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
477 res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
478 IErrorInfo_Release(pei);
479 return res;
482 /***********************************************************************
483 * GetErrorInfo (OLE32.@)
485 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
487 APARTMENT * apt = COM_CurrentInfo();
489 TRACE("(%ld, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->ErrorInfo);
491 if(!pperrinfo) return E_INVALIDARG;
492 if (!apt || !apt->ErrorInfo)
494 *pperrinfo = NULL;
495 return S_FALSE;
498 *pperrinfo = (IErrorInfo*)(apt->ErrorInfo);
499 /* clear thread error state */
500 apt->ErrorInfo = NULL;
501 return S_OK;
504 /***********************************************************************
505 * SetErrorInfo (OLE32.@)
507 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
509 IErrorInfo * pei;
510 APARTMENT * apt = COM_CurrentInfo();
512 TRACE("(%ld, %p)\n", dwReserved, perrinfo);
514 if (!apt) apt = COM_CreateApartment(COINIT_UNINITIALIZED);
516 /* release old errorinfo */
517 pei = (IErrorInfo*)apt->ErrorInfo;
518 if(pei) IErrorInfo_Release(pei);
520 /* set to new value */
521 apt->ErrorInfo = perrinfo;
522 if(perrinfo) IErrorInfo_AddRef(perrinfo);
523 return S_OK;