errorinfo.c 14.3 KB
Newer Older
1 2 3
/*
 * ErrorInfo API
 *
4
 * Copyright 2000 Patrik Stridvall, Juergen Schmied
5
 *
6 7 8 9 10 11 12 13 14 15 16 17
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 30
#define COBJMACROS

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 45 46 47 48 49 50

/* this code is from SysAllocStringLen (ole2disp.c in oleaut32) */
static BSTR WINAPI ERRORINFO_SysAllocString(const OLECHAR* in)
{
    DWORD  bufferSize;
    DWORD* newBuffer;
    WCHAR* stringBuffer;
    DWORD len;
51

52 53 54
    if (in == NULL)
	return NULL;
    /*
55
     * Find the length of the buffer passed-in, in bytes.
56 57 58 59 60 61
     */
    len = strlenW(in);
    bufferSize = len * sizeof (WCHAR);

    /*
     * 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
                                 bufferSize + sizeof(WCHAR) + sizeof(DWORD));

    /*
     * If the memory allocation failed, return a null pointer.
     */
    if (newBuffer==0)
      return 0;

    /*
     * Copy the length of the string in the placeholder.
     */
    *newBuffer = bufferSize;

    /*
     * Skip the byte count.
     */
    newBuffer++;

    /*
86 87
     * Copy the information in the buffer.  It is not possible to pass 
     * a NULL pointer here. 
88
     */
89
    memcpy(newBuffer, in, bufferSize);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    /*
     * Make sure that there is a nul character at the end of the
     * string.
     */
    stringBuffer = (WCHAR*)newBuffer;
    stringBuffer[len] = 0;

    return (LPWSTR)stringBuffer;
}

/* this code is from SysFreeString (ole2disp.c in oleaut32)*/
static VOID WINAPI ERRORINFO_SysFreeString(BSTR in)
{
    DWORD* bufferPointer;
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    /* NULL is a valid parameter */
    if(!in) return;

    /*
     * We have to be careful when we free a BSTR pointer, it points to
     * the beginning of the string but it skips the byte count contained
     * before the string.
     */
    bufferPointer = (DWORD*)in;

    bufferPointer--;

    /*
     * Free the memory from it's "real" origin.
     */
    HeapFree(GetProcessHeap(), 0, bufferPointer);
}
123 124


125 126
typedef struct ErrorInfoImpl
{
127 128 129
	const IErrorInfoVtbl           *lpvtei;
	const ICreateErrorInfoVtbl     *lpvtcei;
	const ISupportErrorInfoVtbl    *lpvtsei;
130
	LONG				ref;
131

132 133 134 135 136
	GUID m_Guid;
	BSTR bstrSource;
	BSTR bstrDescription;
	BSTR bstrHelpFile;
	DWORD m_dwHelpContext;
137 138
} ErrorInfoImpl;

139 140 141
static const IErrorInfoVtbl        IErrorInfoImpl_VTable;
static const ICreateErrorInfoVtbl  ICreateErrorInfoImpl_VTable;
static const ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable;
142 143

/*
144
 converts an object pointer to This
145 146
 */

147 148 149 150 151 152 153 154 155 156 157 158 159 160
static inline ErrorInfoImpl *impl_from_IErrorInfo( IErrorInfo *iface )
{
    return (ErrorInfoImpl *)((char*)iface - FIELD_OFFSET(ErrorInfoImpl, lpvtei));
}

static inline ErrorInfoImpl *impl_from_ICreateErrorInfo( ICreateErrorInfo *iface )
{
    return (ErrorInfoImpl *)((char*)iface - FIELD_OFFSET(ErrorInfoImpl, lpvtcei));
}

static inline ErrorInfoImpl *impl_from_ISupportErrorInfo( ISupportErrorInfo *iface )
{
    return (ErrorInfoImpl *)((char*)iface - FIELD_OFFSET(ErrorInfoImpl, lpvtsei));
}
161 162 163


