Get rid of the non-standard ICOM_VFIELD macro.
[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 "oleauto.h"
34 #include "winerror.h"
35
36 #include "objbase.h"
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 = (DWORD*)HeapAlloc(GetProcessHeap(),
67                                  0,
68                                  bufferSize + sizeof(WCHAR) + sizeof(DWORD));
69
70     /*
71      * If the memory allocation failed, return a null pointer.
72      */
73     if (newBuffer==0)
74       return 0;
75
76     /*
77      * Copy the length of the string in the placeholder.
78      */
79     *newBuffer = bufferSize;
80
81     /*
82      * Skip the byte count.
83      */
84     newBuffer++;
85
86     /*
87      * Copy the information in the buffer.
88      * Since it is valid to pass a NULL pointer here, we'll initialize the
89      * buffer to nul if it is the case.
90      */
91     if (in != 0)
92       memcpy(newBuffer, in, bufferSize);
93     else
94       memset(newBuffer, 0, bufferSize);
95
96     /*
97      * Make sure that there is a nul character at the end of the
98      * string.
99      */
100     stringBuffer = (WCHAR*)newBuffer;
101     stringBuffer[len] = 0;
102
103     return (LPWSTR)stringBuffer;
104 }
105
106 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
107 static VOID WINAPI ERRORINFO_SysFreeString(BSTR in)
108 {
109     DWORD* bufferPointer;
110
111     /* NULL is a valid parameter */
112     if(!in) return;
113
114     /*
115      * We have to be careful when we free a BSTR pointer, it points to
116      * the beginning of the string but it skips the byte count contained
117      * before the string.
118      */
119     bufferPointer = (DWORD*)in;
120
121     bufferPointer--;
122
123     /*
124      * Free the memory from it's "real" origin.
125      */
126     HeapFree(GetProcessHeap(), 0, bufferPointer);
127 }
128
129
130 typedef struct ErrorInfoImpl
131 {
132         ICOM_VTABLE(IErrorInfo)         *lpvtei;
133         ICOM_VTABLE(ICreateErrorInfo)   *lpvtcei;
134         ICOM_VTABLE(ISupportErrorInfo)  *lpvtsei;
135         DWORD                           ref;
136
137         GUID m_Guid;
138         BSTR bstrSource;
139         BSTR bstrDescription;
140         BSTR bstrHelpFile;
141         DWORD m_dwHelpContext;
142 } ErrorInfoImpl;
143
144 static ICOM_VTABLE(IErrorInfo)          IErrorInfoImpl_VTable;
145 static ICOM_VTABLE(ICreateErrorInfo)    ICreateErrorInfoImpl_VTable;
146 static ICOM_VTABLE(ISupportErrorInfo)   ISupportErrorInfoImpl_VTable;
147
148 /*
149  converts a objectpointer to This
150  */
151 #define _IErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtei)))
152 #define _ICOM_THIS_From_IErrorInfo(class, name) class* This = (class*)(((char*)name)-_IErrorInfo_Offset);
153
154 #define _ICreateErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtcei)))
155 #define _ICOM_THIS_From_ICreateErrorInfo(class, name) class* This = (class*)(((char*)name)-_ICreateErrorInfo_Offset);
156
157 #define _ISupportErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtsei)))
158 #define _ICOM_THIS_From_ISupportErrorInfo(class, name) class* This = (class*)(((char*)name)-_ISupportErrorInfo_Offset);
159
160 /*
161  converts This to a objectpointer
162  */
163 #define _IErrorInfo_(This)              (IErrorInfo*)&(This->lpvtei)
164 #define _ICreateErrorInfo_(This)        (ICreateErrorInfo*)&(This->lpvtcei)
165 #define _ISupportErrorInfo_(This)       (ISupportErrorInfo*)&(This->lpvtsei)
166
167 IErrorInfo * IErrorInfoImpl_Constructor()
168 {
169         ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
170         if (ei)
171         {
172           ei->lpvtei = &IErrorInfoImpl_VTable;
173           ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
174           ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
175           ei->ref = 1;
176           ei->bstrSource = NULL;
177           ei->bstrDescription = NULL;
178           ei->bstrHelpFile = NULL;
179           ei->m_dwHelpContext = 0;
180         }
181         return (IErrorInfo *)ei;
182 }
183
184
185 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
186         IErrorInfo* iface,
187         REFIID     riid,
188         VOID**     ppvoid)
189 {
190         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
191         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);
192
193         *ppvoid = NULL;
194
195         if(IsEqualIID(riid, &IID_IErrorInfo))
196         {
197           *ppvoid = _IErrorInfo_(This);
198         }
199         else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
200         {
201           *ppvoid = _ICreateErrorInfo_(This);
202         }
203         else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
204         {
205           *ppvoid = _ISupportErrorInfo_(This);
206         }
207
208         if(*ppvoid)
209         {
210           IUnknown_AddRef( (IUnknown*)*ppvoid );
211           TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
212           return S_OK;
213         }
214         TRACE("-- Interface: E_NOINTERFACE\n");
215         return E_NOINTERFACE;
216 }
217
218 static ULONG WINAPI IErrorInfoImpl_AddRef(
219         IErrorInfo* iface)
220 {
221         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
222         TRACE("(%p)->(count=%lu)\n",This,This->ref);
223         return InterlockedIncrement(&This->ref);
224 }
225
226 static ULONG WINAPI IErrorInfoImpl_Release(
227         IErrorInfo* iface)
228 {
229         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
230         TRACE("(%p)->(count=%lu)\n",This,This->ref);
231
232         if (!InterlockedDecrement(&This->ref))
233         {
234           TRACE("-- destroying IErrorInfo(%p)\n",This);
235           HeapFree(GetProcessHeap(),0,This);
236           return 0;
237         }
238         return This->ref;
239 }
240
241 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
242         IErrorInfo* iface,
243         GUID * pGUID)
244 {
245         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
246         TRACE("(%p)->(count=%lu)\n",This,This->ref);
247         if(!pGUID )return E_INVALIDARG;
248         memcpy(pGUID, &This->m_Guid, sizeof(GUID));
249         return S_OK;
250 }
251
252 static HRESULT WINAPI IErrorInfoImpl_GetSource(
253         IErrorInfo* iface,
254         BSTR *pBstrSource)
255 {
256         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
257         TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
258         if (pBstrSource == NULL)
259             return E_INVALIDARG;
260         *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
261         return S_OK;
262 }
263
264 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
265         IErrorInfo* iface,
266         BSTR *pBstrDescription)
267 {
268         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
269
270         TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
271         if (pBstrDescription == NULL)
272             return E_INVALIDARG;
273         *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
274
275         return S_OK;
276 }
277
278 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
279         IErrorInfo* iface,
280         BSTR *pBstrHelpFile)
281 {
282         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
283
284         TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
285         if (pBstrHelpFile == NULL)
286             return E_INVALIDARG;
287         *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
288
289         return S_OK;
290 }
291
292 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
293         IErrorInfo* iface,
294         DWORD *pdwHelpContext)
295 {
296         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
297         TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
298         if (pdwHelpContext == NULL)
299             return E_INVALIDARG;
300         *pdwHelpContext = This->m_dwHelpContext;
301
302         return S_OK;
303 }
304
305 static ICOM_VTABLE(IErrorInfo) IErrorInfoImpl_VTable =
306 {
307   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
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)\n",This);
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)\n",This);
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)\n",This);
389         if (This->bstrHelpFile != NULL)
390             ERRORINFO_SysFreeString(This->bstrHelpFile);
391         This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
392
393         return S_OK;
394 }
395
396 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
397         ICreateErrorInfo* iface,
398         DWORD dwHelpContext)
399 {
400         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
401         TRACE("(%p)\n",This);
402         This->m_dwHelpContext = dwHelpContext;
403
404         return S_OK;
405 }
406
407 static ICOM_VTABLE(ICreateErrorInfo) ICreateErrorInfoImpl_VTable =
408 {
409   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
410   ICreateErrorInfoImpl_QueryInterface,
411   ICreateErrorInfoImpl_AddRef,
412   ICreateErrorInfoImpl_Release,
413
414   ICreateErrorInfoImpl_SetGUID,
415   ICreateErrorInfoImpl_SetSource,
416   ICreateErrorInfoImpl_SetDescription,
417   ICreateErrorInfoImpl_SetHelpFile,
418   ICreateErrorInfoImpl_SetHelpContext
419 };
420
421 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
422         ISupportErrorInfo* iface,
423         REFIID     riid,
424         VOID**     ppvoid)
425 {
426         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
427         TRACE("(%p)\n", This);
428
429         return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
430 }
431
432 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
433         ISupportErrorInfo* iface)
434 {
435         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
436         TRACE("(%p)\n", This);
437         return IErrorInfo_AddRef(_IErrorInfo_(This));
438 }
439
440 static ULONG WINAPI ISupportErrorInfoImpl_Release(
441         ISupportErrorInfo* iface)
442 {
443         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
444         TRACE("(%p)\n", This);
445         return IErrorInfo_Release(_IErrorInfo_(This));
446 }
447
448
449 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
450         ISupportErrorInfo* iface,
451         REFIID riid)
452 {
453         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
454         TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
455         return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
456 }
457
458 static ICOM_VTABLE(ISupportErrorInfo) ISupportErrorInfoImpl_VTable =
459 {
460   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
461   ISupportErrorInfoImpl_QueryInterface,
462   ISupportErrorInfoImpl_AddRef,
463   ISupportErrorInfoImpl_Release,
464
465
466   ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
467 };
468 /***********************************************************************
469  *              CreateErrorInfo (OLE32.@)
470  */
471 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
472 {
473         IErrorInfo * pei;
474         HRESULT res;
475         TRACE("(%p): stub:\n", pperrinfo);
476         if(! pperrinfo ) return E_INVALIDARG;
477         if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
478
479         res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
480         IErrorInfo_Release(pei);
481         return res;
482 }
483
484 /***********************************************************************
485  *              GetErrorInfo (OLE32.@)
486  */
487 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
488 {
489         APARTMENT * apt = COM_CurrentInfo();
490
491         TRACE("(%ld, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->ErrorInfo);
492
493         if(!pperrinfo) return E_INVALIDARG;
494         if (!apt || !apt->ErrorInfo)
495         {
496            *pperrinfo = NULL;
497            return S_FALSE;
498         }
499
500         *pperrinfo = (IErrorInfo*)(apt->ErrorInfo);
501         /* clear thread error state */
502         apt->ErrorInfo = NULL;
503         return S_OK;
504 }
505
506 /***********************************************************************
507  *              SetErrorInfo (OLE32.@)
508  */
509 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
510 {
511         IErrorInfo * pei;
512         APARTMENT * apt = COM_CurrentInfo();
513
514         TRACE("(%ld, %p)\n", dwReserved, perrinfo);
515         
516         if (!apt) apt = COM_CreateApartment(COINIT_UNINITIALIZED);
517
518         /* release old errorinfo */
519         pei = (IErrorInfo*)apt->ErrorInfo;
520         if(pei) IErrorInfo_Release(pei);
521
522         /* set to new value */
523         apt->ErrorInfo = perrinfo;
524         if(perrinfo) IErrorInfo_AddRef(perrinfo);
525         return S_OK;
526 }