Prevent crash when no URL is specified.
[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 = (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         IErrorInfoVtbl          *lpvtei;
133         ICreateErrorInfoVtbl    *lpvtcei;
134         ISupportErrorInfoVtbl   *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 IErrorInfoVtbl           IErrorInfoImpl_VTable;
145 static ICreateErrorInfoVtbl     ICreateErrorInfoImpl_VTable;
146 static ISupportErrorInfoVtbl    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         ULONG ref = InterlockedDecrement(&This->ref);
231
232         TRACE("(%p)->(count=%lu)\n",This,ref+1);
233
234         if (!ref)
235         {
236           TRACE("-- destroying IErrorInfo(%p)\n",This);
237           HeapFree(GetProcessHeap(),0,This);
238           return 0;
239         }
240         return ref;
241 }
242
243 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
244         IErrorInfo* iface,
245         GUID * pGUID)
246 {
247         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
248         TRACE("(%p)->(count=%lu)\n",This,This->ref);
249         if(!pGUID )return E_INVALIDARG;
250         memcpy(pGUID, &This->m_Guid, sizeof(GUID));
251         return S_OK;
252 }
253
254 static HRESULT WINAPI IErrorInfoImpl_GetSource(
255         IErrorInfo* iface,
256         BSTR *pBstrSource)
257 {
258         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
259         TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
260         if (pBstrSource == NULL)
261             return E_INVALIDARG;
262         *pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
263         return S_OK;
264 }
265
266 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
267         IErrorInfo* iface,
268         BSTR *pBstrDescription)
269 {
270         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
271
272         TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
273         if (pBstrDescription == NULL)
274             return E_INVALIDARG;
275         *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
276
277         return S_OK;
278 }
279
280 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
281         IErrorInfo* iface,
282         BSTR *pBstrHelpFile)
283 {
284         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
285
286         TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
287         if (pBstrHelpFile == NULL)
288             return E_INVALIDARG;
289         *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
290
291         return S_OK;
292 }
293
294 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
295         IErrorInfo* iface,
296         DWORD *pdwHelpContext)
297 {
298         _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
299         TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
300         if (pdwHelpContext == NULL)
301             return E_INVALIDARG;
302         *pdwHelpContext = This->m_dwHelpContext;
303
304         return S_OK;
305 }
306
307 static IErrorInfoVtbl IErrorInfoImpl_VTable =
308 {
309   IErrorInfoImpl_QueryInterface,
310   IErrorInfoImpl_AddRef,
311   IErrorInfoImpl_Release,
312
313   IErrorInfoImpl_GetGUID,
314   IErrorInfoImpl_GetSource,
315   IErrorInfoImpl_GetDescription,
316   IErrorInfoImpl_GetHelpFile,
317   IErrorInfoImpl_GetHelpContext
318 };
319
320
321 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
322         ICreateErrorInfo* iface,
323         REFIID     riid,
324         VOID**     ppvoid)
325 {
326         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
327         TRACE("(%p)\n", This);
328         return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
329 }
330
331 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
332         ICreateErrorInfo* iface)
333 {
334         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
335         TRACE("(%p)\n", This);
336         return IErrorInfo_AddRef(_IErrorInfo_(This));
337 }
338
339 static ULONG WINAPI ICreateErrorInfoImpl_Release(
340         ICreateErrorInfo* iface)
341 {
342         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
343         TRACE("(%p)\n", This);
344         return IErrorInfo_Release(_IErrorInfo_(This));
345 }
346
347
348 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
349         ICreateErrorInfo* iface,
350         REFGUID rguid)
351 {
352         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
353         TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
354         memcpy(&This->m_Guid,  rguid, sizeof(GUID));
355         return S_OK;
356 }
357
358 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
359         ICreateErrorInfo* iface,
360         LPOLESTR szSource)
361 {
362         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
363         TRACE("(%p): %s\n",This, debugstr_w(szSource));
364         if (This->bstrSource != NULL)
365             ERRORINFO_SysFreeString(This->bstrSource);
366         This->bstrSource = ERRORINFO_SysAllocString(szSource);
367
368         return S_OK;
369 }
370
371 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
372         ICreateErrorInfo* iface,
373         LPOLESTR szDescription)
374 {
375         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
376         TRACE("(%p): %s\n",This, debugstr_w(szDescription));
377         if (This->bstrDescription != NULL)
378             ERRORINFO_SysFreeString(This->bstrDescription);
379         This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
380
381         return S_OK;
382 }
383
384 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
385         ICreateErrorInfo* iface,
386         LPOLESTR szHelpFile)
387 {
388         _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
389         TRACE("(%p,%s)\n",This,debugstr_w(szHelpFile));
390         if (This->bstrHelpFile != NULL)
391             ERRORINFO_SysFreeString(This->bstrHelpFile);
392         This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
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,%ld)\n",This,dwHelpContext);
402         This->m_dwHelpContext = dwHelpContext;
403         return S_OK;
404 }
405
406 static ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable =
407 {
408   ICreateErrorInfoImpl_QueryInterface,
409   ICreateErrorInfoImpl_AddRef,
410   ICreateErrorInfoImpl_Release,
411
412   ICreateErrorInfoImpl_SetGUID,
413   ICreateErrorInfoImpl_SetSource,
414   ICreateErrorInfoImpl_SetDescription,
415   ICreateErrorInfoImpl_SetHelpFile,
416   ICreateErrorInfoImpl_SetHelpContext
417 };
418
419 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
420         ISupportErrorInfo* iface,
421         REFIID     riid,
422         VOID**     ppvoid)
423 {
424         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
425         TRACE("(%p)\n", This);
426
427         return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
428 }
429
430 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
431         ISupportErrorInfo* iface)
432 {
433         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
434         TRACE("(%p)\n", This);
435         return IErrorInfo_AddRef(_IErrorInfo_(This));
436 }
437
438 static ULONG WINAPI ISupportErrorInfoImpl_Release(
439         ISupportErrorInfo* iface)
440 {
441         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
442         TRACE("(%p)\n", This);
443         return IErrorInfo_Release(_IErrorInfo_(This));
444 }
445
446
447 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
448         ISupportErrorInfo* iface,
449         REFIID riid)
450 {
451         _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
452         TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
453         return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
454 }
455
456 static ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable =
457 {
458   ISupportErrorInfoImpl_QueryInterface,
459   ISupportErrorInfoImpl_AddRef,
460   ISupportErrorInfoImpl_Release,
461
462
463   ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
464 };
465 /***********************************************************************
466  *              CreateErrorInfo (OLE32.@)
467  */
468 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
469 {
470         IErrorInfo * pei;
471         HRESULT res;
472         TRACE("(%p): stub:\n", pperrinfo);
473         if(! pperrinfo ) return E_INVALIDARG;
474         if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
475
476         res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
477         IErrorInfo_Release(pei);
478         return res;
479 }
480
481 /***********************************************************************
482  *              GetErrorInfo (OLE32.@)
483  */
484 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
485 {
486         TRACE("(%ld, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->errorinfo);
487
488         if(!pperrinfo) return E_INVALIDARG;
489         
490         if (!COM_CurrentInfo()->errorinfo)
491         {
492            *pperrinfo = NULL;
493            return S_FALSE;
494         }
495
496         *pperrinfo = COM_CurrentInfo()->errorinfo;
497         
498         /* clear thread error state */
499         COM_CurrentInfo()->errorinfo = NULL;
500         return S_OK;
501 }
502
503 /***********************************************************************
504  *              SetErrorInfo (OLE32.@)
505  */
506 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
507 {
508         IErrorInfo * pei;
509
510         TRACE("(%ld, %p)\n", dwReserved, perrinfo);
511         
512         /* release old errorinfo */
513         pei = COM_CurrentInfo()->errorinfo;
514         if (pei) IErrorInfo_Release(pei);
515
516         /* set to new value */
517         COM_CurrentInfo()->errorinfo = perrinfo;
518         if (perrinfo) IErrorInfo_AddRef(perrinfo);
519         
520         return S_OK;
521 }