/*
164
 converts This to an object pointer
165 166 167 168 169
 */
#define _IErrorInfo_(This)		(IErrorInfo*)&(This->lpvtei)
#define _ICreateErrorInfo_(This)	(ICreateErrorInfo*)&(This->lpvtcei)
#define _ISupportErrorInfo_(This)	(ISupportErrorInfo*)&(This->lpvtsei)

170
static IErrorInfo * IErrorInfoImpl_Constructor(void)
171 172 173 174 175 176 177 178
{
	ErrorInfoImpl * ei = HeapAlloc(GetProcessHeap(), 0, sizeof(ErrorInfoImpl));
	if (ei)
	{
	  ei->lpvtei = &IErrorInfoImpl_VTable;
	  ei->lpvtcei = &ICreateErrorInfoImpl_VTable;
	  ei->lpvtsei = &ISupportErrorInfoImpl_VTable;
	  ei->ref = 1;
179 180 181 182
	  ei->bstrSource = NULL;
	  ei->bstrDescription = NULL;
	  ei->bstrHelpFile = NULL;
	  ei->m_dwHelpContext = 0;
183 184 185 186 187 188 189 190 191 192
	}
	return (IErrorInfo *)ei;
}


static HRESULT WINAPI IErrorInfoImpl_QueryInterface(
	IErrorInfo* iface,
	REFIID     riid,
	VOID**     ppvoid)
{
193
	ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
194 195 196 197 198 199
	TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvoid);

	*ppvoid = NULL;

	if(IsEqualIID(riid, &IID_IErrorInfo))
	{
200
	  *ppvoid = _IErrorInfo_(This);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	}
	else if(IsEqualIID(riid, &IID_ICreateErrorInfo))
	{
	  *ppvoid = _ICreateErrorInfo_(This);
	}
	else if(IsEqualIID(riid, &IID_ISupportErrorInfo))
	{
	  *ppvoid = _ISupportErrorInfo_(This);
	}

	if(*ppvoid)
	{
	  IUnknown_AddRef( (IUnknown*)*ppvoid );
	  TRACE("-- Interface: (%p)->(%p)\n",ppvoid,*ppvoid);
	  return S_OK;
	}
	TRACE("-- Interface: E_NOINTERFACE\n");
	return E_NOINTERFACE;
}

static ULONG WINAPI IErrorInfoImpl_AddRef(
 	IErrorInfo* iface)
{
224
	ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
225
	TRACE("(%p)->(count=%u)\n",This,This->ref);
226 227 228 229 230 231
	return InterlockedIncrement(&This->ref);
}

static ULONG WINAPI IErrorInfoImpl_Release(
	IErrorInfo* iface)
{
232
	ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
233 234
        ULONG ref = InterlockedDecrement(&This->ref);

235
	TRACE("(%p)->(count=%u)\n",This,ref+1);
236

237
	if (!ref)
238 239 240 241 242
	{
	  TRACE("-- destroying IErrorInfo(%p)\n",This);
	  HeapFree(GetProcessHeap(),0,This);
	  return 0;
	}
243
	return ref;
244 245 246 247
}

static HRESULT WINAPI IErrorInfoImpl_GetGUID(
	IErrorInfo* iface,
248
	GUID * pGUID)
249
{
250
	ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
251
	TRACE("(%p)->(count=%u)\n",This,This->ref);
252 253
	if(!pGUID )return E_INVALIDARG;
	memcpy(pGUID, &This->m_Guid, sizeof(GUID));
254 255 256 257 258 259 260
	return S_OK;
}

static HRESULT WINAPI IErrorInfoImpl_GetSource(
	IErrorInfo* iface,
	BSTR *pBstrSource)
{
261
	ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
262 263 264 265 266
	TRACE("(%p)->(pBstrSource=%p)\n",This,pBstrSource);
	if (pBstrSource == NULL)
	    return E_INVALIDARG;
	*pBstrSource = ERRORINFO_SysAllocString(This->bstrSource);
	return S_OK;
267 268 269 270 271 272
}

