Source code of Windows XP (NT5)
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.

479 lines
12 KiB

  1. // Cookie.cpp : Implementation of CFileMgmtCookie and related classes
  2. #include "stdafx.h"
  3. #include "cookie.h"
  4. #include "stdutils.h" // g_aNodetypeGuids
  5. #include "cmponent.h"
  6. #include "atlimpl.cpp"
  7. DECLARE_INFOLEVEL(FileMgmtSnapin)
  8. #include "macros.h"
  9. USE_HANDLE_MACROS("FILEMGMT(cookie.cpp)")
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15. #include "stdcooki.cpp"
  16. //
  17. // This is used by the nodetype utility routines in stdutils.cpp
  18. //
  19. const struct NODETYPE_GUID_ARRAYSTRUCT g_NodetypeGuids[FILEMGMT_NUMTYPES] =
  20. {
  21. { // FILEMGMT_ROOT
  22. structuuidNodetypeRoot,
  23. lstruuidNodetypeRoot },
  24. { // FILEMGMT_SHARES
  25. structuuidNodetypeShares,
  26. lstruuidNodetypeShares },
  27. { // FILEMGMT_SESSIONS
  28. structuuidNodetypeSessions,
  29. lstruuidNodetypeSessions },
  30. { // FILEMGMT_RESOURCES
  31. structuuidNodetypeResources,
  32. lstruuidNodetypeResources },
  33. { // FILEMGMT_SERVICES
  34. structuuidNodetypeServices,
  35. lstruuidNodetypeServices },
  36. { // FILEMGMT_SHARE
  37. structuuidNodetypeShare,
  38. lstruuidNodetypeShare },
  39. { // FILEMGMT_SESSION
  40. structuuidNodetypeSession,
  41. lstruuidNodetypeSession },
  42. { // FILEMGMT_RESOURCE
  43. structuuidNodetypeResource,
  44. lstruuidNodetypeResource },
  45. { // FILEMGMT_SERVICE
  46. structuuidNodetypeService,
  47. lstruuidNodetypeService }
  48. };
  49. const struct NODETYPE_GUID_ARRAYSTRUCT* g_aNodetypeGuids = g_NodetypeGuids;
  50. const int g_cNumNodetypeGuids = FILEMGMT_NUMTYPES;
  51. //
  52. // CFileMgmtCookie
  53. //
  54. HRESULT CFileMgmtCookie::GetTransport( FILEMGMT_TRANSPORT* /*ptransport*/ )
  55. {
  56. return DV_E_FORMATETC;
  57. }
  58. HRESULT CFileMgmtCookie::GetShareName( OUT CString& /*strShareName*/ )
  59. {
  60. return DV_E_FORMATETC;
  61. }
  62. HRESULT CFileMgmtCookie::GetSharePIDList( OUT LPITEMIDLIST * /*ppidl*/ )
  63. {
  64. return DV_E_FORMATETC;
  65. }
  66. HRESULT CFileMgmtCookie::GetSessionClientName( OUT CString& /*strShareName*/ )
  67. {
  68. return DV_E_FORMATETC;
  69. }
  70. HRESULT CFileMgmtCookie::GetSessionUserName( OUT CString& /*strShareName*/ )
  71. {
  72. return DV_E_FORMATETC;
  73. }
  74. HRESULT CFileMgmtCookie::GetSessionID( DWORD* /*pdwFileID*/ )
  75. {
  76. return DV_E_FORMATETC;
  77. }
  78. HRESULT CFileMgmtCookie::GetFileID( DWORD* /*pdwFileID*/ )
  79. {
  80. return DV_E_FORMATETC;
  81. }
  82. HRESULT CFileMgmtCookie::GetServiceName( OUT CString& /*strServiceName*/ )
  83. {
  84. return DV_E_FORMATETC;
  85. }
  86. HRESULT CFileMgmtCookie::GetServiceDisplayName( OUT CString& /*strServiceDisplayName*/ )
  87. {
  88. return DV_E_FORMATETC;
  89. }
  90. HRESULT CFileMgmtCookie::GetExplorerViewDescription( OUT CString& /*strExplorerViewDescription*/ )
  91. {
  92. return DV_E_FORMATETC;
  93. }
  94. HRESULT CFileMgmtCookie::CompareSimilarCookies (
  95. CCookie* pOtherCookie,
  96. int* pnResult)
  97. {
  98. CFileMgmtCookie* pcookie = (CFileMgmtCookie*)pOtherCookie;
  99. int iColumn = *pnResult;
  100. HRESULT hr = CHasMachineName::CompareMachineNames( *pcookie, pnResult );
  101. if (S_OK != hr || 0 != *pnResult)
  102. return hr;
  103. switch (QueryObjectType())
  104. {
  105. case FILEMGMT_SHARE:
  106. {
  107. switch (iColumn)
  108. {
  109. case COMPARESIMILARCOOKIE_FULL:
  110. {
  111. BSTR bstr1 = GetColumnText(COLNUM_SHARES_SHARED_FOLDER);
  112. BSTR bstr2 = pcookie->GetColumnText(COLNUM_SHARES_SHARED_FOLDER);
  113. *pnResult = lstrcmpi(bstr1, bstr2);
  114. if (!*pnResult)
  115. {
  116. bstr1 = GetColumnText(COLNUM_SHARES_TRANSPORT);
  117. bstr2 = pcookie->GetColumnText(COLNUM_SHARES_TRANSPORT);
  118. *pnResult = lstrcmpi(bstr1, bstr2);
  119. }
  120. }
  121. break;
  122. case COLNUM_SHARES_SHARED_FOLDER:
  123. case COLNUM_SHARES_SHARED_PATH:
  124. case COLNUM_SHARES_TRANSPORT:
  125. case COLNUM_SHARES_COMMENT:
  126. {
  127. BSTR bstr1 = GetColumnText(iColumn);
  128. BSTR bstr2 = pcookie->GetColumnText(iColumn);
  129. *pnResult = lstrcmpi(bstr1, bstr2);
  130. }
  131. break;
  132. case COLNUM_SHARES_NUM_SESSIONS:
  133. {
  134. DWORD dw1 = GetNumOfCurrentUses();
  135. DWORD dw2 = pcookie->GetNumOfCurrentUses();
  136. *pnResult = (dw1 < dw2) ? -1 : ((dw1 == dw2) ? 0 : 1);
  137. }
  138. break;
  139. default:
  140. ASSERT(FALSE);
  141. *pnResult = 0;
  142. break;
  143. }
  144. }
  145. break;
  146. case FILEMGMT_SERVICE:
  147. ASSERT (0); // comparison provided by CServiceCookie
  148. break;
  149. case FILEMGMT_SESSION:
  150. {
  151. switch (iColumn)
  152. {
  153. case COMPARESIMILARCOOKIE_FULL:
  154. iColumn = COLNUM_SESSIONS_USERNAME; // fall through
  155. case COLNUM_SESSIONS_USERNAME:
  156. case COLNUM_SESSIONS_COMPUTERNAME:
  157. case COLNUM_SESSIONS_TRANSPORT:
  158. case COLNUM_SESSIONS_IS_GUEST:
  159. {
  160. BSTR bstr1 = GetColumnText(iColumn);
  161. BSTR bstr2 = pcookie->GetColumnText(iColumn);
  162. *pnResult = lstrcmpi(bstr1, bstr2);
  163. }
  164. break;
  165. case COLNUM_SESSIONS_NUM_FILES:
  166. {
  167. DWORD dw1 = GetNumOfOpenFiles();
  168. DWORD dw2 = pcookie->GetNumOfOpenFiles();
  169. *pnResult = (dw1 < dw2) ? -1 : ((dw1 == dw2) ? 0 : 1);
  170. }
  171. break;
  172. case COLNUM_SESSIONS_CONNECTED_TIME:
  173. {
  174. DWORD dw1 = GetConnectedTime();
  175. DWORD dw2 = pcookie->GetConnectedTime();
  176. *pnResult = (dw1 < dw2) ? -1 : ((dw1 == dw2) ? 0 : 1);
  177. }
  178. break;
  179. case COLNUM_SESSIONS_IDLE_TIME:
  180. {
  181. DWORD dw1 = GetIdleTime();
  182. DWORD dw2 = pcookie->GetIdleTime();
  183. *pnResult = (dw1 < dw2) ? -1 : ((dw1 == dw2) ? 0 : 1);
  184. }
  185. break;
  186. default:
  187. ASSERT(FALSE);
  188. *pnResult = 0;
  189. break;
  190. }
  191. }
  192. break;
  193. case FILEMGMT_RESOURCE:
  194. {
  195. switch (iColumn)
  196. {
  197. case COMPARESIMILARCOOKIE_FULL:
  198. iColumn = COLNUM_RESOURCES_FILENAME; // fall through
  199. case COLNUM_RESOURCES_FILENAME:
  200. case COLNUM_RESOURCES_USERNAME:
  201. case COLNUM_RESOURCES_TRANSPORT:
  202. case COLNUM_RESOURCES_OPEN_MODE:
  203. {
  204. BSTR bstr1 = GetColumnText(iColumn);
  205. BSTR bstr2 = pcookie->GetColumnText(iColumn);
  206. *pnResult = lstrcmpi(bstr1, bstr2);
  207. }
  208. break;
  209. case COLNUM_RESOURCES_NUM_LOCKS:
  210. {
  211. DWORD dw1 = GetNumOfLocks();
  212. DWORD dw2 = pcookie->GetNumOfLocks();
  213. *pnResult = (dw1 < dw2) ? -1 : ((dw1 == dw2) ? 0 : 1);
  214. }
  215. break;
  216. default:
  217. ASSERT(FALSE);
  218. *pnResult = 0;
  219. break;
  220. }
  221. }
  222. break;
  223. case FILEMGMT_ROOT:
  224. case FILEMGMT_SHARES:
  225. case FILEMGMT_SESSIONS:
  226. case FILEMGMT_RESOURCES:
  227. #ifdef SNAPIN_PROTOTYPER
  228. case FILEMGMT_PROTOTYPER:
  229. *pnResult = 0;
  230. break;
  231. #endif
  232. case FILEMGMT_SERVICES:
  233. *pnResult = 0;
  234. break;
  235. default:
  236. ASSERT(FALSE);
  237. break;
  238. }
  239. return S_OK;
  240. }
  241. //
  242. // CFileMgmtCookieBlock
  243. //
  244. DEFINE_COOKIE_BLOCK(CFileMgmtCookie)
  245. //
  246. // CFileMgmtScopeCookie
  247. //
  248. CFileMgmtScopeCookie::CFileMgmtScopeCookie(
  249. LPCTSTR lpcszMachineName,
  250. FileMgmtObjectType objecttype)
  251. : CFileMgmtCookie( objecttype )
  252. , m_hScManager( NULL )
  253. , m_fQueryServiceConfig2( FALSE )
  254. , m_hScopeItemParent( NULL )
  255. , m_strMachineName( lpcszMachineName )
  256. {
  257. ASSERT( IsAutonomousObjectType( objecttype ) );
  258. m_hScManager = NULL;
  259. m_fQueryServiceConfig2 = TRUE; // Pretend the target machine does support QueryServiceConfig2() API
  260. }
  261. CFileMgmtScopeCookie::~CFileMgmtScopeCookie()
  262. {
  263. }
  264. CCookie* CFileMgmtScopeCookie::QueryBaseCookie(int i)
  265. {
  266. UNREFERENCED_PARAMETER (i);
  267. ASSERT(0 == i);
  268. return (CCookie*)this;
  269. }
  270. int CFileMgmtScopeCookie::QueryNumCookies()
  271. {
  272. return 1;
  273. }
  274. void CFileMgmtCookie::GetDisplayName( CString& /*strref*/, BOOL /*fStaticNode*/ )
  275. {
  276. ASSERT(FALSE);
  277. }
  278. void CFileMgmtScopeCookie::GetDisplayName( CString& strref, BOOL fStaticNode )
  279. {
  280. if ( !IsAutonomousObjectType(QueryObjectType()) )
  281. {
  282. ASSERT(FALSE);
  283. return;
  284. }
  285. int nStringId = IDS_DISPLAYNAME_ROOT;
  286. if (fStaticNode)
  287. {
  288. if (NULL != QueryTargetServer())
  289. nStringId = IDS_DISPLAYNAME_s_ROOT;
  290. else
  291. nStringId = IDS_DISPLAYNAME_ROOT_LOCAL;
  292. }
  293. nStringId += (QueryObjectType() - FILEMGMT_ROOT);
  294. LoadStringPrintf(nStringId, OUT &strref, (LPCTSTR)QueryNonNULLMachineName());
  295. }
  296. void CFileMgmtScopeCookie::MarkResultChildren( CBITFLAG_FLAGWORD state )
  297. {
  298. ASSERT( FILEMGMT_SERVICES == QueryObjectType() ); // CODEWORK remove
  299. POSITION pos = m_listResultCookieBlocks.GetHeadPosition();
  300. while (NULL != pos)
  301. {
  302. CBaseCookieBlock* pblock = m_listResultCookieBlocks.GetNext( pos );
  303. ASSERT( NULL != pblock && 1 == pblock->QueryNumCookies() );
  304. CCookie* pbasecookie = pblock->QueryBaseCookie(0);
  305. CNewResultCookie* pcookie = (CNewResultCookie*)pbasecookie;
  306. pcookie->MarkState( state );
  307. }
  308. }
  309. void CFileMgmtScopeCookie::RemoveMarkedChildren()
  310. {
  311. ASSERT( FILEMGMT_SERVICES == QueryObjectType() ); // CODEWORK remove
  312. POSITION pos = m_listResultCookieBlocks.GetHeadPosition();
  313. while (NULL != pos)
  314. {
  315. POSITION posCurr = pos;
  316. CBaseCookieBlock* pblock = m_listResultCookieBlocks.GetNext( pos );
  317. ASSERT( NULL != pblock && 1 == pblock->QueryNumCookies() );
  318. CCookie* pbasecookie = pblock->QueryBaseCookie(0);
  319. CNewResultCookie* pcookie = (CNewResultCookie*)pbasecookie;
  320. if ( pcookie->IsMarkedForDeletion() )
  321. {
  322. m_listResultCookieBlocks.RemoveAt( posCurr );
  323. pcookie->Release();
  324. }
  325. }
  326. }
  327. // CODEWORK This may be a O(N^^2) performance problem when the list is long
  328. void CFileMgmtScopeCookie::ScanAndAddResultCookie( CNewResultCookie* pnewcookie )
  329. {
  330. ASSERT( FILEMGMT_SERVICES == QueryObjectType() ); // CODEWORK remove
  331. POSITION pos = m_listResultCookieBlocks.GetHeadPosition();
  332. while (NULL != pos)
  333. {
  334. CBaseCookieBlock* pblock = m_listResultCookieBlocks.GetNext( pos );
  335. ASSERT( NULL != pblock && 1 == pblock->QueryNumCookies() );
  336. CCookie* pbasecookie = pblock->QueryBaseCookie(0);
  337. CNewResultCookie* pcookie = (CNewResultCookie*)pbasecookie;
  338. if (!pcookie->IsMarkedForDeletion())
  339. continue; // this is not an old-and-still-unmatched object
  340. BOOL bSame = FALSE;
  341. HRESULT hr = pcookie->SimilarCookieIsSameObject( pnewcookie, &bSame );
  342. if ( !FAILED(hr) && bSame )
  343. {
  344. // this existing object is the same as our new object
  345. if (pcookie->CopySimilarCookie( pnewcookie ) )
  346. pcookie->MarkAsChanged();
  347. else
  348. pcookie->MarkAsOld();
  349. delete pnewcookie;
  350. return;
  351. }
  352. }
  353. AddResultCookie( pnewcookie );
  354. }
  355. #ifdef SNAPIN_PROTOTYPER
  356. //
  357. // CPrototyperScopeCookie
  358. //
  359. DEFINE_COOKIE_BLOCK(CPrototyperScopeCookie)
  360. //
  361. // CPrototyperResultCookie
  362. //
  363. DEFINE_COOKIE_BLOCK(CPrototyperResultCookie)
  364. #endif
  365. //
  366. // CNewResultCookie
  367. //
  368. CNewResultCookie::CNewResultCookie( PVOID pvCookieTypeMarker, FileMgmtObjectType objecttype )
  369. : CFileMgmtCookie( objecttype )
  370. , m_pvCookieTypeMarker( pvCookieTypeMarker )
  371. {
  372. }
  373. CNewResultCookie::~CNewResultCookie()
  374. {
  375. }
  376. // return TRUE iif object has changed
  377. BOOL CNewResultCookie::CopySimilarCookie( CNewResultCookie* /*pcookie*/ )
  378. {
  379. return FALSE;
  380. }
  381. /*
  382. class CNewShareCookie
  383. : public CNewResultCookie
  384. {
  385. public:
  386. CNewShareCookie( FILEMGMT_TRANSPORT transport )
  387. : CNewResultCookie( FILEMGMT_SHARE )
  388. , m_transport( transport )
  389. {}
  390. virtual ~CNewShareCookie();
  391. virtual BSTR QueryResultColumnText( int nCol, CFileMgmtComponentData& refcdata );
  392. public:
  393. FILEMGMT_TRANSPORT m_transport;
  394. CString m_strName;
  395. CString m_strPath;
  396. CString m_strComment;
  397. DWORD m_dwSessions;
  398. DWORD m_dwID;
  399. }; // CNewShareCookie
  400. CNewShareCookie::~CNewShareCookie() {}
  401. BSTR CNewShareCookie::QueryResultColumnText(
  402. int nCol,
  403. CFileMgmtComponentData& refcdata )
  404. {
  405. switch (nCol)
  406. {
  407. case COLNUM_SHARES_SHARED_FOLDER:
  408. return const_cast<BSTR>((LPCTSTR)m_strName);
  409. case COLNUM_SHARES_SHARED_PATH:
  410. return const_cast<BSTR>((LPCTSTR)m_strPath);
  411. case COLNUM_SHARES_TRANSPORT:
  412. return refcdata.MakeTransportResult(m_transport);
  413. case COLNUM_SHARES_NUM_SESSIONS:
  414. return refcdata.MakeDwordResult( m_dwSessions );
  415. case COLNUM_SHARES_COMMENT:
  416. return const_cast<BSTR>((LPCTSTR)m_strComment);
  417. default:
  418. ASSERT(FALSE);
  419. break;
  420. }
  421. return L"";
  422. }
  423. */