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.

346 lines
9.7 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) Microsoft Corporation
  4. //
  5. // SYNOPSIS
  6. //
  7. // Defines the class ReportEventCommand.
  8. //
  9. ///////////////////////////////////////////////////////////////////////////////
  10. #include "ias.h"
  11. #include "reporteventcmd.h"
  12. #include "msdasc.h"
  13. HRESULT ReportEventCommand::Prepare(IDBCreateSession* dbCreateSession) throw ()
  14. {
  15. HRESULT hr;
  16. CComPtr<IDBCreateCommand> dbCreateCommand;
  17. hr = dbCreateSession->CreateSession(
  18. 0,
  19. __uuidof(IDBCreateCommand),
  20. reinterpret_cast<IUnknown**>(&dbCreateCommand)
  21. );
  22. if (FAILED(hr))
  23. {
  24. TraceOleDbError("IDBCreateSession::CreateSession", hr);
  25. return hr;
  26. }
  27. CComPtr<ICommandText> commandText;
  28. hr = dbCreateCommand->CreateCommand(
  29. 0,
  30. __uuidof(ICommandText),
  31. reinterpret_cast<IUnknown**>(&commandText)
  32. );
  33. if (FAILED(hr))
  34. {
  35. TraceOleDbError("IDBCreateCommand::CreateCommand", hr);
  36. return hr;
  37. }
  38. hr = commandText->SetCommandText(
  39. DBGUID_SQL,
  40. L"{rpc dbo.report_event}"
  41. );
  42. if (FAILED(hr))
  43. {
  44. TraceOleDbError("ICommandText::SetCommandText", hr);
  45. return hr;
  46. }
  47. CComPtr<ICommandWithParameters> commandWithParameters;
  48. hr = commandText->QueryInterface(
  49. __uuidof(ICommandWithParameters),
  50. reinterpret_cast<void**>(&commandWithParameters)
  51. );
  52. if (FAILED(hr))
  53. {
  54. TraceComError("IUnknown::QueryInterface(ICommandWithParameters", hr);
  55. return hr;
  56. }
  57. // When using RPC call semantics, the stored procedure always has a return
  58. // value even if it's not declared. Thus, we bind and ignore.
  59. static const DB_UPARAMS paramOrdinal[] =
  60. {
  61. 1,
  62. 2
  63. };
  64. static const DBPARAMBINDINFO dbParamBindInfo[] =
  65. {
  66. {
  67. L"int", // pwszDataSourceType
  68. L"return_value", // pwszName
  69. sizeof(long), // ulParamSize
  70. DBPARAMFLAGS_ISOUTPUT, // dwFlags
  71. 0, // bPrecision
  72. 0 // bScale
  73. },
  74. {
  75. L"ntext", // pwszDataSourceType
  76. L"@doc", // pwszName
  77. ~0, // ulParamSize
  78. DBPARAMFLAGS_ISINPUT, // dwFlags
  79. 0, // bPrecision
  80. 0 // bScale
  81. }
  82. };
  83. hr = commandWithParameters->SetParameterInfo(
  84. 2,
  85. paramOrdinal,
  86. dbParamBindInfo
  87. );
  88. if (FAILED(hr))
  89. {
  90. TraceOleDbError("ICommandWithParameters::SetParameterInfo", hr);
  91. return hr;
  92. }
  93. CComPtr<IAccessor> accessor;
  94. hr = commandText->QueryInterface(
  95. __uuidof(IAccessor),
  96. reinterpret_cast<void**>(&accessor)
  97. );
  98. if (FAILED(hr))
  99. {
  100. TraceComError("IUnknown::QueryInterface(IAccessor)", hr);
  101. return hr;
  102. }
  103. static const DBBINDING dbBinding[] =
  104. {
  105. {
  106. 1, // iOrdinal
  107. 0, // obValue
  108. 0, // obLength
  109. 0, // obStatus
  110. 0, // pTypeInfo
  111. 0, // pObject
  112. 0, // pBindExt
  113. DBPART_VALUE, // dwPart
  114. DBMEMOWNER_CLIENTOWNED, // dwMemOwner
  115. DBPARAMIO_OUTPUT, // eParamIO
  116. 0, // cbMaxLen
  117. 0, // dwFlags
  118. DBTYPE_I4, // wType
  119. 0, // bPrecision
  120. 0 // bScale
  121. },
  122. {
  123. 2, // iOrdinal
  124. offsetof(SprocParams, doc), // obValue
  125. 0, // obLength
  126. 0, // obStatus
  127. 0, // pTypeInfo
  128. 0, // pObject
  129. 0, // pBindExt
  130. DBPART_VALUE, // dwPart
  131. DBMEMOWNER_CLIENTOWNED, // dwMemOwner
  132. DBPARAMIO_INPUT, // eParamIO
  133. 0, // cbMaxLen
  134. 0, // dwFlags
  135. (DBTYPE_WSTR | DBTYPE_BYREF), // wType
  136. 0, // bPrecision
  137. 0 // bScale
  138. }
  139. };
  140. HACCESSOR h;
  141. hr = accessor->CreateAccessor(
  142. DBACCESSOR_PARAMETERDATA,
  143. 2,
  144. dbBinding,
  145. sizeof(SprocParams),
  146. &h,
  147. 0
  148. );
  149. if (FAILED(hr))
  150. {
  151. TraceOleDbError("IAccessor::CreateAccessor", hr);
  152. return hr;
  153. }
  154. // Everything succeeded, so release the old resources ...
  155. ReleaseAccessorHandle();
  156. // ... and store the new.
  157. command = commandText;
  158. accessorManager = accessor;
  159. accessorHandle = h;
  160. return S_OK;
  161. }
  162. HRESULT ReportEventCommand::Execute(const wchar_t* doc) throw ()
  163. {
  164. SprocParams data =
  165. {
  166. 0,
  167. doc
  168. };
  169. DBPARAMS dbParmas =
  170. {
  171. &data, // pData
  172. 1, // cParamSets
  173. accessorHandle // hAccessor
  174. };
  175. HRESULT hr = command->Execute(
  176. 0,
  177. IID_NULL,
  178. &dbParmas,
  179. 0,
  180. 0
  181. );
  182. if (FAILED(hr))
  183. {
  184. TraceOleDbError("ICommand::Execute", hr);
  185. }
  186. return hr;
  187. }
  188. void ReportEventCommand::Unprepare() throw ()
  189. {
  190. ReleaseAccessorHandle();
  191. accessorManager.Release();
  192. command.Release();
  193. version = 0;
  194. }
  195. HRESULT ReportEventCommand::CreateDataSource(
  196. const wchar_t* initString,
  197. IDBCreateSession** newDataSource
  198. ) throw ()
  199. {
  200. HRESULT hr;
  201. CComPtr<IDataInitialize> dataInitialize;
  202. hr = CoCreateInstance(
  203. CLSID_MSDAINITIALIZE,
  204. 0,
  205. CLSCTX_INPROC_SERVER,
  206. __uuidof(IDataInitialize),
  207. reinterpret_cast<void**>(&dataInitialize)
  208. );
  209. if (FAILED(hr))
  210. {
  211. TraceComError("CoCreateInstance(MSDAINITIALIZE)", hr);
  212. return hr;
  213. }
  214. CComPtr<IDBInitialize> dbInitialize;
  215. hr = dataInitialize->GetDataSource(
  216. 0,
  217. CLSCTX_INPROC_SERVER,
  218. initString,
  219. __uuidof(IDBInitialize),
  220. reinterpret_cast<IUnknown**>(&dbInitialize)
  221. );
  222. if (FAILED(hr))
  223. {
  224. TraceOleDbError("IDataInitialize::GetDataSource", hr);
  225. return hr;
  226. }
  227. hr = dbInitialize->Initialize();
  228. if (FAILED(hr))
  229. {
  230. TraceOleDbError("IDBInitialize::Initialize", hr);
  231. return hr;
  232. }
  233. hr = dbInitialize->QueryInterface(
  234. __uuidof(IDBCreateSession),
  235. reinterpret_cast<void**>(newDataSource)
  236. );
  237. if (FAILED(hr))
  238. {
  239. TraceComError("IUnknown::QueryInterface(IDBCreateSession)", hr);
  240. return hr;
  241. }
  242. return S_OK;
  243. }
  244. void ReportEventCommand::ReleaseAccessorHandle() throw ()
  245. {
  246. if ((accessorHandle != 0) && accessorManager)
  247. {
  248. accessorManager->ReleaseAccessor(accessorHandle, 0);
  249. }
  250. accessorHandle = 0;
  251. }
  252. void ReportEventCommand::TraceComError(
  253. const char* function,
  254. HRESULT error
  255. ) throw ()
  256. {
  257. IASTracePrintf("%s failed; return value = 0x%08X", function, error);
  258. }
  259. void ReportEventCommand::TraceOleDbError(
  260. const char* function,
  261. HRESULT error
  262. ) throw ()
  263. {
  264. IASTracePrintf("%s failed; return value = 0x%08X", function, error);
  265. IErrorInfo* errInfo;
  266. if (GetErrorInfo(0, &errInfo) == S_OK)
  267. {
  268. HRESULT hr;
  269. BSTR description;
  270. hr = errInfo->GetDescription(&description);
  271. if (SUCCEEDED(hr))
  272. {
  273. IASTracePrintf("\tDescription: %S", description);
  274. SysFreeString(description);
  275. }
  276. IErrorRecords* errRecords;
  277. hr = errInfo->QueryInterface(
  278. __uuidof(IErrorRecords),
  279. reinterpret_cast<void**>(&errRecords)
  280. );
  281. if (SUCCEEDED(hr))
  282. {
  283. ULONG numRecords = 0;
  284. errRecords->GetRecordCount(&numRecords);
  285. for (ULONG i = 0; i < numRecords; ++i)
  286. {
  287. ERRORINFO info;
  288. hr = errRecords->GetBasicErrorInfo(i, &info);
  289. if (SUCCEEDED(hr))
  290. {
  291. IASTracePrintf(
  292. "\tRecord %lu: hrError = 0x%08X; dwMinor = 0x%08X",
  293. i,
  294. info.hrError,
  295. info.dwMinor
  296. );
  297. }
  298. }
  299. errRecords->Release();
  300. }
  301. errInfo->Release();
  302. }
  303. }