static HRESULT WINAPI IErrorInfoImpl_GetDescription(
	IErrorInfo* iface,
	BSTR *pBstrDescription)
{
273
	ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
274 275 276 277 278

	TRACE("(%p)->(pBstrDescription=%p)\n",This,pBstrDescription);
	if (pBstrDescription == NULL)
	    return E_INVALIDARG;
	*pBstrDescription = ERRORINFO_SysAllocString(This->bstrDescription);
279

280
	return S_OK;
281 282 283 284 285 286
}

static HRESULT WINAPI IErrorInfoImpl_GetHelpFile(
	IErrorInfo* iface,
	BSTR *pBstrHelpFile)
{
287
	ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
288 289 290 291 292

	TRACE("(%p)->(pBstrHelpFile=%p)\n",This, pBstrHelpFile);
	if (pBstrHelpFile == NULL)
	    return E_INVALIDARG;
	*pBstrHelpFile = ERRORINFO_SysAllocString(This->bstrHelpFile);
293

294
	return S_OK;
295 296 297 298 299 300
}

static HRESULT WINAPI IErrorInfoImpl_GetHelpContext(
	IErrorInfo* iface,
	DWORD *pdwHelpContext)
{
301
	ErrorInfoImpl *This = impl_from_IErrorInfo(iface);
302 303 304 305
	TRACE("(%p)->(pdwHelpContext=%p)\n",This, pdwHelpContext);
	if (pdwHelpContext == NULL)
	    return E_INVALIDARG;
	*pdwHelpContext = This->m_dwHelpContext;
306

307
	return S_OK;
308 309
}

310
static const IErrorInfoVtbl IErrorInfoImpl_VTable =
311 312 313 314
{
  IErrorInfoImpl_QueryInterface,
  IErrorInfoImpl_AddRef,
  IErrorInfoImpl_Release,
315

316 317 318 319 320 321 322 323 324 325 326 327 328
  IErrorInfoImpl_GetGUID,
  IErrorInfoImpl_GetSource,
  IErrorInfoImpl_GetDescription,
  IErrorInfoImpl_GetHelpFile,
  IErrorInfoImpl_GetHelpContext
};


static HRESULT WINAPI ICreateErrorInfoImpl_QueryInterface(
	ICreateErrorInfo* iface,
	REFIID     riid,
	VOID**     ppvoid)
{
329
	ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
330 331 332 333 334 335 336
	TRACE("(%p)\n", This);
	return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
}

static ULONG WINAPI ICreateErrorInfoImpl_AddRef(
 	ICreateErrorInfo* iface)
{
337
	ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
338 339 340 341 342 343 344
	TRACE("(%p)\n", This);
	return IErrorInfo_AddRef(_IErrorInfo_(This));
}

static ULONG WINAPI ICreateErrorInfoImpl_Release(
	ICreateErrorInfo* iface)
{
345
	ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
346 347 348 349 350 351 352 353 354
	TRACE("(%p)\n", This);
	return IErrorInfo_Release(_IErrorInfo_(This));
}


static HRESULT WINAPI ICreateErrorInfoImpl_SetGUID(
	ICreateErrorInfo* iface,
	REFGUID rguid)
{
355
	ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
356
	TRACE("(%p)->(%s)\n", This, debugstr_guid(rguid));
357
	memcpy(&This->m_Guid,  rguid, sizeof(GUID));
358 359 360 361 362 363 364
	return S_OK;
}

