Improved a bit link handling (a few more link types loaded from file
[wine/hacks.git] / dlls / ole32 / errorinfo.c
blob2e4598bf875eaeba5159d7b82393bc473f7eae67
1 /*
2 * ErrorInfo API
4 * Copyright 2000 Patrik Stridvall, Juergen Schmied
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.
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.
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
20 * NOTES:
22 * The errorinfo is a per-thread object. The reference is stored in the
23 * TEB at offset 0xf80
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "oleauto.h"
31 #include "winerror.h"
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"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 /* this code is from SysAllocStringLen (ole2disp.c in oleaut32) */
44 static BSTR WINAPI ERRORINFO_SysAllocString(const OLECHAR* in)
46 DWORD bufferSize;
47 DWORD* newBuffer;
48 WCHAR* stringBuffer;
49 DWORD len;
51 if (in == NULL)
52 return NULL;
54 * Find the lenth of the buffer passed-in in bytes.
56 len = strlenW(in);
57 bufferSize = len * sizeof (WCHAR);
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'.
65 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
67 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
70 * If the memory allocation failed, return a null pointer.
72 if (newBuffer==0)
73 return 0;
76 * Copy the length of the string in the placeholder.
78 *newBuffer = bufferSize;
81 * Skip the byte count.
83 newBuffer++;
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.
90 if (in != 0)
91 memcpy(newBuffer, in, bufferSize);
92 else
93 memset(newBuffer, 0, bufferSize);
96 * Make sure that there is a nul character at the end of the
97 * string.
99 stringBuffer = (WCHAR*)newBuffer;
100 stringBuffer[len] = 0;
102 return (LPWSTR)stringBuffer;
105 /* this code is from SysFreeString (ole2disp.c in oleaut32)*/
106 static VOID WINAPI ERRORINFO_SysFreeString(BSTR in)
108 DWORD* bufferPointer;
110 /* NULL is a valid parameter */
111 if(!in) return;
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.
118 bufferPointer = (DWORD*)in;
120 bufferPointer--;
123 * Free the memory from it's "real" origin.
125 HeapFree(GetProcessHeap(), 0, bufferPointer);
129 typedef struct ErrorInfoImpl
131 ICOM_VTABLE(IErrorInfo) *lpvtei;
132 ICOM_VTABLE(ICreateErrorInfo) *lpvtcei;
133 ICOM_VTABLE(ISupportErrorInfo) *lpvtsei;
134 DWORD ref;
136 GUID m_Guid;
137 BSTR bstrSource;
138 BSTR bstrDescription;
139 BSTR bstrHelpFile;
140 DWORD m_dwHelpContext;
141 } ErrorInfoImpl;
143 static ICOM_VTABLE(IErrorInfo) IErrorInfoImpl_VTable;
144 static ICOM_VTABLE(ICreateErrorInfo) ICreateErrorInfoImpl_VTable;
145 static ICOM_VTABLE(ISupportErrorInfo) ISupportErrorInfoImpl_VTable;
148 converts a objectpointer to This
150 #define _IErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtei)))
151 #define _ICOM_THIS_From_IErrorInfo(class, name) class* This = (class*)(((char*)name)-_IErrorInfo_Offset);
153 #define _ICreateErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtcei)))
154 #define _ICOM_THIS_From_ICreateErrorInfo(class, name) class* This = (class*)(((char*)name)-_ICreateErrorInfo_Offset);
156 #define _ISupportErrorInfo_Offset ((int)(&(((ErrorInfoImpl*)0)->lpvtsei)))
157 #define _ICOM_THIS_From_ISupportErrorInfo(class, name) class* This = (class*)(((char*)name)-_ISupportErrorInfo_Offset);
160 converts This to a objectpointer
162 #define _IErrorInfo_(This) (IErrorInfo*)&(This->lpvtei)
163 #define _ICreateErrorInfo_(This) (ICreateErrorInfo*)&(This->lpvtcei)
164 #define _ISupportErrorInfo_(This) (ISupportErrorInfo*)&(This->lpvtsei)
166 IErrorInfo * IErrorInfoImpl_Constructor()
168 ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
169 if (ei)
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;
180 return (IErrorInfo *)ei;
184 static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
185 IErrorInfo* iface,
186 REFIID riid,
187 VOID** ppvoid)
189 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
190 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);
192 *ppvoid = NULL;
194 if(IsEqualIID(riid, &IID_IErrorInfo))
196 *ppvoid = _IErrorInfo_(This);
198 else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
200 *ppvoid = _ICreateErrorInfo_(This);
202 else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
204 *ppvoid = _ISupportErrorInfo_(This);
207 if(*ppvoid)
209 IUnknown_AddRef( (IUnknown*)*ppvoid );
210 TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
211 return S_OK;
213 TRACE("-- Interface: E_NOINTERFACE\n");
214 return E_NOINTERFACE;
217 static ULONG WINAPI IErrorInfoImpl_AddRef(
218 IErrorInfo* iface)
220 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
221 TRACE("(%p)->(count=%lu)\n",This,This->ref);
222 return InterlockedIncrement(&This->ref);
225 static ULONG WINAPI IErrorInfoImpl_Release(
226 IErrorInfo* iface)
228 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
229 TRACE("(%p)->(count=%lu)\n",This,This->ref);
231 if (!InterlockedDecrement(&This->ref))
233 TRACE("-- destroying IErrorInfo(%p)\n",This);
234 HeapFree(GetProcessHeap(),0,This);
235 return 0;
237 return This->ref;
240 static HRESULT WINAPI IErrorInfoImpl_GetGUID(
241 IErrorInfo* iface,
242 GUID * pGUID)
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;
251 static HRESULT WINAPI IErrorInfoImpl_GetSource(
252 IErrorInfo* iface,
253 BSTR *pBstrSource)
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;
263 static HRESULT WINAPI IErrorInfoImpl_GetDescription(
264 IErrorInfo* iface,
265 BSTR *pBstrDescription)
267 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
269 TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
270 if (pBstrDescription == NULL)
271 return E_INVALIDARG;
272 *pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
274 return S_OK;
277 static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
278 IErrorInfo* iface,
279 BSTR *pBstrHelpFile)
281 _ICOM_THIS_From_IErrorInfo(ErrorInfoImpl, iface);
283 TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
284 if (pBstrHelpFile == NULL)
285 return E_INVALIDARG;
286 *pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
288 return S_OK;
291 static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
292 IErrorInfo* iface,
293 DWORD *pdwHelpContext)
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;
301 return S_OK;
304 static ICOM_VTABLE(IErrorInfo) IErrorInfoImpl_VTable =
306 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
307 IErrorInfoImpl_QueryInterface,
308 IErrorInfoImpl_AddRef,
309 IErrorInfoImpl_Release,
311 IErrorInfoImpl_GetGUID,
312 IErrorInfoImpl_GetSource,
313 IErrorInfoImpl_GetDescription,
314 IErrorInfoImpl_GetHelpFile,
315 IErrorInfoImpl_GetHelpContext
319 static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
320 ICreateErrorInfo* iface,
321 REFIID riid,
322 VOID** ppvoid)
324 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
325 TRACE("(%p)\n", This);
326 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
329 static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
330 ICreateErrorInfo* iface)
332 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
333 TRACE("(%p)\n", This);
334 return IErrorInfo_AddRef(_IErrorInfo_(This));
337 static ULONG WINAPI ICreateErrorInfoImpl_Release(
338 ICreateErrorInfo* iface)
340 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
341 TRACE("(%p)\n", This);
342 return IErrorInfo_Release(_IErrorInfo_(This));
346 static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
347 ICreateErrorInfo* iface,
348 REFGUID rguid)
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;
356 static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
357 ICreateErrorInfo* iface,
358 LPOLESTR szSource)
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);
366 return S_OK;
369 static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
370 ICreateErrorInfo* iface,
371 LPOLESTR szDescription)
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);
379 return S_OK;
382 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
383 ICreateErrorInfo* iface,
384 LPOLESTR szHelpFile)
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);
392 return S_OK;
395 static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
396 ICreateErrorInfo* iface,
397 DWORD dwHelpContext)
399 _ICOM_THIS_From_ICreateErrorInfo(ErrorInfoImpl, iface);
400 TRACE("(%p)\n",This);
401 This->m_dwHelpContext = dwHelpContext;
403 return S_OK;
406 static ICOM_VTABLE(ICreateErrorInfo) ICreateErrorInfoImpl_VTable =
408 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
409 ICreateErrorInfoImpl_QueryInterface,
410 ICreateErrorInfoImpl_AddRef,
411 ICreateErrorInfoImpl_Release,
413 ICreateErrorInfoImpl_SetGUID,
414 ICreateErrorInfoImpl_SetSource,
415 ICreateErrorInfoImpl_SetDescription,
416 ICreateErrorInfoImpl_SetHelpFile,
417 ICreateErrorInfoImpl_SetHelpContext
420 static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
421 ISupportErrorInfo* iface,
422 REFIID riid,
423 VOID** ppvoid)
425 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
426 TRACE("(%p)\n", This);
428 return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
431 static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
432 ISupportErrorInfo* iface)
434 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
435 TRACE("(%p)\n", This);
436 return IErrorInfo_AddRef(_IErrorInfo_(This));
439 static ULONG WINAPI ISupportErrorInfoImpl_Release(
440 ISupportErrorInfo* iface)
442 _ICOM_THIS_From_ISupportErrorInfo(ErrorInfoImpl, iface);
443 TRACE("(%p)\n", This);
444 return IErrorInfo_Release(_IErrorInfo_(This));
448 static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
449 ISupportErrorInfo* iface,
450 REFIID riid)
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;
457 static ICOM_VTABLE(ISupportErrorInfo) ISupportErrorInfoImpl_VTable =
459 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
460 ISupportErrorInfoImpl_QueryInterface,
461 ISupportErrorInfoImpl_AddRef,
462 ISupportErrorInfoImpl_Release,
465 ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
467 /***********************************************************************
468 * CreateErrorInfo (OLE32.192)
470 HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
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;
478 res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
479 IErrorInfo_Release(pei);
480 return res;
483 /***********************************************************************
484 * GetErrorInfo (OLE32.196)
486 HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
488 TRACE("(%ld, %p, %p): stub:\n", dwReserved, pperrinfo, NtCurrentTeb()->ErrorInfo);
490 if(! pperrinfo ) return E_INVALIDARG;
491 if(!(*pperrinfo = (IErrorInfo*)(NtCurrentTeb()->ErrorInfo))) return S_FALSE;
493 /* clear thread error state */
494 NtCurrentTeb()->ErrorInfo = NULL;
495 return S_OK;
498 /***********************************************************************
499 * SetErrorInfo (OLE32.255)
501 HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
503 IErrorInfo * pei;
504 TRACE("(%ld, %p): stub:\n", dwReserved, perrinfo);
506 /* release old errorinfo */
507 pei = (IErrorInfo*)NtCurrentTeb()->ErrorInfo;
508 if(pei) IErrorInfo_Release(pei);
510 /* set to new value */
511 NtCurrentTeb()->ErrorInfo = perrinfo;
512 if(perrinfo) IErrorInfo_AddRef(perrinfo);
513 return S_OK;