user32: Treat the top-level message window as a desktop window.
[wine/multimedia.git] / dlls / ole32 / errorinfo.c
blobe4a1c47ac535d7343695c4ba6c25070540c1403b
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 WINAPI 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 (LPWSTR)stringBuffer;
101 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
102 static VOID WINAPI 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) (ICreateErrorInfo*)&(This->lpvtcei)
168 #define _ISupportErrorInfo_(This) (ISupportErrorInfo*)&(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)->(\n\tIID:\t%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);
240 HeapFree(GetProcessHeap(),0,This);
241 return 0;
243 return ref;
246 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
247 IErrorInfo* iface,
248 GUID * pGUID)
250 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
251 TRACE("(%p)->(count=%u)\n",This,This->ref);
252 if(!pGUID )return E_INVALIDARG;
253 *pGUID = This->m_Guid;
254 return S_OK;
257 static HRESULT WINAPI IErrorInfoImpl_GetSource(
258 IErrorInfo* iface,
259 BSTR *pBstrSource)
261 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
262 TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
263 if (pBstrSource == NULL)
264 return E_INVALIDARG;
265 *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
266 return S_OK;
269 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
270 IErrorInfo* iface,
271 BSTR *pBstrDescription)
273 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
275 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
276 if (pBstrDescription == NULL)
277 return E_INVALIDARG;
278 *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
280 return S_OK;
283 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
284 IErrorInfo* iface,
285 BSTR *pBstrHelpFile)
287 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
289 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
290 if (pBstrHelpFile == NULL)
291 return E_INVALIDARG;
292 *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
294 return S_OK;
297 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
298 IErrorInfo* iface,
299 DWORD *pdwHelpContext)
301 ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
302 TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
303 if (pdwHelpContext == NULL)
304 return E_INVALIDARG;
305 *pdwHelpContext = This->m_dwHelpContext;
307 return S_OK;
310 static const IErrorInfoVtbl IErrorInfoImpl_VTable =
312 IErrorInfoImpl_QueryInterface,
313 IErrorInfoImpl_AddRef,
314 IErrorInfoImpl_Release,
316 IErrorInfoImpl_GetGUID,
317 IErrorInfoImpl_GetSource,
318 IErrorInfoImpl_GetDescription,
319 IErrorInfoImpl_GetHelpFile,
320 IErrorInfoImpl_GetHelpContext
324 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
325 ICreateErrorInfo* iface,
326 REFIID riid,
327 VOID** ppvoid)
329 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
330 TRACE("(%p)\n", This);
331 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
334 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
335 ICreateErrorInfo* iface)
337 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
338 TRACE("(%p)\n", This);
339 return IErrorInfo_AddRef(_IErrorInfo_(This));
342 static ULONG WINAPI ICreateErrorInfoImpl_Release(
343 ICreateErrorInfo* iface)
345 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
346 TRACE("(%p)\n", This);
347 return IErrorInfo_Release(_IErrorInfo_(This));
351 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
352 ICreateErrorInfo* iface,
353 REFGUID rguid)
355 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
356 TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
357 This->m_Guid = *rguid;
358 return S_OK;
361 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
362 ICreateErrorInfo* iface,
363 LPOLESTR szSource)
365 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
366 TRACE("(%p): %s\n",This, debugstr_w(szSource));
367 if (This->bstrSource != NULL)
368 ERRORINFO_SysFreeString(This->bstrSource);
369 This->bstrSource = ERRORINFO_SysAllocString(szSource);
371 return S_OK;
374 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
375 ICreateErrorInfo* iface,
376 LPOLESTR szDescription)
378 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
379 TRACE("(%p): %s\n",This, debugstr_w(szDescription));
380 if (This->bstrDescription != NULL)
381 ERRORINFO_SysFreeString(This->bstrDescription);
382 This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
384 return S_OK;
387 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
388 ICreateErrorInfo* iface,
389 LPOLESTR szHelpFile)
391 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
392 TRACE("(%p,%s)\n",This,debugstr_w(szHelpFile));
393 if (This->bstrHelpFile != NULL)
394 ERRORINFO_SysFreeString(This->bstrHelpFile);
395 This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
396 return S_OK;
399 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
400 ICreateErrorInfo* iface,
401 DWORD dwHelpContext)
403 ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
404 TRACE("(%p,%d)\n",This,dwHelpContext);
405 This->m_dwHelpContext = dwHelpContext;
406 return S_OK;
409 static const ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable =
411 ICreateErrorInfoImpl_QueryInterface,
412 ICreateErrorInfoImpl_AddRef,
413 ICreateErrorInfoImpl_Release,
415 ICreateErrorInfoImpl_SetGUID,
416 ICreateErrorInfoImpl_SetSource,
417 ICreateErrorInfoImpl_SetDescription,
418 ICreateErrorInfoImpl_SetHelpFile,
419 ICreateErrorInfoImpl_SetHelpContext
422 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
423 ISupportErrorInfo* iface,
424 REFIID riid,
425 VOID** ppvoid)
427 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
428 TRACE("(%p)\n", This);
430 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
433 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
434 ISupportErrorInfo* iface)
436 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
437 TRACE("(%p)\n", This);
438 return IErrorInfo_AddRef(_IErrorInfo_(This));
441 static ULONG WINAPI ISupportErrorInfoImpl_Release(
442 ISupportErrorInfo* iface)
444 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
445 TRACE("(%p)\n", This);
446 return IErrorInfo_Release(_IErrorInfo_(This));
450 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
451 ISupportErrorInfo* iface,
452 REFIID riid)
454 ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
455 TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
456 return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
459 static const ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable =
461 ISupportErrorInfoImpl_QueryInterface,
462 ISupportErrorInfoImpl_AddRef,
463 ISupportErrorInfoImpl_Release,
466 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
469 /***********************************************************************
470 * CreateErrorInfo (OLE32.@)
472 * Creates an object used to set details for an error info object.
474 * PARAMS
475 * pperrinfo [O]. Address where error info creation object will be stored.
477 * RETURNS
478 * Success: S_OK.
479 * Failure: HRESULT code.
481 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
483 IErrorInfo * pei;
484 HRESULT res;
485 TRACE("(%p)\n", pperrinfo);
486 if(! pperrinfo ) return E_INVALIDARG;
487 if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
489 res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
490 IErrorInfo_Release(pei);
491 return res;
494 /***********************************************************************
495 * GetErrorInfo (OLE32.@)
497 * Retrieves the error information object for the current thread.
499 * PARAMS
500 * dwReserved [I]. Reserved. Must be zero.
501 * pperrinfo [O]. Address where error information object will be stored on return.
503 * RETURNS
504 * Success: S_OK if an error information object was set for the current thread.
505 * S_FALSE if otherwise.
506 * Failure: E_INVALIDARG if dwReserved is not zero.
508 * NOTES
509 * This function causes the current error info object for the thread to be
510 * cleared if one was set beforehand.
512 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
514 TRACE("(%d, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->errorinfo);
516 if (dwReserved)
518 ERR("dwReserved (0x%x) != 0\n", dwReserved);
519 return E_INVALIDARG;
522 if(!pperrinfo) return E_INVALIDARG;
524 if (!COM_CurrentInfo()->errorinfo)
526 *pperrinfo = NULL;
527 return S_FALSE;
530 *pperrinfo = COM_CurrentInfo()->errorinfo;
532 /* clear thread error state */
533 COM_CurrentInfo()->errorinfo = NULL;
534 return S_OK;
537 /***********************************************************************
538 * SetErrorInfo (OLE32.@)
540 * Sets the error information object for the current thread.
542 * PARAMS
543 * dwReserved [I] Reserved. Must be zero.
544 * perrinfo [I] Error info object.
546 * RETURNS
547 * Success: S_OK.
548 * Failure: E_INVALIDARG if dwReserved is not zero.
550 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
552 IErrorInfo * pei;
554 TRACE("(%d, %p)\n", dwReserved, perrinfo);
556 if (dwReserved)
558 ERR("dwReserved (0x%x) != 0\n", dwReserved);
559 return E_INVALIDARG;
562 /* release old errorinfo */
563 pei = COM_CurrentInfo()->errorinfo;
564 if (pei) IErrorInfo_Release(pei);
566 /* set to new value */
567 COM_CurrentInfo()->errorinfo = perrinfo;
568 if (perrinfo) IErrorInfo_AddRef(perrinfo);
570 return S_OK;