static HRESULT WINAPI ICreateErrorInfoImpl_SetSource(
	ICreateErrorInfo* iface,
	LPOLESTR szSource)
{
365
	ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
366
	TRACE("(%p): %s\n",This, debugstr_w(szSource));
367 368 369
	if (This->bstrSource != NULL)
	    ERRORINFO_SysFreeString(This->bstrSource);
	This->bstrSource = ERRORINFO_SysAllocString(szSource);
370

371
	return S_OK;
372 373 374 375 376 377
}

static HRESULT WINAPI ICreateErrorInfoImpl_SetDescription(
	ICreateErrorInfo* iface,
	LPOLESTR szDescription)
{
378
	ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
379
	TRACE("(%p): %s\n",This, debugstr_w(szDescription));
380 381 382
	if (This->bstrDescription != NULL)
	    ERRORINFO_SysFreeString(This->bstrDescription);
	This->bstrDescription = ERRORINFO_SysAllocString(szDescription);
383

384
	return S_OK;
385 386 387 388 389 390
}

static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpFile(
	ICreateErrorInfo* iface,
	LPOLESTR szHelpFile)
{
391
	ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
392
	TRACE("(%p,%s)\n",This,debugstr_w(szHelpFile));
393 394 395 396
	if (This->bstrHelpFile != NULL)
	    ERRORINFO_SysFreeString(This->bstrHelpFile);
	This->bstrHelpFile = ERRORINFO_SysAllocString(szHelpFile);
	return S_OK;
397 398 399 400 401 402
}

static HRESULT WINAPI ICreateErrorInfoImpl_SetHelpContext(
	ICreateErrorInfo* iface,
 	DWORD dwHelpContext)
{
403
	ErrorInfoImpl *This = impl_from_ICreateErrorInfo(iface);
404
	TRACE("(%p,%d)\n",This,dwHelpContext);
405 406
	This->m_dwHelpContext = dwHelpContext;
	return S_OK;
407 408
}

409
static const ICreateErrorInfoVtbl ICreateErrorInfoImpl_VTable =
410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
{
  ICreateErrorInfoImpl_QueryInterface,
  ICreateErrorInfoImpl_AddRef,
  ICreateErrorInfoImpl_Release,

  ICreateErrorInfoImpl_SetGUID,
  ICreateErrorInfoImpl_SetSource,
  ICreateErrorInfoImpl_SetDescription,
  ICreateErrorInfoImpl_SetHelpFile,
  ICreateErrorInfoImpl_SetHelpContext
};

static HRESULT WINAPI ISupportErrorInfoImpl_QueryInterface(
	ISupportErrorInfo* iface,
	REFIID     riid,
	VOID**     ppvoid)
{
427
	ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
428
	TRACE("(%p)\n", This);
429

430 431 432 433 434 435
	return IErrorInfo_QueryInterface(_IErrorInfo_(This), riid, ppvoid);
}

static ULONG WINAPI ISupportErrorInfoImpl_AddRef(
 	ISupportErrorInfo* iface)
{
436
	ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
437 438 439 440 441 442 443
	TRACE("(%p)\n", This);
	return IErrorInfo_AddRef(_IErrorInfo_(This));
}

static ULONG WINAPI ISupportErrorInfoImpl_Release(
	ISupportErrorInfo* iface)
{
444
	ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
445 446 447 448 449 450 451 452 453
	TRACE("(%p)\n", This);
	return IErrorInfo_Release(_IErrorInfo_(This));
}


static HRESULT WINAPI ISupportErrorInfoImpl_InterfaceSupportsErrorInfo(
	ISupportErrorInfo* iface,
	REFIID riid)
{
454
	ErrorInfoImpl *This = impl_from_ISupportErrorInfo(iface);
455
	TRACE("(%p)->(%s)\n", This, debugstr_guid(riid));
456
	return (IsEqualIID(riid, &This->m_Guid)) ? S_OK : S_FALSE;
457 458
}

