gdi32: Make clip_line external.
[wine/multimedia.git] / dlls / ole32 / errorinfo.c
blob39751ab6551af5aeec6bcffb70f66b077179a457
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #define COBJMACROS
31 #include "windef.h"
32 #include "winbase.h"
33 #include "objbase.h"
34 #include "oleauto.h"
35 #include "winerror.h"
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 ERRORINFO_SysAllocString(const OLECHAR* in)
47 DWORD bufferSize;
48 DWORD* newBuffer;
49 WCHAR* stringBuffer;
50 DWORD len;
52 if (in == NULL)
53 return NULL;
55 * Find the length of the buffer passed-in, in bytes.
57 len = strlenW(in);
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
64 * end for the '\0'.
66 newBuffer = HeapAlloc(GetProcessHeap(), 0,
67 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
70 * If the memory allocation failed, return a null pointer.
72 if (newBuffer==0)
73 return 0;
76 * Copy the length of the string in the placeholder.
78 *newBuffer = bufferSize;
81 * Skip the byte count.
83 newBuffer++;
86 * Copy the information in the buffer. It is not possible to pass
87 * a NULL pointer here.
89 memcpy(newBuffer, in, bufferSize);
92 * Make sure that there is a nul character at the end of the
93 * string.
95 stringBuffer = (WCHAR*)newBuffer;
96 stringBuffer[len] = 0;
98 return stringBuffer;
101 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
102 static VOID ERRORINFO_SysFreeString(BSTR in)
104 DWORD* bufferPointer;
106 /* NULL is a valid parameter */
107 if(!in) return;
110 * We have to be careful when we free a BSTR pointer, it points to
111 * the beginning of the string but it skips the byte count contained
112 * before the string.
114 bufferPointer = (DWORD*)in;
116 bufferPointer--;
119 * Free the memory from it's "real" origin.
121 HeapFree(GetProcessHeap(), 0, bufferPointer);
125 typedef struct ErrorInfoImpl
127 const IErrorInfoVtbl *lpvtei;
128 const ICreateErrorInfoVtbl *lpvtcei;
129 const ISupportErrorInfoVtbl *lpvtsei;
130 LONG ref;
132 GUID m_Guid;
133 BSTR bstrSource;
134 BSTR bstrDescription;
135 BSTR bstrHelpFile;
136 DWORD m_dwHelpContext;
137 } ErrorInfoImpl;
139 static const IErrorInfoVtbl IErrorInfoImpl_VTable;
140 static const ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable;
141 static const ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable;
144 converts an object pointer to This
147 static inline ErrorInfoImpl *impl_from_IErrorInfo( IErrorInfo *iface )
149 return (ErrorInfoImpl *)((char*)iface - FIELD_OFFSET(ErrorInfoImpl, lpvtei));
152 static inline ErrorInfoImpl *impl_from_ICreateErrorInfo( ICreateErrorInfo *iface )
154 return (ErrorInfoImpl *)((char*)iface - FIELD_OFFSET(ErrorInfoImpl, lpvtcei));
157 static inline ErrorInfoImpl *impl_from_ISupportErrorInfo( ISupportErrorInfo *iface )
159 return (ErrorInfoImpl *)((char*)iface - FIELD_OFFSET(ErrorInfoImpl, lpvtsei));
164 converts This to an object pointer
166 #define _IErrorInfo_(This) ((IErrorInfo*)&(This)->lpvtei)
167 #define _ICreateErrorInfo_(This) (&(This)->lpvtcei)
168 #define _ISupportErrorInfo_(This) (&(This)->lpvtsei)
170 static IErrorInfo * IErrorInfoImpl_Constructor(void)
172 ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
173 if (ei)
175 ei->lpvtei = &IErrorInfoImpl_VTable;
176 ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
177 ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
178 ei->ref = 1;
179 ei->bstrSource = NULL;
180 ei->bstrDescription = NULL;
181 ei->bstrHelpFile = NULL;
182 ei->m_dwHelpContext = 0;
184 return (IErrorInfo *)ei;
188 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
189 IErrorInfo* iface,
190 REFIID riid,
191 VOID** ppvoid)
193 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
194 TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvoid);
196 *ppvoid = NULL;
198 if(IsEqualIID(riid, &IID_IErrorInfo))
200 *ppvoid = _IErrorInfo_(This);
202 else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
204 *ppvoid = _ICreateErrorInfo_(This);
206 else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
208 *ppvoid = _ISupportErrorInfo_(This);
211 if(*ppvoid)
213 IUnknown_AddRef( (IUnknown*)*ppvoid );
214 TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
215 return S_OK;
217 TRACE("-- Interface: E_NOINTERFACE\n");
218 return E_NOINTERFACE;
221 static ULONG WINAPI IErrorInfoImpl_AddRef(
222 IErrorInfo* iface)
224 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
225 TRACE("(%p)->(count=%u)\n",This,This->ref);
226 return InterlockedIncrement(&This->ref);
229 static ULONG WINAPI IErrorInfoImpl_Release(
230 IErrorInfo* iface)
232 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
233 ULONG ref = InterlockedDecrement(&This->ref);
235 TRACE("(%p)->(count=%u)\n",This,ref+1);
237 if (!ref)
239 TRACE("-- destroying IErrorInfo(%p)\n",This);
241 ERRORINFO_SysFreeString(This->bstrSource);
242 ERRORINFO_SysFreeString(This->bstrDescription);
243 ERRORINFO_SysFreeString(This->bstrHelpFile);
244 HeapFree(GetProcessHeap(),0,This);
245 return 0;
247 return ref;
250 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
251 IErrorInfo* iface,
252 GUID * pGUID)
254 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
255 TRACE("(%p)->(count=%u)\n",This,This->ref);
256 if(!pGUID )return E_INVALIDARG;
257 *pGUID = This->m_Guid;
258 return S_OK;
261 static HRESULT WINAPI IErrorInfoImpl_GetSource(
262 IErrorInfo* iface,
263 BSTR *pBstrSource)
265 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
266 TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
267 if (pBstrSource == NULL)
268 return E_INVALIDARG;
269 *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
270 return S_OK;
273 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
274 IErrorInfo* iface,
275 BSTR *pBstrDescription)
277 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
279 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
280 if (pBstrDescription == NULL)
281 return E_INVALIDARG;
282 *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
284 return S_OK;
287 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
288 IErrorInfo* iface,
289 BSTR *pBstrHelpFile)
291 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
293 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
294 if (pBstrHelpFile == NULL)
295 return E_INVALIDARG;
296 *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
298 return S_OK;
301 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
302 IErrorInfo* iface,
303 DWORD *pdwHelpContext)
305 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
306 TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
307 if (pdwHelpContext == NULL)
308 return E_INVALIDARG;
309 *pdwHelpContext = This->m_dwHelpContext;
311 return S_OK;
314 static const IErrorInfoVtbl IErrorInfoImpl_VTable =
316 IErrorInfoImpl_QueryInterface,
317 IErrorInfoImpl_AddRef,
318 IErrorInfoImpl_Release,
320 IErrorInfoImpl_GetGUID,
321 IErrorInfoImpl_GetSource,
322 IErrorInfoImpl_GetDescription,
323 IErrorInfoImpl_GetHelpFile,
324 IErrorInfoImpl_GetHelpContext
328 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
329 ICreateErrorInfo* iface,
330 REFIID riid,
331 VOID** ppvoid)
333 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
334 TRACE("(%p)\n", This);
335 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
338 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
339 ICreateErrorInfo* iface)
341 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
342 TRACE("(%p)\n", This);
343 return IErrorInfo_AddRef(_IErrorInfo_(This));
346 static ULONG WINAPI ICreateErrorInfoImpl_Release(
347 ICreateErrorInfo* iface)
349 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
350 TRACE("(%p)\n", This);
351 return IErrorInfo_Release(_IErrorInfo_(This));
355 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
356 ICreateErrorInfo* iface,
357 REFGUID rguid)
359 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
360 TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
361 This->m_Guid = *rguid;
362 return S_OK;
365 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
366 ICreateErrorInfo* iface,
367 LPOLESTR szSource)
369 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
370 TRACE("(%p): %s\n",This, debugstr_w(szSource));
371 if (This->bstrSource != NULL)
372 ERRORINFO_SysFreeString(This->bstrSource);
373 This->bstrSource = ERRORINFO_SysAllocString(szSource);
375 return S_OK;
378 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
379 ICreateErrorInfo* iface,
380 LPOLESTR szDescription)
382 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
383 TRACE("(%p): %s\n",This, debugstr_w(szDescription));
384 if (This->bstrDescription != NULL)
385 ERRORINFO_SysFreeString(This->bstrDescription);
386 This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
388 return S_OK;
391 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
392 ICreateErrorInfo* iface,
393 LPOLESTR szHelpFile)
395 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
396 TRACE("(%p,%s)\n",This,debugstr_w(szHelpFile));
397 if (This->bstrHelpFile != NULL)
398 ERRORINFO_SysFreeString(This->bstrHelpFile);
399 This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
400 return S_OK;
403 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
404 ICreateErrorInfo* iface,
405 DWORD dwHelpContext)
407 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
408 TRACE("(%p,%d)\n",This,dwHelpContext);
409 This->m_dwHelpContext = dwHelpContext;
410 return S_OK;
413 static const ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable =
415 ICreateErrorInfoImpl_QueryInterface,
416 ICreateErrorInfoImpl_AddRef,
417 ICreateErrorInfoImpl_Release,
419 ICreateErrorInfoImpl_SetGUID,
420 ICreateErrorInfoImpl_SetSource,
421 ICreateErrorInfoImpl_SetDescription,
422 ICreateErrorInfoImpl_SetHelpFile,
423 ICreateErrorInfoImpl_SetHelpContext
426 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
427 ISupportErrorInfo* iface,
428 REFIID riid,
429 VOID** ppvoid)
431 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
432 TRACE("(%p)\n", This);
434 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
437 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
438 ISupportErrorInfo* iface)
440 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
441 TRACE("(%p)\n", This);
442 return IErrorInfo_AddRef(_IErrorInfo_(This));
445 static ULONG WINAPI ISupportErrorInfoImpl_Release(
446 ISupportErrorInfo* iface)
448 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
449 TRACE("(%p)\n", This);
450 return IErrorInfo_Release(_IErrorInfo_(This));
454 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
455 ISupportErrorInfo* iface,
456 REFIID riid)
458 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
459 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
460 return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
463 static const ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable =
465 ISupportErrorInfoImpl_QueryInterface,
466 ISupportErrorInfoImpl_AddRef,
467 ISupportErrorInfoImpl_Release,
470 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
473 /***********************************************************************
474 * CreateErrorInfo (OLE32.@)
476 * Creates an object used to set details for an error info object.
478 * PARAMS
479 * pperrinfo [O]. Address where error info creation object will be stored.
481 * RETURNS
482 * Success: S_OK.
483 * Failure: HRESULT code.
485 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
487 IErrorInfo * pei;
488 HRESULT res;
489 TRACE("(%p)\n", pperrinfo);
490 if(! pperrinfo ) return E_INVALIDARG;
491 if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
493 res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
494 IErrorInfo_Release(pei);
495 return res;
498 /***********************************************************************
499 * GetErrorInfo (OLE32.@)
501 * Retrieves the error information object for the current thread.
503 * PARAMS
504 * dwReserved [I]. Reserved. Must be zero.
505 * pperrinfo [O]. Address where error information object will be stored on return.
507 * RETURNS
508 * Success: S_OK if an error information object was set for the current thread.
509 * S_FALSE if otherwise.
510 * Failure: E_INVALIDARG if dwReserved is not zero.
512 * NOTES
513 * This function causes the current error info object for the thread to be
514 * cleared if one was set beforehand.
516 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
518 TRACE("(%d, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->errorinfo);
520 if (dwReserved)
522 ERR("dwReserved (0x%x) != 0\n", dwReserved);
523 return E_INVALIDARG;
526 if(!pperrinfo) return E_INVALIDARG;
528 if (!COM_CurrentInfo()->errorinfo)
530 *pperrinfo = NULL;
531 return S_FALSE;
534 *pperrinfo = COM_CurrentInfo()->errorinfo;
536 /* clear thread error state */
537 COM_CurrentInfo()->errorinfo = NULL;
538 return S_OK;
541 /***********************************************************************
542 * SetErrorInfo (OLE32.@)
544 * Sets the error information object for the current thread.
546 * PARAMS
547 * dwReserved [I] Reserved. Must be zero.
548 * perrinfo [I] Error info object.
550 * RETURNS
551 * Success: S_OK.
552 * Failure: E_INVALIDARG if dwReserved is not zero.
554 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
556 IErrorInfo * pei;
558 TRACE("(%d, %p)\n", dwReserved, perrinfo);
560 if (dwReserved)
562 ERR("dwReserved (0x%x) != 0\n", dwReserved);
563 return E_INVALIDARG;
566 /* release old errorinfo */
567 pei = COM_CurrentInfo()->errorinfo;
568 if (pei) IErrorInfo_Release(pei);
570 /* set to new value */
571 COM_CurrentInfo()->errorinfo = perrinfo;
572 if (perrinfo) IErrorInfo_AddRef(perrinfo);
574 return S_OK;