Various fixes, typos corrected and clarifying trace points.
[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 <string.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "oleauto.h"
31 #include "winerror.h"
32
33 #include "wine/obj_base.h"
34 #include "wine/obj_oleaut.h"
35 #include "wine/obj_errorinfo.h"
36 #include "wine/unicode.h"
37 #include "thread.h"
38
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42
43 /* this code is from SysAllocStringLen (ole2disp.c in oleaut32) */
44 static BSTR WINAPI ERRORINFO_SysAllocString(const OLECHAR* in)
45 {
46     DWORD  bufferSize;
47     DWORD* newBuffer;
48     WCHAR* stringBuffer;
49     DWORD len;
50
51     if (in == NULL)
52         return NULL;
53     /*
54      * Find the lenth of the buffer passed-in in bytes.
55      */
56     len = strlenW(in);
57     bufferSize = len * sizeof (WCHAR);
58
59     /*
60      * Allocate a new buffer to hold the string.
61      * dont't forget to keep an empty spot at the beginning of the
62      * buffer for the character count and an extra character at the
63      * end for the '\0'.
64      */
65     newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
66                                  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         ICOM_VTABLE(IErrorInfo)         *lpvtei;
132         ICOM_VTABLE(ICreateErrorInfo)   *lpvtcei;
133         ICOM_VTABLE(ISupportErrorInfo)  *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 ICOM_VTABLE(IErrorInfo)          IErrorInfoImpl_VTable;
144 static ICOM_VTABLE(ICreateErrorInfo)    ICreateErrorInfoImpl_VTable;
145 static ICOM_VTABLE(ISupportErrorInfo)   ISupportErrorInfoImpl_VTable;
146
147 /*
148  converts a objectpointer 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 a objectpointer
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         TRACE("(%p)->(count=%lu)\n",This,This->ref);
230
231         if (!InterlockedDecrement(&This->ref))
232         {
233           TRACE("-- destroying IErrorInfo(%p)\n",This);
234           HeapFree(GetProcessHeap(),0,This);
235           return 0;
236         }
237         return This->ref;
238 }
239
240 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
241         IErrorInfo* iface,
242         GUID * pGUID)
243 {
244         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
245         TRACE("(%p)->(count=%lu)\n",This,This->ref);
246         if(!pGUID )return E_INVALIDARG;
247         memcpy(pGUID, &This->m_Guid, sizeof(GUID));
248         return S_OK;
249 }
250
251 static HRESULT WINAPI IErrorInfoImpl_GetSource(
252         IErrorInfo* iface,
253         BSTR *pBstrSource)
254 {
255         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
256         TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
257         if (pBstrSource == NULL)
258             return E_INVALIDARG;
259         *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
260         return S_OK;
261 }
262
263 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
264         IErrorInfo* iface,
265         BSTR *pBstrDescription)
266 {
267         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
268
269         TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
270         if (pBstrDescription == NULL)
271             return E_INVALIDARG;
272         *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
273
274         return S_OK;
275 }
276
277 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
278         IErrorInfo* iface,
279         BSTR *pBstrHelpFile)
280 {
281         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
282
283         TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
284         if (pBstrHelpFile == NULL)
285             return E_INVALIDARG;
286         *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
287
288         return S_OK;
289 }
290
291 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
292         IErrorInfo* iface,
293         DWORD *pdwHelpContext)
294 {
295         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
296         TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
297         if (pdwHelpContext == NULL)
298             return E_INVALIDARG;
299         *pdwHelpContext = This->m_dwHelpContext;
300
301         return S_OK;
302 }
303
304 static ICOM_VTABLE(IErrorInfo) IErrorInfoImpl_VTable =
305 {
306   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
307   IErrorInfoImpl_QueryInterface,
308   IErrorInfoImpl_AddRef,
309   IErrorInfoImpl_Release,
310
311   IErrorInfoImpl_GetGUID,
312   IErrorInfoImpl_GetSource,
313   IErrorInfoImpl_GetDescription,
314   IErrorInfoImpl_GetHelpFile,
315   IErrorInfoImpl_GetHelpContext
316 };
317
318
319 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
320         ICreateErrorInfo* iface,
321         REFIID     riid,
322         VOID**     ppvoid)
323 {
324         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
325         TRACE("(%p)\n", This);
326         return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
327 }
328
329 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
330         ICreateErrorInfo* iface)
331 {
332         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
333         TRACE("(%p)\n", This);
334         return IErrorInfo_AddRef(_IErrorInfo_(This));
335 }
336
337 static ULONG WINAPI ICreateErrorInfoImpl_Release(
338         ICreateErrorInfo* iface)
339 {
340         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
341         TRACE("(%p)\n", This);
342         return IErrorInfo_Release(_IErrorInfo_(This));
343 }
344
345
346 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
347         ICreateErrorInfo* iface,
348         REFGUID rguid)
349 {
350         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
351         TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
352         memcpy(&This->m_Guid,  rguid, sizeof(GUID));
353         return S_OK;
354 }
355
356 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
357         ICreateErrorInfo* iface,
358         LPOLESTR szSource)
359 {
360         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
361         TRACE("(%p)\n",This);
362         if (This->bstrSource != NULL)
363             ERRORINFO_SysFreeString(This->bstrSource);
364         This->bstrSource = ERRORINFO_SysAllocString(szSource);
365
366         return S_OK;
367 }
368
369 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
370         ICreateErrorInfo* iface,
371         LPOLESTR szDescription)
372 {
373         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
374         TRACE("(%p)\n",This);
375         if (This->bstrDescription != NULL)
376             ERRORINFO_SysFreeString(This->bstrDescription);
377         This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
378
379         return S_OK;
380 }
381
382 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
383         ICreateErrorInfo* iface,
384         LPOLESTR szHelpFile)
385 {
386         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
387         TRACE("(%p)\n",This);
388         if (This->bstrHelpFile != NULL)
389             ERRORINFO_SysFreeString(This->bstrHelpFile);
390         This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
391
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)\n",This);
401         This->m_dwHelpContext = dwHelpContext;
402
403         return S_OK;
404 }
405
406 static ICOM_VTABLE(ICreateErrorInfo) ICreateErrorInfoImpl_VTable =
407 {
408   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
409   ICreateErrorInfoImpl_QueryInterface,
410   ICreateErrorInfoImpl_AddRef,
411   ICreateErrorInfoImpl_Release,
412
413   ICreateErrorInfoImpl_SetGUID,
414   ICreateErrorInfoImpl_SetSource,
415   ICreateErrorInfoImpl_SetDescription,
416   ICreateErrorInfoImpl_SetHelpFile,
417   ICreateErrorInfoImpl_SetHelpContext
418 };
419
420 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
421         ISupportErrorInfo* iface,
422         REFIID     riid,
423         VOID**     ppvoid)
424 {
425         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
426         TRACE("(%p)\n", This);
427
428         return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
429 }
430
431 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
432         ISupportErrorInfo* iface)
433 {
434         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
435         TRACE("(%p)\n", This);
436         return IErrorInfo_AddRef(_IErrorInfo_(This));
437 }
438
439 static ULONG WINAPI ISupportErrorInfoImpl_Release(
440         ISupportErrorInfo* iface)
441 {
442         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
443         TRACE("(%p)\n", This);
444         return IErrorInfo_Release(_IErrorInfo_(This));
445 }
446
447
448 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
449         ISupportErrorInfo* iface,
450         REFIID riid)
451 {
452         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
453         TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
454         return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
455 }
456
457 static ICOM_VTABLE(ISupportErrorInfo) ISupportErrorInfoImpl_VTable =
458 {
459   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
460   ISupportErrorInfoImpl_QueryInterface,
461   ISupportErrorInfoImpl_AddRef,
462   ISupportErrorInfoImpl_Release,
463
464
465   ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
466 };
467 /***********************************************************************
468  *              CreateErrorInfo (OLE32.192)
469  */
470 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
471 {
472         IErrorInfo * pei;
473         HRESULT res;
474         TRACE("(%p): stub:\n", pperrinfo);
475         if(! pperrinfo ) return E_INVALIDARG;
476         if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
477
478         res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
479         IErrorInfo_Release(pei);
480         return res;
481 }
482
483 /***********************************************************************
484  *              GetErrorInfo (OLE32.196)
485  */
486 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
487 {
488         TRACE("(%ld, %p, %p): stub:\n", dwReserved, pperrinfo, NtCurrentTeb()->ErrorInfo);
489
490         if(! pperrinfo ) return E_INVALIDARG;
491         if(!(*pperrinfo = (IErrorInfo*)(NtCurrentTeb()->ErrorInfo))) return S_FALSE;
492
493         /* clear thread error state */
494         NtCurrentTeb()->ErrorInfo = NULL;
495         return S_OK;
496 }
497
498 /***********************************************************************
499  *              SetErrorInfo (OLE32.255)
500  */
501 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
502 {
503         IErrorInfo * pei;
504         TRACE("(%ld, %p): stub:\n", dwReserved, perrinfo);
505
506         /* release old errorinfo */
507         pei = (IErrorInfo*)NtCurrentTeb()->ErrorInfo;
508         if(pei) IErrorInfo_Release(pei);
509
510         /* set to new value */
511         NtCurrentTeb()->ErrorInfo = perrinfo;
512         if(perrinfo) IErrorInfo_AddRef(perrinfo);
513         return S_OK;
514 }