459
static const ISupportErrorInfoVtbl ISupportErrorInfoImpl_VTable =
460 461 462 463 464 465 466 467
{
  ISupportErrorInfoImpl_QueryInterface,
  ISupportErrorInfoImpl_AddRef,
  ISupportErrorInfoImpl_Release,


  ISupportErrorInfoImpl_InterfaceSupportsErrorInfo
};
468

469
/***********************************************************************
470
 *		CreateErrorInfo (OLE32.@)
471 472 473 474 475 476 477 478 479
 *
 * Creates an object used to set details for an error info object.
 *
 * PARAMS
 *  pperrinfo [O]. Address where error info creation object will be stored.
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: HRESULT code.
480 481 482
 */
HRESULT WINAPI CreateErrorInfo(ICreateErrorInfo **pperrinfo)
{
483 484
	IErrorInfo * pei;
	HRESULT res;
485
	TRACE("(%p)\n", pperrinfo);
486
	if(! pperrinfo ) return E_INVALIDARG;
487
	if(!(pei=IErrorInfoImpl_Constructor()))return E_OUTOFMEMORY;
488

489 490 491
	res = IErrorInfo_QueryInterface(pei, &IID_ICreateErrorInfo, (LPVOID*)pperrinfo);
	IErrorInfo_Release(pei);
	return res;
492 493 494
}

/***********************************************************************
495
 *		GetErrorInfo (OLE32.@)
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
 *
 * Retrieves the error information object for the current thread.
 *
 * PARAMS
 *  dwReserved [I]. Reserved. Must be zero.
 *  pperrinfo  [O]. Address where error information object will be stored on return.
 *
 * RETURNS
 *  Success: S_OK if an error information object was set for the current thread.
 *           S_FALSE if otherwise.
 *  Failure: E_INVALIDARG if dwReserved is not zero.
 *
 * NOTES
 *  This function causes the current error info object for the thread to be
 *  cleared if one was set beforehand.
511 512 513
 */
HRESULT WINAPI GetErrorInfo(ULONG dwReserved, IErrorInfo **pperrinfo)
{
514
	TRACE("(%d, %p, %p)\n", dwReserved, pperrinfo, COM_CurrentInfo()->errorinfo);
515

516 517 518 519 520 521
	if (dwReserved)
	{
		ERR("dwReserved (0x%x) != 0\n", dwReserved);
		return E_INVALIDARG;
	}

522
	if(!pperrinfo) return E_INVALIDARG;
523

524
	if (!COM_CurrentInfo()->errorinfo)
525 526 527 528 529
	{
	   *pperrinfo = NULL;
	   return S_FALSE;
	}

530 531
	*pperrinfo = COM_CurrentInfo()->errorinfo;
        
532
	/* clear thread error state */
533
	COM_CurrentInfo()->errorinfo = NULL;
534
	return S_OK;
535 536 537
}

/***********************************************************************
538
 *		SetErrorInfo (OLE32.@)
539 540 541 542 543 544 545 546 547 548
 *
 * Sets the error information object for the current thread.
 *
 * PARAMS
 *  dwReserved [I] Reserved. Must be zero.
 *  perrinfo   [I] Error info object.
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: E_INVALIDARG if dwReserved is not zero.
549 550 551
 */
HRESULT WINAPI SetErrorInfo(ULONG dwReserved, IErrorInfo *perrinfo)
{
552
	IErrorInfo * pei;
553

554
	TRACE("(%d, %p)\n", dwReserved, perrinfo);
555 556 557 558 559 560 561

	if (dwReserved)
	{
		ERR("dwReserved (0x%x) != 0\n", dwReserved);
		return E_INVALIDARG;
	}

562
	/* release old errorinfo */
563 564
	pei = COM_CurrentInfo()->errorinfo;
	if (pei) IErrorInfo_Release(pei);
565 566

	/* set to new value */
567 568 569
	COM_CurrentInfo()->errorinfo = perrinfo;
	if (perrinfo) IErrorInfo_AddRef(perrinfo);
        
570
	return S_OK;
571
}