Added SystemHandleInformation tests.
[wine] / dlls / ole32 / errorinfo.c
1 /*
2  * ErrorInfo API
3  *
4  * Copyright 2000 Patrik Stridvall, Juergen Schmied
5  *
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.
10  *
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.
15  *
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
19  *
20  * NOTES:
21  *
22  * The errorinfo is a per-thread object. The reference is stored in the
23  * TEB at offset 0xf80.
24  */
25
26 #include <stdarg.h>
27 #include <string.h>
28
29 #define COBJMACROS
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "objbase.h"
34 #include "oleauto.h"
35 #include "winerror.h"
36
37 #include "wine/unicode.h"
38 #include "compobj_private.h"
39
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43
44 /* this code is from SysAllocStringLen (ole2disp.c in oleaut32) */
45 static BSTR WINAPI ERRORINFO_SysAllocString(const OLECHAR* in)
46 {
47     DWORD  bufferSize;
48     DWORD* newBuffer;
49     WCHAR* stringBuffer;
50     DWORD len;
51
52     if (in == NULL)
53         return NULL;
54     /*
55      * Find the lenth of the buffer passed-in in bytes.
56      */
57     len = strlenW(in);
58     bufferSize = len * sizeof (WCHAR);
59
60     /*
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'.
65      */
66     newBuffer = HeapAlloc(GetProcessHeap(), 0,
67                                  bufferSize + sizeof(WCHAR) + sizeof(DWORD));
68
69     /*
70      * If the memory allocation failed, return a null pointer.
71      */
72     if (newBuffer==0)
73       return 0;
74
75     /*
76      * Copy the length of the string in the placeholder.
77      */
78     *newBuffer = bufferSize;
79
80     /*
81      * Skip the byte count.
82      */
83     newBuffer++;
84
85     /*
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.
89      */
90     if (in != 0)
91       memcpy(newBuffer, in, bufferSize);
92     else
93       memset(newBuffer, 0, bufferSize);
94
95     /*
96      * Make sure that there is a nul character at the end of the
97      * string.
98      */
99     stringBuffer = (WCHAR*)newBuffer;
100     stringBuffer[len] = 0;
101
102     return (LPWSTR)stringBuffer;
103 }
104
105 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
106 static VOID WINAPI ERRORINFO_SysFreeString(BSTR in)
107 {
108     DWORD* bufferPointer;
109
110     /* NULL is a valid parameter */
111     if(!in) return;
112
113     /*
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
116      * before the string.
117      */
118     bufferPointer = (DWORD*)in;
119
120     bufferPointer--;
121
122     /*
123      * Free the memory from it's "real" origin.
124      */
125     HeapFree(GetProcessHeap(), 0, bufferPointer);
126 }
127
128
129 typedef struct ErrorInfoImpl
130 {
131         IErrorInfoVtbl          *lpvtei;
132         ICreateErrorInfoVtbl    *lpvtcei;
133         ISupportErrorInfoVtbl   *lpvtsei;
134         DWORD                           ref;
135
136         GUID m_Guid;
137         BSTR bstrSource;
138         BSTR bstrDescription;
139         BSTR bstrHelpFile;
140         DWORD m_dwHelpContext;
141 } ErrorInfoImpl;
142
143 static IErrorInfoVtbl           IErrorInfoImpl_VTable;
144 static ICreateErrorInfoVtbl     ICreateErrorInfoImpl_VTable;
145 static ISupportErrorInfoVtbl    ISupportErrorInfoImpl_VTable;
146
147 /*
148  converts an object pointer to This
149  */
150 #define _IErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtei)))
151 #define _ICOM_THIS_From_IErrorInfo(class, name) class* This = (class*)(((char*)name)-_IErrorInfo_Offset)
152
153 #define _ICreateErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtcei)))
154 #define _ICOM_THIS_From_ICreateErrorInfo(class, name) class* This = (class*)(((char*)name)-_ICreateErrorInfo_Offset)
155
156 #define _ISupportErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtsei)))
157 #define _ICOM_THIS_From_ISupportErrorInfo(class, name) class* This = (class*)(((char*)name)-_ISupportErrorInfo_Offset)
158
159 /*
160  converts This to an object pointer
161  */
162 #define _IErrorInfo_(This)              (IErrorInfo*)&(This->lpvtei)
163 #define _ICreateErrorInfo_(This)        (ICreateErrorInfo*)&(This->lpvtcei)
164 #define _ISupportErrorInfo_(This)       (ISupportErrorInfo*)&(This->lpvtsei)
165
166 IErrorInfo * IErrorInfoImpl_Constructor()
167 {
168         ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
169         if (ei)
170         {
171           ei->lpvtei = &IErrorInfoImpl_VTable;
172           ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
173           ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
174           ei->ref = 1;
175           ei->bstrSource = NULL;
176           ei->bstrDescription = NULL;
177           ei->bstrHelpFile = NULL;
178           ei->m_dwHelpContext = 0;
179         }
180         return (IErrorInfo *)ei;
181 }
182
183
184 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
185         IErrorInfo* iface,
186         REFIID     riid,
187         VOID**     ppvoid)
188 {
189         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
190         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);
191
192         *ppvoid = NULL;
193
194         if(IsEqualIID(riid, &IID_IErrorInfo))
195         {
196           *ppvoid = _IErrorInfo_(This);
197         }
198         else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
199         {
200           *ppvoid = _ICreateErrorInfo_(This);
201         }
202         else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
203         {
204           *ppvoid = _ISupportErrorInfo_(This);
205         }
206
207         if(*ppvoid)
208         {
209           IUnknown_AddRef( (IUnknown*)*ppvoid );
210           TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
211           return S_OK;
212         }
213         TRACE("-- Interface: E_NOINTERFACE\n");
214         return E_NOINTERFACE;
215 }
216
217 static ULONG WINAPI IErrorInfoImpl_AddRef(
218         IErrorInfo* iface)
219 {
220         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
221         TRACE("(%p)->(count=%lu)\n",This,This->ref);
222         return InterlockedIncrement(&This->ref);
223 }
224
225 static ULONG WINAPI IErrorInfoImpl_Release(
226         IErrorInfo* iface)
227 {
228         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
229         ULONG ref = InterlockedDecrement(&This->ref);
230
231         TRACE("(%p)->(count=%lu)\n",This,ref+1);
232
233         if (!ref)
234         {
235           TRACE("-- destroying IErrorInfo(%p)\n",This);
236           HeapFree(GetProcessHeap(),0,This);
237           return 0;
238         }
239         return ref;
240 }
241
242 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
243         IErrorInfo* iface,
244         GUID * pGUID)
245 {
246         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
247         TRACE("(%p)->(count=%lu)\n",This,This->ref);
248         if(!pGUID )return E_INVALIDARG;
249         memcpy(pGUID, &This->m_Guid, sizeof(GUID));
250         return S_OK;
251 }
252
253 static HRESULT WINAPI IErrorInfoImpl_GetSource(
254         IErrorInfo* iface,
255         BSTR *pBstrSource)
256 {
257         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
258         TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
259         if (pBstrSource == NULL)
260             return E_INVALIDARG;
261         *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
262         return S_OK;
263 }
264
265 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
266         IErrorInfo* iface,
267         BSTR *pBstrDescription)
268 {
269         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
270
271         TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
272         if (pBstrDescription == NULL)
273             return E_INVALIDARG;
274         *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
275
276         return S_OK;
277 }
278
279 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
280         IErrorInfo* iface,
281         BSTR *pBstrHelpFile)
282 {
283         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
284
285         TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
286         if (pBstrHelpFile == NULL)
287             return E_INVALIDARG;
288         *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
289
290         return S_OK;
291 }
292
293 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
294         IErrorInfo* iface,
295         DWORD *pdwHelpContext)
296 {
297         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
298         TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
299         if (pdwHelpContext == NULL)
300             return E_INVALIDARG;
301         *pdwHelpContext = This->m_dwHelpContext;
302
303         return S_OK;
304 }
305
306 static IErrorInfoVtbl IErrorInfoImpl_VTable =
307 {
308   IErrorInfoImpl_QueryInterface,
309   IErrorInfoImpl_AddRef,
310   IErrorInfoImpl_Release,
311
312   IErrorInfoImpl_GetGUID,
313   IErrorInfoImpl_GetSource,
314   IErrorInfoImpl_GetDescription,
315   IErrorInfoImpl_GetHelpFile,
316   IErrorInfoImpl_GetHelpContext
317 };
318
319
320 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
321         ICreateErrorInfo* iface,
322         REFIID     riid,
323         VOID**     ppvoid)
324 {
325         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
326         TRACE("(%p)\n", This);
327         return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
328 }
329
330 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
331         ICreateErrorInfo* iface)
332 {
333         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
334         TRACE("(%p)\n", This);
335         return IErrorInfo_AddRef(_IErrorInfo_(This));
336 }
337
338 static ULONG WINAPI ICreateErrorInfoImpl_Release(
339         ICreateErrorInfo* iface)
340 {
341         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
342         TRACE("(%p)\n", This);
343         return IErrorInfo_Release(_IErrorInfo_(This));
344 }
345
346
347 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
348         ICreateErrorInfo* iface,
349         REFGUID rguid)
350 {
351         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
352         TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
353         memcpy(&This->m_Guid,  rguid, sizeof(GUID));
354         return S_OK;
355 }
356
357 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
358         ICreateErrorInfo* iface,
359         LPOLESTR szSource)
360 {
361         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
362         TRACE("(%p): %s\n",This, debugstr_w(szSource));
363         if (This->bstrSource != NULL)
364             ERRORINFO_SysFreeString(This->bstrSource);
365         This->bstrSource = ERRORINFO_SysAllocString(szSource);
366
367         return S_OK;
368 }
369
370 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
371         ICreateErrorInfo* iface,
372         LPOLESTR szDescription)
373 {
374         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
375         TRACE("(%p): %s\n",This, debugstr_w(szDescription));
376         if (This->bstrDescription != NULL)
377             ERRORINFO_SysFreeString(This->bstrDescription);
378         This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
379
380         return S_OK;
381 }
382
383 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
384         ICreateErrorInfo* iface,
385         LPOLESTR szHelpFile)
386 {
387         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
388         TRACE("(%p,%s)\n",This,debugstr_w(szHelpFile));
389         if (This->bstrHelpFile != NULL)
390             ERRORINFO_SysFreeString(This->bstrHelpFile);
391         This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
392         return S_OK;
393 }
394
395 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
396         ICreateErrorInfo* iface,
397         DWORD dwHelpContext)
398 {
399         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
400         TRACE("(%p,%ld)\n",This,dwHelpContext);
401         This->m_dwHelpContext = dwHelpContext;
402         return S_OK;
403 }
404
405 static ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable =
406 {
407   ICreateErrorInfoImpl_QueryInterface,
408   ICreateErrorInfoImpl_AddRef,
409   ICreateErrorInfoImpl_Release,
410
411   ICreateErrorInfoImpl_SetGUID,
412   ICreateErrorInfoImpl_SetSource,
413   ICreateErrorInfoImpl_SetDescription,
414   ICreateErrorInfoImpl_SetHelpFile,
415   ICreateErrorInfoImpl_SetHelpContext
416 };
417
418 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
419         ISupportErrorInfo* iface,
420         REFIID     riid,
421         VOID**     ppvoid)
422 {
423         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
424         TRACE("(%p)\n", This);
425
426         return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
427 }
428
429 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
430         ISupportErrorInfo* iface)
431 {
432         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
433         TRACE("(%p)\n", This);
434         return IErrorInfo_AddRef(_IErrorInfo_(This));
435 }
436
437 static ULONG WINAPI ISupportErrorInfoImpl_Release(
438         ISupportErrorInfo* iface)
439 {
440         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
441         TRACE("(%p)\n", This);
442         return IErrorInfo_Release(_IErrorInfo_(This));
443 }
444
445
446 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
447         ISupportErrorInfo* iface,
448         REFIID riid)
449 {
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;
453 }
454
455 static ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable =
456 {
457   ISupportErrorInfoImpl_QueryInterface,
458   ISupportErrorInfoImpl_AddRef,
459   ISupportErrorInfoImpl_Release,
460
461
462   ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
463 };
464 /***********************************************************************
465  *              CreateErrorInfo (OLE32.@)
466  */
467 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
468 {
469         IErrorInfo * pei;
470         HRESULT res;
471         TRACE("(%p): stub:\n", pperrinfo);
472         if(! pperrinfo ) return E_INVALIDARG;
473         if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
474
475         res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
476         IErrorInfo_Release(pei);
477         return res;
478 }
479
480 /***********************************************************************
481  *              GetErrorInfo (OLE32.@)
482  */
483 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
484 {
485         TRACE("(%ld, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->errorinfo);
486
487         if(!pperrinfo) return E_INVALIDARG;
488         
489         if (!COM_CurrentInfo()->errorinfo)
490         {
491            *pperrinfo = NULL;
492            return S_FALSE;
493         }
494
495         *pperrinfo = COM_CurrentInfo()->errorinfo;
496         
497         /* clear thread error state */
498         COM_CurrentInfo()->errorinfo = NULL;
499         return S_OK;
500 }
501
502 /***********************************************************************
503  *              SetErrorInfo (OLE32.@)
504  */
505 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
506 {
507         IErrorInfo * pei;
508
509         TRACE("(%ld, %p)\n", dwReserved, perrinfo);
510         
511         /* release old errorinfo */
512         pei = COM_CurrentInfo()->errorinfo;
513         if (pei) IErrorInfo_Release(pei);
514
515         /* set to new value */
516         COM_CurrentInfo()->errorinfo = perrinfo;
517         if (perrinfo) IErrorInfo_AddRef(perrinfo);
518         
519         return S_OK;
520 }