ole32: Add on the size of the whole OBJREF structure in the case of custom marshaling.
[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         const IErrorInfoVtbl           *lpvtei;
132         const ICreateErrorInfoVtbl     *lpvtcei;
133         const ISupportErrorInfoVtbl    *lpvtsei;
134         LONG                            ref;
135
136         GUID m_Guid;
137         BSTR bstrSource;
138         BSTR bstrDescription;
139         BSTR bstrHelpFile;
140         DWORD m_dwHelpContext;
141 } ErrorInfoImpl;
142
143 static const IErrorInfoVtbl        IErrorInfoImpl_VTable;
144 static const ICreateErrorInfoVtbl  ICreateErrorInfoImpl_VTable;
145 static const ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable;
146
147 /*
148  converts an object pointer to This
149  */
150
151 static inline ErrorInfoImpl *impl_from_IErrorInfo( IErrorInfo *iface )
152 {
153     return (ErrorInfoImpl *)((char*)iface - FIELD_OFFSET(ErrorInfoImpl, lpvtei));
154 }
155
156 static inline ErrorInfoImpl *impl_from_ICreateErrorInfo( ICreateErrorInfo *iface )
157 {
158     return (ErrorInfoImpl *)((char*)iface - FIELD_OFFSET(ErrorInfoImpl, lpvtcei));
159 }
160
161 static inline ErrorInfoImpl *impl_from_ISupportErrorInfo( ISupportErrorInfo *iface )
162 {
163     return (ErrorInfoImpl *)((char*)iface - FIELD_OFFSET(ErrorInfoImpl, lpvtsei));
164 }
165
166
167 /*
168  converts This to an object pointer
169  */
170 #define _IErrorInfo_(This)              (IErrorInfo*)&(This->lpvtei)
171 #define _ICreateErrorInfo_(This)        (ICreateErrorInfo*)&(This->lpvtcei)
172 #define _ISupportErrorInfo_(This)       (ISupportErrorInfo*)&(This->lpvtsei)
173
174 IErrorInfo * IErrorInfoImpl_Constructor(void)
175 {
176         ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
177         if (ei)
178         {
179           ei->lpvtei = &IErrorInfoImpl_VTable;
180           ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
181           ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
182           ei->ref = 1;
183           ei->bstrSource = NULL;
184           ei->bstrDescription = NULL;
185           ei->bstrHelpFile = NULL;
186           ei->m_dwHelpContext = 0;
187         }
188         return (IErrorInfo *)ei;
189 }
190
191
192 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
193         IErrorInfo* iface,
194         REFIID     riid,
195         VOID**     ppvoid)
196 {
197         ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
198         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);
199
200         *ppvoid = NULL;
201
202         if(IsEqualIID(riid, &IID_IErrorInfo))
203         {
204           *ppvoid = _IErrorInfo_(This);
205         }
206         else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
207         {
208           *ppvoid = _ICreateErrorInfo_(This);
209         }
210         else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
211         {
212           *ppvoid = _ISupportErrorInfo_(This);
213         }
214
215         if(*ppvoid)
216         {
217           IUnknown_AddRef( (IUnknown*)*ppvoid );
218           TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
219           return S_OK;
220         }
221         TRACE("-- Interface: E_NOINTERFACE\n");
222         return E_NOINTERFACE;
223 }
224
225 static ULONG WINAPI IErrorInfoImpl_AddRef(
226         IErrorInfo* iface)
227 {
228         ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
229         TRACE("(%p)->(count=%lu)\n",This,This->ref);
230         return InterlockedIncrement(&This->ref);
231 }
232
233 static ULONG WINAPI IErrorInfoImpl_Release(
234         IErrorInfo* iface)
235 {
236         ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
237         ULONG ref = InterlockedDecrement(&This->ref);
238
239         TRACE("(%p)->(count=%lu)\n",This,ref+1);
240
241         if (!ref)
242         {
243           TRACE("-- destroying IErrorInfo(%p)\n",This);
244           HeapFree(GetProcessHeap(),0,This);
245           return 0;
246         }
247         return ref;
248 }
249
250 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
251         IErrorInfo* iface,
252         GUID * pGUID)
253 {
254         ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
255         TRACE("(%p)->(count=%lu)\n",This,This->ref);
256         if(!pGUID )return E_INVALIDARG;
257         memcpy(pGUID, &This->m_Guid, sizeof(GUID));
258         return S_OK;
259 }
260
261 static HRESULT WINAPI IErrorInfoImpl_GetSource(
262         IErrorInfo* iface,
263         BSTR *pBstrSource)
264 {
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;
271 }
272
273 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
274         IErrorInfo* iface,
275         BSTR *pBstrDescription)
276 {
277         ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
278
279         TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
280         if (pBstrDescription == NULL)
281             return E_INVALIDARG;
282         *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
283
284         return S_OK;
285 }
286
287 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
288         IErrorInfo* iface,
289         BSTR *pBstrHelpFile)
290 {
291         ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
292
293         TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
294         if (pBstrHelpFile == NULL)
295             return E_INVALIDARG;
296         *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
297
298         return S_OK;
299 }
300
301 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
302         IErrorInfo* iface,
303         DWORD *pdwHelpContext)
304 {
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;
310
311         return S_OK;
312 }
313
314 static const IErrorInfoVtbl IErrorInfoImpl_VTable =
315 {
316   IErrorInfoImpl_QueryInterface,
317   IErrorInfoImpl_AddRef,
318   IErrorInfoImpl_Release,
319
320   IErrorInfoImpl_GetGUID,
321   IErrorInfoImpl_GetSource,
322   IErrorInfoImpl_GetDescription,
323   IErrorInfoImpl_GetHelpFile,
324   IErrorInfoImpl_GetHelpContext
325 };
326
327
328 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
329         ICreateErrorInfo* iface,
330         REFIID     riid,
331         VOID**     ppvoid)
332 {
333         ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
334         TRACE("(%p)\n", This);
335         return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
336 }
337
338 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
339         ICreateErrorInfo* iface)
340 {
341         ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
342         TRACE("(%p)\n", This);
343         return IErrorInfo_AddRef(_IErrorInfo_(This));
344 }
345
346 static ULONG WINAPI ICreateErrorInfoImpl_Release(
347         ICreateErrorInfo* iface)
348 {
349         ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
350         TRACE("(%p)\n", This);
351         return IErrorInfo_Release(_IErrorInfo_(This));
352 }
353
354
355 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
356         ICreateErrorInfo* iface,
357         REFGUID rguid)
358 {
359         ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
360         TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
361         memcpy(&This->m_Guid,  rguid, sizeof(GUID));
362         return S_OK;
363 }
364
365 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
366         ICreateErrorInfo* iface,
367         LPOLESTR szSource)
368 {
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);
374
375         return S_OK;
376 }
377
378 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
379         ICreateErrorInfo* iface,
380         LPOLESTR szDescription)
381 {
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);
387
388         return S_OK;
389 }
390
391 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
392         ICreateErrorInfo* iface,
393         LPOLESTR szHelpFile)
394 {
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;
401 }
402
403 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
404         ICreateErrorInfo* iface,
405         DWORD dwHelpContext)
406 {
407         ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
408         TRACE("(%p,%ld)\n",This,dwHelpContext);
409         This->m_dwHelpContext = dwHelpContext;
410         return S_OK;
411 }
412
413 static const ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable =
414 {
415   ICreateErrorInfoImpl_QueryInterface,
416   ICreateErrorInfoImpl_AddRef,
417   ICreateErrorInfoImpl_Release,
418
419   ICreateErrorInfoImpl_SetGUID,
420   ICreateErrorInfoImpl_SetSource,
421   ICreateErrorInfoImpl_SetDescription,
422   ICreateErrorInfoImpl_SetHelpFile,
423   ICreateErrorInfoImpl_SetHelpContext
424 };
425
426 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
427         ISupportErrorInfo* iface,
428         REFIID     riid,
429         VOID**     ppvoid)
430 {
431         ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
432         TRACE("(%p)\n", This);
433
434         return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
435 }
436
437 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
438         ISupportErrorInfo* iface)
439 {
440         ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
441         TRACE("(%p)\n", This);
442         return IErrorInfo_AddRef(_IErrorInfo_(This));
443 }
444
445 static ULONG WINAPI ISupportErrorInfoImpl_Release(
446         ISupportErrorInfo* iface)
447 {
448         ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
449         TRACE("(%p)\n", This);
450         return IErrorInfo_Release(_IErrorInfo_(This));
451 }
452
453
454 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
455         ISupportErrorInfo* iface,
456         REFIID riid)
457 {
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;
461 }
462
463 static const ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable =
464 {
465   ISupportErrorInfoImpl_QueryInterface,
466   ISupportErrorInfoImpl_AddRef,
467   ISupportErrorInfoImpl_Release,
468
469
470   ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
471 };
472 /***********************************************************************
473  *              CreateErrorInfo (OLE32.@)
474  */
475 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
476 {
477         IErrorInfo * pei;
478         HRESULT res;
479         TRACE("(%p)\n", pperrinfo);
480         if(! pperrinfo ) return E_INVALIDARG;
481         if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
482
483         res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
484         IErrorInfo_Release(pei);
485         return res;
486 }
487
488 /***********************************************************************
489  *              GetErrorInfo (OLE32.@)
490  */
491 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
492 {
493         TRACE("(%ld, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->errorinfo);
494
495         if(!pperrinfo) return E_INVALIDARG;
496         
497         if (!COM_CurrentInfo()->errorinfo)
498         {
499            *pperrinfo = NULL;
500            return S_FALSE;
501         }
502
503         *pperrinfo = COM_CurrentInfo()->errorinfo;
504         
505         /* clear thread error state */
506         COM_CurrentInfo()->errorinfo = NULL;
507         return S_OK;
508 }
509
510 /***********************************************************************
511  *              SetErrorInfo (OLE32.@)
512  */
513 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
514 {
515         IErrorInfo * pei;
516
517         TRACE("(%ld, %p)\n", dwReserved, perrinfo);
518         
519         /* release old errorinfo */
520         pei = COM_CurrentInfo()->errorinfo;
521         if (pei) IErrorInfo_Release(pei);
522
523         /* set to new value */
524         COM_CurrentInfo()->errorinfo = perrinfo;
525         if (perrinfo) IErrorInfo_AddRef(perrinfo);
526         
527         return S_OK;
528 }