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.

513 lines
15 KiB

  1. /*++
  2. 1998 Seagate Software, Inc. All rights reserved.
  3. Module Name:
  4. DataObj.cpp
  5. Abstract:
  6. Implementation of IDataObject interface, which is supported
  7. by the CBaseHsm COM object.
  8. Author:
  9. Rohde Wakefield [rohde] 19-Aug-1997
  10. Revision History:
  11. --*/
  12. #include "stdafx.h"
  13. // Declare Snap-in NodeType formats:
  14. // - GUID format
  15. // - string GUID format
  16. // - display name format.
  17. // - internal format.
  18. UINT CSakNode::m_cfNodeType = RegisterClipboardFormat(CCF_NODETYPE);
  19. UINT CSakNode::m_cfNodeTypeString = RegisterClipboardFormat(CCF_SZNODETYPE);
  20. UINT CSakNode::m_cfDisplayName = RegisterClipboardFormat(CCF_DISPLAY_NAME);
  21. UINT CSakNode::m_cfInternal = RegisterClipboardFormat(SAKSNAP_INTERNAL);
  22. UINT CSakNode::m_cfClassId = RegisterClipboardFormat(CCF_SNAPIN_CLASSID);
  23. UINT CSakNode::m_cfComputerName = RegisterClipboardFormat(MMC_SNAPIN_MACHINE_NAME);
  24. UINT CSakNode::m_cfEventLogViews = RegisterClipboardFormat(CF_EV_VIEWS);
  25. HRESULT
  26. CSakNode::GetDataGeneric(
  27. IN LPFORMATETC lpFormatetcIn,
  28. IN OUT LPSTGMEDIUM lpMedium,
  29. IN BOOL DoAlloc
  30. )
  31. /*++
  32. Routine Description:
  33. Retrieve information FROM the dataobject and put INTO lpMedium.
  34. Arguments:
  35. lpFormatetc - Format to retreive.
  36. lpMedium - Storage to put information into.
  37. Return Value:
  38. S_OK - Storage filled in.
  39. E_xxxxxxxxxxx - Failure occurred.
  40. --*/
  41. {
  42. WsbTraceIn( L"CSakNode::GetDataGeneric", L"lpFormatetc->cfFormat = <%ls>", RsClipFormatAsString( lpFormatetcIn->cfFormat ), WsbBoolAsString( DoAlloc ) );
  43. HRESULT hr = DV_E_CLIPFORMAT;
  44. try {
  45. WsbAffirmPointer( lpMedium );
  46. if( DoAlloc ) {
  47. lpMedium->hGlobal = 0;
  48. lpMedium->tymed = TYMED_HGLOBAL;
  49. } else {
  50. WsbAffirm( TYMED_HGLOBAL == lpMedium->tymed, DV_E_TYMED );
  51. WsbAffirmPointer( lpMedium->hGlobal );
  52. }
  53. // Based on the CLIPFORMAT write data to "lpMediam" in the correct format.
  54. const CLIPFORMAT cf = lpFormatetcIn->cfFormat;
  55. // clip format is the GUID node type
  56. if(cf == m_cfNodeType) {
  57. hr = RetrieveNodeTypeData(lpMedium);
  58. // clip format is the string "spelling" of the GUID node type
  59. } else if(cf == m_cfNodeTypeString) {
  60. hr = RetrieveNodeTypeStringData(lpMedium);
  61. // clip format is the computer represented
  62. } else if (cf == m_cfComputerName) {
  63. hr = RetrieveComputerName(lpMedium);
  64. // clip format is the event viewer setup
  65. } else if (cf == m_cfEventLogViews) {
  66. hr = RetrieveEventLogViews(lpMedium);
  67. // clip format is the display name of the node
  68. } else if (cf == m_cfDisplayName) {
  69. hr = RetrieveDisplayName(lpMedium);
  70. // clip format is the ClassId
  71. } else if( cf == m_cfClassId ) {
  72. hr = RetrieveClsid( lpMedium );
  73. // clip format is an INTERNAL format
  74. } else if (cf == m_cfInternal) {
  75. hr = RetrieveInternal(lpMedium);
  76. } else {
  77. hr = DV_E_CLIPFORMAT;
  78. }
  79. } WsbCatch( hr );
  80. WsbTraceOut( L"CSakNode::GetDataGeneric", L"hr = <%ls>", WsbHrAsString( hr ) );
  81. return( hr );
  82. }
  83. STDMETHODIMP
  84. CSakNode::GetData(
  85. IN LPFORMATETC lpFormatetcIn,
  86. OUT LPSTGMEDIUM lpMedium
  87. )
  88. /*++
  89. Routine Description:
  90. Retrieve information FROM the dataobject and put INTO lpMedium.
  91. Storage allocated and returned.
  92. Arguments:
  93. lpFormatetc - Format to retreive.
  94. lpMedium - Storage to put information into.
  95. Return Value:
  96. S_OK - Storage filled in.
  97. E_xxxxxxxxxxx - Failure occurred.
  98. --*/
  99. {
  100. WsbTraceIn( L"CSakNode::GetData", L"lpFormatetc->cfFormat = <%ls>", RsClipFormatAsString( lpFormatetcIn->cfFormat ) );
  101. HRESULT hr = S_OK;
  102. hr = GetDataGeneric( lpFormatetcIn, lpMedium, TRUE );
  103. WsbTraceOut( L"CSakNode::GetData", L"hr = <%ls>", WsbHrAsString( hr ) );
  104. return( hr );
  105. }
  106. STDMETHODIMP
  107. CSakNode::GetDataHere(
  108. IN LPFORMATETC lpFormatetc,
  109. IN OUT LPSTGMEDIUM lpMedium
  110. )
  111. /*++
  112. Routine Description:
  113. Retrieve information FROM the dataobject and put INTO lpMedium.
  114. Arguments:
  115. lpFormatetc - Format to retreive.
  116. lpMedium - Storage to put information into.
  117. Return Value:
  118. S_OK - Storage filled in.
  119. E_xxxxxxxxxxx - Failure occurred.
  120. --*/
  121. {
  122. WsbTraceIn( L"CSakNode::GetDataHere", L"lpFormatetc->cfFormat = <%ls>", RsClipFormatAsString( lpFormatetc->cfFormat ) );
  123. HRESULT hr = S_OK;
  124. hr = GetDataGeneric( lpFormatetc, lpMedium, FALSE );
  125. WsbTraceOut( L"CSakNode::GetDataHere", L"hr = <%ls>", WsbHrAsString( hr ) );
  126. return( hr );
  127. }
  128. STDMETHODIMP
  129. CSakNode::SetData(
  130. LPFORMATETC lpFormatetc,
  131. LPSTGMEDIUM lpMedium,
  132. BOOL /*fRelease*/
  133. )
  134. /*++
  135. Routine Description:
  136. Put data INTO a dataobject FROM the information in the lpMedium.
  137. We do not allow any data to be set.
  138. Arguments:
  139. lpFormatetc - Format to set.
  140. lpMedium - Storage to get information from.
  141. fRelease - Indicates who owns storage after call.
  142. Return Value:
  143. S_OK - Storage retreived.
  144. E_xxxxxxxxxxx - Failure occurred.
  145. --*/
  146. {
  147. WsbTraceIn( L"CSakNode::SetData", L"lpFormatetc->cfFormat = <%ls>", RsClipFormatAsString( lpFormatetc->cfFormat ) );
  148. HRESULT hr = DV_E_CLIPFORMAT;
  149. // Based on the CLIPFORMAT write data to "lpMediam" in the correct format.
  150. const CLIPFORMAT cf = lpFormatetc->cfFormat;
  151. //clip format is an INTERNAL format
  152. if( cf == m_cfInternal ) {
  153. hr = StoreInternal( lpMedium );
  154. }
  155. WsbTraceOut( L"CSakNode::SetData", L"hr = <%ls>", WsbHrAsString( hr ) );
  156. return( hr );
  157. }
  158. ///////////////////////////////////////////////////////////////////////
  159. // Note - CSakNode does not implement these
  160. ///////////////////////////////////////////////////////////////////////
  161. STDMETHODIMP CSakNode::EnumFormatEtc(DWORD /*dwDirection*/, LPENUMFORMATETC* /*ppEnumFormatEtc*/)
  162. {
  163. WsbTraceIn( L"CSakNode::EnumFormatEtc", L"" );
  164. HRESULT hr = E_NOTIMPL;
  165. WsbTraceOut( L"CSakNode::EnumFormatEtc", L"hr = <%ls>", WsbHrAsString( hr ) );
  166. return( hr );
  167. }
  168. // Retrieve from a dataobject with the NodeType (GUID) data in it.
  169. HRESULT CSakNode::RetrieveNodeTypeData(LPSTGMEDIUM lpMedium)
  170. {
  171. return Retrieve((const void*)(m_rTypeGuid), sizeof(GUID), lpMedium);
  172. }
  173. // Retrieve from a dataobject with the node type object in GUID string format
  174. HRESULT CSakNode::RetrieveNodeTypeStringData(LPSTGMEDIUM lpMedium)
  175. {
  176. CWsbStringPtr guidString = *m_rTypeGuid;
  177. return Retrieve(guidString, ((wcslen(guidString)+1) * sizeof(wchar_t)), lpMedium);
  178. }
  179. // Retrieve from a dataobject with the display named used in the scope pane
  180. HRESULT CSakNode::RetrieveDisplayName(LPSTGMEDIUM lpMedium)
  181. {
  182. // Load the name the data object
  183. return Retrieve(m_szName, ((wcslen(m_szName)+1) * sizeof(wchar_t)), lpMedium);
  184. }
  185. // Retrieve from a dataobject with the CLSID data in it.
  186. HRESULT CSakNode::RetrieveClsid(LPSTGMEDIUM lpMedium)
  187. {
  188. // zzzz
  189. return Retrieve( (const void*)(&CLSID_HsmAdminDataSnapin), sizeof(CLSID), lpMedium );
  190. }
  191. // Retrieve INTERNAL data from the dataobject's m_internal member INTO the lpMedium
  192. HRESULT CSakNode::RetrieveInternal(LPSTGMEDIUM lpMedium)
  193. {
  194. return Retrieve(&m_internal, sizeof(INTERNAL), lpMedium);
  195. }
  196. // Retrieve data from the dataobject's hsm name
  197. HRESULT CSakNode::RetrieveComputerName(LPSTGMEDIUM lpMedium)
  198. {
  199. HRESULT hr = S_OK;
  200. try {
  201. CWsbStringPtr computerName;
  202. HRESULT hrTemp = m_pSakSnapAsk->GetHsmName( &computerName );
  203. WsbAffirmHr( hrTemp );
  204. if( S_FALSE == hrTemp ) {
  205. computerName = L"";
  206. }
  207. WsbAffirmHr(
  208. Retrieve(
  209. (WCHAR*)computerName,
  210. ( wcslen( computerName ) + 1 ) * sizeof(WCHAR),
  211. lpMedium ) );
  212. } WsbCatch( hr );
  213. return( hr );
  214. }
  215. // Retrieve event setup info
  216. HRESULT CSakNode::RetrieveEventLogViews(LPSTGMEDIUM lpMedium)
  217. {
  218. HRESULT hr = S_OK;
  219. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  220. try {
  221. WsbAffirmPointer( lpMedium );
  222. ULONG strLength = 0;
  223. CWsbStringPtr hsmName;
  224. CString appName, sysName;
  225. CString nullString;
  226. CString appPath, sysPath;
  227. appName.LoadString( IDS_EVENT_LOG_APP_TITLE );
  228. sysName.LoadString( IDS_EVENT_LOG_SYS_TITLE );
  229. nullString = L"";
  230. HRESULT hrTemp = m_pSakSnapAsk->GetHsmName( &hsmName );
  231. WsbAffirmHr( hrTemp );
  232. if( S_FALSE == hrTemp ) {
  233. hsmName = L"";
  234. appPath = L"";
  235. sysPath = L"";
  236. } else {
  237. CString configPath = L"\\\\";
  238. configPath += hsmName;
  239. configPath += L"\\Admin$\\System32\\config\\";
  240. appPath = configPath;
  241. sysPath = configPath;
  242. appPath += L"AppEvent.Evt";
  243. sysPath += L"SysEvent.Evt";
  244. }
  245. #define ADD_TYPE(data, type) \
  246. {type x = data; \
  247. WsbAffirmHr( spStm->Write(&x, sizeof(type), NULL) ) };
  248. #define ADD_USHORT(us) ADD_TYPE(us, USHORT)
  249. #define ADD_BOOL(b) ADD_TYPE(b, BOOL)
  250. #define ADD_ULONG(ul) ADD_TYPE(ul, ULONG)
  251. #define ADD_STRING(str) \
  252. {strLength = (ULONG)wcslen((LPCWSTR)(str)) + 1; \
  253. ADD_USHORT((USHORT)strLength); \
  254. WsbAffirmHr( spStm->Write(str, strLength * sizeof(WCHAR), NULL) ) };
  255. CComPtr<IStream> spStm;
  256. WsbAffirmHr( CreateStreamOnHGlobal(NULL, FALSE, &spStm) );
  257. //
  258. // Add header info
  259. //
  260. ADD_BOOL( TRUE ); // fOnlyTheseViews
  261. ADD_USHORT( 2 ); // cViews
  262. //
  263. // Add application log filtered for our services
  264. //
  265. ADD_ULONG( ELT_APPLICATION ); // Type;
  266. ADD_USHORT( VIEWINFO_FILTERED |
  267. LOGINFO_DONT_PERSIST); // flViewFlags
  268. ADD_STRING( hsmName ); // ServerName
  269. PCWSTR pwz = L"Application";
  270. ADD_STRING( pwz ); // SourceName
  271. ADD_STRING( appPath ); // FileName
  272. ADD_STRING( appName ); // DisplayName
  273. ADD_ULONG( EVENTLOG_ALL_EVENTS ); // flRecType (could filter warning, error, etc.)
  274. ADD_USHORT( 0 ); // usCategory
  275. ADD_BOOL( FALSE ); // fEventID
  276. ADD_ULONG( 0 ); // ulEventID
  277. ADD_STRING( WSB_LOG_SOURCE_NAME ); // szSourceName
  278. ADD_STRING( nullString ); // szUser
  279. ADD_STRING( hsmName ); // szComputer
  280. ADD_ULONG( 0 ); // ulFrom
  281. ADD_ULONG( 0 ); // ulTo
  282. //
  283. // Add system log filtered for our device
  284. //
  285. ADD_ULONG( ELT_SYSTEM ); // Type;
  286. ADD_USHORT( VIEWINFO_FILTERED |
  287. LOGINFO_DONT_PERSIST); // flViewFlags
  288. ADD_STRING( hsmName ); // ServerName
  289. pwz = L"System";
  290. ADD_STRING( pwz ); // SourceName
  291. ADD_STRING( sysPath ); // FileName
  292. ADD_STRING( sysName ); // DisplayName
  293. ADD_ULONG( EVENTLOG_ALL_EVENTS ); // flRecType (could filter warning, error, etc.)
  294. ADD_USHORT( 0 ); // usCategory
  295. ADD_BOOL( FALSE ); // fEventID
  296. ADD_ULONG( 0 ); // ulEventID
  297. ADD_STRING( WSB_LOG_FILTER_NAME ); // szSourceName
  298. ADD_STRING( nullString ); // szUser
  299. ADD_STRING( hsmName ); // szComputer
  300. ADD_ULONG( 0 ); // ulFrom
  301. ADD_ULONG( 0 ); // ulTo
  302. HGLOBAL hMem = NULL;
  303. WsbAffirmHr( GetHGlobalFromStream(spStm, &hMem) );
  304. lpMedium->hGlobal = hMem; // StgMedium variables
  305. lpMedium->tymed = TYMED_HGLOBAL;
  306. lpMedium->pUnkForRelease = NULL;
  307. } WsbCatch( hr );
  308. return( hr );
  309. }
  310. // Store the INTERNAL data FROM the lpMedium->hGlobal INTO the dataobject's m_internal member
  311. HRESULT CSakNode::StoreInternal(LPSTGMEDIUM lpMedium)
  312. {
  313. return Store(&m_internal, sizeof(INTERNAL), lpMedium);
  314. }
  315. // Retrieve FROM a dataobject INTO a lpMedium. The data object can be one of
  316. // several types of data in it (nodetype, nodetype string, display name, or
  317. // INTERNAL data).
  318. // This function moves data from pBuffer to the lpMedium->hGlobal
  319. //
  320. HRESULT CSakNode::Retrieve(const void* pBuffer, DWORD len, LPSTGMEDIUM lpMedium)
  321. {
  322. HRESULT hr = S_OK;
  323. try {
  324. WsbAffirmPointer( pBuffer );
  325. WsbAffirmPointer( lpMedium );
  326. WsbAffirm( TYMED_HGLOBAL == lpMedium->tymed, DV_E_TYMED );
  327. //
  328. // Check to see if we need to allocate the global memory here
  329. //
  330. if( 0 == lpMedium->hGlobal ) {
  331. lpMedium->hGlobal = ::GlobalAlloc( GPTR, len );
  332. } else {
  333. WsbAffirm( GlobalSize( lpMedium->hGlobal ) >= (DWORD)len, STG_E_MEDIUMFULL );
  334. }
  335. WsbAffirmPointer( lpMedium->hGlobal );
  336. // Create the stream on the hGlobal passed in. When we write to the stream,
  337. // it simultaneously writes to the hGlobal the same information.
  338. LPSTREAM lpStream;
  339. WsbAffirmHr( CreateStreamOnHGlobal(lpMedium->hGlobal, FALSE, &lpStream ));
  340. // Write 'len' number of bytes from pBuffer into the stream. When we write
  341. // to the stream, it simultaneously writes to the global memory we
  342. // associated it with above.
  343. ULONG numBytesWritten;
  344. WsbAffirmHr( lpStream->Write(pBuffer, len, &numBytesWritten ));
  345. // Because we told CreateStreamOnHGlobal with 'FALSE', only the stream is released here.
  346. // Note - the caller (i.e. snap-in, object) will free the HGLOBAL
  347. // at the correct time. This is according to the IDataObject specification.
  348. lpStream->Release();
  349. } WsbCatch( hr );
  350. return hr;
  351. }
  352. // Store INTO a dataobject FROM an lpMedium. The data object can be one of
  353. // several types of data in it (nodetype, nodetype string, display name, or
  354. // INTERNAL data).
  355. // This function moves data INTO pBuffer FROM the lpMedium->hGlobal
  356. //
  357. HRESULT CSakNode::Store( void* pBuffer, DWORD len, LPSTGMEDIUM lpMedium )
  358. {
  359. HRESULT hr = S_OK;
  360. try {
  361. WsbAffirmPointer( pBuffer );
  362. WsbAffirmPointer( lpMedium );
  363. WsbAffirm( lpMedium->tymed == TYMED_HGLOBAL, E_INVALIDARG );
  364. // Use memcpy, because the lpStream->Read is failing to read any bytes.
  365. memcpy(pBuffer, &(lpMedium->hGlobal), len);
  366. } WsbCatch( hr );
  367. return hr;
  368. }