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.

343 lines
7.7 KiB

  1. // RARegSetting.cpp : Implementation of CRARegSetting
  2. #include "stdafx.h"
  3. #include "RAssistance.h"
  4. #include "common.h"
  5. #include "RAEventLog.h"
  6. #include "assert.h"
  7. extern "C" void
  8. AttachDebuggerIfAsked(HINSTANCE hInst);
  9. extern HINSTANCE g_hInst;
  10. HRESULT
  11. CRAEventLog::LogRemoteAssistanceEvent(
  12. IN long ulEventType,
  13. IN long ulEventCode,
  14. IN long numStrings,
  15. IN LPCTSTR* pszStrings
  16. )
  17. /*++
  18. Description:
  19. Log a Salem related event, this is invoked by TermSrv and rdshost to log
  20. event related to help assistant connection.
  21. Parameters:
  22. Returns:
  23. S_OK or error code.
  24. --*/
  25. {
  26. HANDLE hAppLog=NULL;
  27. BOOL bSuccess=FALSE;
  28. try {
  29. if(hAppLog=RegisterEventSource(NULL, REMOTEASSISTANCE_EVENT_NAME))
  30. {
  31. bSuccess = ReportEvent(
  32. hAppLog,
  33. (WORD)ulEventType,
  34. 0,
  35. ulEventCode,
  36. NULL,
  37. (WORD)numStrings,
  38. 0,
  39. pszStrings,
  40. NULL
  41. );
  42. DeregisterEventSource(hAppLog);
  43. }
  44. }
  45. catch(...) {
  46. }
  47. return S_OK;
  48. }
  49. STDMETHODIMP
  50. CRAEventLog::LogRemoteAssistanceEvent(
  51. /*[in]*/ long ulEventType,
  52. /*[in]*/ long ulEventCode,
  53. /*[in]*/ VARIANT* pEventStrings
  54. )
  55. /*++
  56. --*/
  57. {
  58. HRESULT hRes = S_OK;
  59. BSTR* bstrArray = NULL;
  60. SAFEARRAY* psa = NULL;
  61. VARTYPE vt_type;
  62. DWORD dwNumStrings = 0;
  63. AttachDebuggerIfAsked( g_hInst );
  64. if( NULL == pEventStrings )
  65. {
  66. hRes = LogRemoteAssistanceEvent( ulEventType, ulEventCode );
  67. }
  68. else if( pEventStrings->vt == VT_DISPATCH )
  69. {
  70. // coming from JSCRIPT
  71. hRes = LogJScriptEventSource( ulEventType, ulEventCode, pEventStrings );
  72. }
  73. else
  74. {
  75. // we only support BSTR and safearry of BSTR
  76. if( (pEventStrings->vt != VT_BSTR) && (pEventStrings->vt != (VT_ARRAY | VT_BSTR)) )
  77. {
  78. hRes = E_INVALIDARG;
  79. goto CLEANUPANDEXIT;
  80. }
  81. //
  82. // we are dealing with multiple BSTRs
  83. if( pEventStrings->vt & VT_ARRAY )
  84. {
  85. psa = pEventStrings->parray;
  86. // only accept 1 dim.
  87. if( 1 != SafeArrayGetDim(psa) )
  88. {
  89. hRes = E_INVALIDARG;
  90. goto CLEANUPANDEXIT;
  91. }
  92. // only accept BSTR as input type.
  93. hRes = SafeArrayGetVartype( psa, &vt_type );
  94. if( FAILED(hRes) )
  95. {
  96. goto CLEANUPANDEXIT;
  97. }
  98. if( VT_BSTR != vt_type )
  99. {
  100. hRes = E_INVALIDARG;
  101. goto CLEANUPANDEXIT;
  102. }
  103. hRes = SafeArrayAccessData(psa, (void **)&bstrArray);
  104. if( FAILED(hRes) )
  105. {
  106. goto CLEANUPANDEXIT;
  107. }
  108. hRes = LogRemoteAssistanceEvent(
  109. ulEventType,
  110. ulEventCode,
  111. psa->rgsabound->cElements,
  112. (LPCTSTR *)bstrArray
  113. );
  114. SafeArrayUnaccessData(psa);
  115. }
  116. else
  117. {
  118. hRes = LogRemoteAssistanceEvent(
  119. ulEventType,
  120. ulEventCode,
  121. 1,
  122. (LPCTSTR *)&(pEventStrings->bstrVal)
  123. );
  124. }
  125. }
  126. CLEANUPANDEXIT:
  127. return hRes;
  128. }
  129. HRESULT
  130. CRAEventLog::GetProperty(IDispatch* pDisp, BSTR szProperty, VARIANT * pVarRet)
  131. {
  132. HRESULT hr = S_OK;
  133. DISPID pDispId;
  134. DISPPARAMS dp;
  135. if ((pVarRet == NULL) ||
  136. (szProperty == NULL) ||
  137. (pDisp == NULL))
  138. {
  139. hr = E_INVALIDARG;
  140. goto done;
  141. }
  142. memset(&dp,0,sizeof(DISPPARAMS));
  143. // Get the DispID of the property
  144. hr = pDisp->GetIDsOfNames(IID_NULL, &szProperty, 1, LOCALE_SYSTEM_DEFAULT, &pDispId);
  145. if (FAILED(hr))
  146. {
  147. goto done;
  148. }
  149. hr = pDisp->Invoke(pDispId, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dp, pVarRet, NULL, NULL);
  150. if (FAILED(hr))
  151. {
  152. goto done;
  153. }
  154. done:
  155. return hr;
  156. }
  157. HRESULT
  158. CRAEventLog::GetArrayValue(IDispatch * pDisp, LONG index, VARIANT * pVarRet)
  159. {
  160. HRESULT hr = S_OK;
  161. VARIANT vtLength;
  162. LONG lArrayLen;
  163. CComBSTR bstrIndex;
  164. WCHAR wbuff[100];
  165. CComBSTR bstrTemp(OLESTR("length"));
  166. VariantInit( &vtLength );
  167. if (bstrTemp.m_str == NULL) {
  168. hr = E_OUTOFMEMORY;
  169. goto done;
  170. }
  171. if ((pVarRet == NULL) ||
  172. (pDisp == NULL))
  173. {
  174. hr = E_INVALIDARG;
  175. goto done;
  176. }
  177. // Get the length of the array
  178. hr = GetProperty(pDisp, bstrTemp, &vtLength);
  179. if (FAILED(hr))
  180. {
  181. goto done;
  182. }
  183. // if the length is < index, return E_INVALIDARG
  184. if (vtLength.vt != VT_I4)
  185. {
  186. hr = E_FAIL;
  187. goto done;
  188. }
  189. lArrayLen = vtLength.lVal;
  190. if (index > (lArrayLen - 1))
  191. {
  192. hr = E_INVALIDARG;
  193. goto done;
  194. }
  195. // Get the VARIANT at the index of the array
  196. wsprintf(&wbuff[0],L"%ld", index);
  197. bstrIndex.Append(wbuff);
  198. hr = GetProperty(pDisp, bstrIndex, pVarRet);
  199. if (FAILED(hr))
  200. {
  201. goto done;
  202. }
  203. done:
  204. VariantClear( &vtLength );
  205. return hr;
  206. }
  207. //
  208. // We limit max. number of 256 parameters into event log.
  209. //
  210. #define EVENTSTRING_MAX_PARMS 256
  211. HRESULT
  212. CRAEventLog::LogJScriptEventSource(
  213. IN long ulEventType,
  214. IN long ulEventCode,
  215. IN VARIANT *pVar
  216. )
  217. /*++
  218. Code modify from jperez
  219. --*/
  220. {
  221. HRESULT hr = S_OK;
  222. CComPtr<IDispatch> pDisp;
  223. VARIANT varValue;
  224. BSTR* pEventStrings = NULL;
  225. DWORD dwNumStrings = 0;
  226. if (pVar->vt != VT_DISPATCH || pVar->pdispVal == NULL)
  227. {
  228. hr = E_INVALIDARG;
  229. goto done;
  230. }
  231. pEventStrings = (BSTR*) LocalAlloc(LPTR, sizeof(BSTR) * EVENTSTRING_MAX_PARMS);
  232. if( NULL == pEventStrings )
  233. {
  234. hr = E_OUTOFMEMORY;
  235. goto done;
  236. }
  237. pDisp = pVar->pdispVal;
  238. for(; hr == S_OK && dwNumStrings < EVENTSTRING_MAX_PARMS; dwNumStrings++)
  239. {
  240. VariantInit( &varValue );
  241. hr = GetArrayValue(pDisp, dwNumStrings, &varValue);
  242. if( FAILED(hr) )
  243. {
  244. // GetArrayValue return 0x80070057 when it reach the end; however
  245. // lot of place returns this value so we should assume end of
  246. // msg parameter and log what we have.
  247. hr = S_FALSE;
  248. break;
  249. }
  250. if( varValue.vt != VT_BSTR )
  251. {
  252. // unsupported type, return error.
  253. hr = E_INVALIDARG;
  254. break;
  255. }
  256. pEventStrings[dwNumStrings] = varValue.bstrVal;
  257. // we take ownership of BSTR, clear out necessary field.
  258. varValue.bstrVal = NULL;
  259. varValue.vt = VT_EMPTY;
  260. }
  261. if( hr == S_FALSE )
  262. {
  263. hr = LogRemoteAssistanceEvent(
  264. ulEventType,
  265. ulEventCode,
  266. dwNumStrings,
  267. (LPCTSTR *)pEventStrings
  268. );
  269. }
  270. done:
  271. if( NULL != pEventStrings )
  272. {
  273. for(DWORD index=0; index < dwNumStrings; index++)
  274. {
  275. SysFreeString(pEventStrings[index]);
  276. }
  277. LocalFree( pEventStrings );
  278. }
  279. return hr;
  280. }