Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
6.7 KiB

  1. /*===================================================================
  2. Microsoft Denali
  3. Microsoft Confidential.
  4. Copyright 1998 Microsoft Corporation. All Rights Reserved.
  5. Component: ASPError object
  6. File: asperror.cpp
  7. Owner: dmitryr
  8. This file contains the code for the implementation of
  9. the ASPError class.
  10. ===================================================================*/
  11. #include "denpre.h"
  12. #pragma hdrstop
  13. #include "asperror.h"
  14. #include "memchk.h"
  15. /*===================================================================
  16. CASPError::CASPError
  17. Constructor for the empty error object
  18. Returns:
  19. ===================================================================*/
  20. CASPError::CASPError()
  21. :
  22. m_cRefs(1),
  23. m_szASPCode(NULL),
  24. m_lNumber(0),
  25. m_szSource(NULL),
  26. m_szFileName(NULL),
  27. m_lLineNumber(0),
  28. m_szDescription(NULL),
  29. m_szASPDescription(NULL),
  30. m_bstrLineText(NULL)
  31. {
  32. CDispatch::Init(IID_IASPError);
  33. }
  34. /*===================================================================
  35. CASPError::CASPError
  36. Constructor for real error object given CErrInfo
  37. Parameters
  38. pErrInfo [in] copy data from there
  39. Returns:
  40. ===================================================================*/
  41. CASPError::CASPError(CErrInfo *pErrInfo)
  42. :
  43. m_cRefs(1),
  44. m_szASPCode(NULL),
  45. m_lNumber(0),
  46. m_szSource(NULL),
  47. m_szFileName(NULL),
  48. m_lLineNumber(0),
  49. m_szDescription(NULL),
  50. m_szASPDescription(NULL),
  51. m_bstrLineText(NULL)
  52. {
  53. CDispatch::Init(IID_IASPError);
  54. if (!pErrInfo)
  55. return;
  56. // Parse ASP error code and HRESULT from szErrorCode
  57. CHAR *szErrorCode = StringDupA(pErrInfo->GetItem(Im_szErrorCode));
  58. if (szErrorCode != NULL)
  59. {
  60. CHAR *szC = strchr(szErrorCode, ':');
  61. if (szC)
  62. {
  63. // format "ASP XXX : HRESULT"
  64. szC[-1] = '\0';
  65. m_szASPCode = szErrorCode;
  66. m_lNumber = strtoul(szC+2, NULL, 16);
  67. }
  68. else if (strncmp(szErrorCode, "ASP", 3) == 0)
  69. {
  70. // format "ASP XXX"
  71. m_szASPCode = szErrorCode;
  72. m_lNumber = E_FAIL;
  73. }
  74. else
  75. {
  76. // format "HRESULT"
  77. m_szASPCode = NULL;
  78. m_lNumber = strtoul(szErrorCode, NULL, 16);
  79. free(szErrorCode);
  80. }
  81. }
  82. else
  83. {
  84. // no error description available
  85. m_szASPCode = NULL;
  86. m_lNumber = E_FAIL;
  87. }
  88. // Copy the rest
  89. m_szSource = StringDupA(pErrInfo->GetItem(Im_szEngine));
  90. m_szFileName = StringDupA(pErrInfo->GetItem(Im_szFileName));
  91. m_szDescription = StringDupA(pErrInfo->GetItem(Im_szShortDescription));
  92. m_szASPDescription = StringDupA(pErrInfo->GetItem(Im_szLongDescription));
  93. // Get line text & column (supplies init. values if not available)
  94. BSTR bstrLineText;
  95. pErrInfo->GetLineInfo(&bstrLineText, &m_nColumn);
  96. m_bstrLineText = SysAllocString(bstrLineText);
  97. // Line number if present
  98. if (pErrInfo->GetItem(Im_szLineNum))
  99. m_lLineNumber = atoi(pErrInfo->GetItem(Im_szLineNum));
  100. }
  101. /*===================================================================
  102. CASPError::~CASPError
  103. Destructor
  104. Parameters:
  105. Returns:
  106. ===================================================================*/
  107. CASPError::~CASPError()
  108. {
  109. Assert(m_cRefs == 0); // must have 0 ref count
  110. if (m_szASPCode)
  111. free(m_szASPCode);
  112. if (m_szSource)
  113. free(m_szSource);
  114. if (m_szFileName)
  115. free(m_szFileName);
  116. if (m_szDescription)
  117. free(m_szDescription);
  118. if (m_szASPDescription)
  119. free(m_szASPDescription);
  120. if (m_bstrLineText)
  121. SysFreeString(m_bstrLineText);
  122. }
  123. /*===================================================================
  124. CASPError::ToBSTR
  125. Produce a BSTR to be returned by get_XXX methods
  126. Parameters:
  127. sz return this string as BSTR
  128. Returns:
  129. BSTR or NULL if FAILED
  130. ===================================================================*/
  131. BSTR CASPError::ToBSTR(CHAR *sz)
  132. {
  133. BSTR bstr;
  134. if (sz == NULL || *sz == '\0')
  135. bstr = SysAllocString(L"");
  136. else if (FAILED(SysAllocStringFromSz(sz, 0, &bstr)))
  137. bstr = NULL;
  138. return bstr;
  139. }
  140. /*===================================================================
  141. CASPError::QueryInterface
  142. CASPError::AddRef
  143. CASPError::Release
  144. IUnknown members for CASPError object.
  145. ===================================================================*/
  146. STDMETHODIMP CASPError::QueryInterface(REFIID riid, VOID **ppv)
  147. {
  148. if (IID_IUnknown == riid || IID_IDispatch == riid || IID_IASPError == riid)
  149. {
  150. AddRef();
  151. *ppv = this;
  152. return S_OK;
  153. }
  154. *ppv = NULL;
  155. return E_NOINTERFACE;
  156. }
  157. STDMETHODIMP_(ULONG) CASPError::AddRef()
  158. {
  159. return InterlockedIncrement(&m_cRefs);
  160. }
  161. STDMETHODIMP_(ULONG) CASPError::Release()
  162. {
  163. LONG cRefs = InterlockedDecrement(&m_cRefs);
  164. if (cRefs)
  165. return cRefs;
  166. delete this;
  167. return 0;
  168. }
  169. /*===================================================================
  170. CASPError::get_ASPCode
  171. CASPError::get_Number
  172. CASPError::get_Source
  173. CASPError::get_FileName
  174. CASPError::get_LineNumber
  175. CASPError::get_Description
  176. CASPError::get_ASPDescription
  177. IASPError members for CASPError object.
  178. ===================================================================*/
  179. STDMETHODIMP CASPError::get_ASPCode(BSTR *pbstr)
  180. {
  181. *pbstr = ToBSTR(m_szASPCode);
  182. return (*pbstr) ? S_OK : E_OUTOFMEMORY;
  183. }
  184. STDMETHODIMP CASPError::get_Number(long *plNumber)
  185. {
  186. *plNumber = m_lNumber;
  187. return S_OK;
  188. }
  189. STDMETHODIMP CASPError::get_Category(BSTR *pbstr)
  190. {
  191. *pbstr = ToBSTR(m_szSource);
  192. return (*pbstr) ? S_OK : E_OUTOFMEMORY;
  193. }
  194. STDMETHODIMP CASPError::get_File(BSTR *pbstr)
  195. {
  196. *pbstr = ToBSTR(m_szFileName);
  197. return (*pbstr) ? S_OK : E_OUTOFMEMORY;
  198. }
  199. STDMETHODIMP CASPError::get_Line(long *plLineNumber)
  200. {
  201. *plLineNumber = m_lLineNumber;
  202. return S_OK;
  203. }
  204. STDMETHODIMP CASPError::get_Column(long *pnColumn)
  205. {
  206. *pnColumn = long(m_nColumn);
  207. return S_OK;
  208. }
  209. STDMETHODIMP CASPError::get_Source(BSTR *pbstrLineText)
  210. {
  211. *pbstrLineText = SysAllocString(m_bstrLineText? m_bstrLineText : L"");
  212. return (*pbstrLineText) ? S_OK : E_OUTOFMEMORY;
  213. }
  214. STDMETHODIMP CASPError::get_Description(BSTR *pbstr)
  215. {
  216. *pbstr = ToBSTR(m_szDescription);
  217. return (*pbstr) ? S_OK : E_OUTOFMEMORY;
  218. }
  219. STDMETHODIMP CASPError::get_ASPDescription(BSTR *pbstr)
  220. {
  221. *pbstr = ToBSTR(m_szASPDescription);
  222. return (*pbstr) ? S_OK : E_OUTOFMEMORY;
  223. }