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.

577 lines
16 KiB

  1. #include "main.h"
  2. ///////////////////////////////////////////////////////////////////////////////
  3. // //
  4. // CRSOPSnapIn object implementation //
  5. // //
  6. ///////////////////////////////////////////////////////////////////////////////
  7. CRSOPSnapIn::CRSOPSnapIn(CRSOPComponentData *pComponent)
  8. {
  9. m_cRef = 1;
  10. InterlockedIncrement(&g_cRefThisDll);
  11. m_pcd = pComponent;
  12. m_pConsole = NULL;
  13. m_pConsole2 = NULL;
  14. m_pResult = NULL;
  15. m_pHeader = NULL;
  16. m_pConsoleVerb = NULL;
  17. m_pDisplayHelp = NULL;
  18. m_nColumnSize = 180;
  19. m_lViewMode = LVS_REPORT;
  20. m_bExpand = TRUE;
  21. LoadString(g_hInstance, IDS_NAME, m_column1, ARRAYSIZE(m_column1));
  22. }
  23. CRSOPSnapIn::~CRSOPSnapIn()
  24. {
  25. InterlockedDecrement(&g_cRefThisDll);
  26. }
  27. ///////////////////////////////////////////////////////////////////////////////
  28. // //
  29. // CRSOPSnapIn object implementation (IUnknown) //
  30. // //
  31. ///////////////////////////////////////////////////////////////////////////////
  32. HRESULT CRSOPSnapIn::QueryInterface (REFIID riid, void **ppv)
  33. {
  34. if (IsEqualIID(riid, IID_IComponent) || IsEqualIID(riid, IID_IUnknown))
  35. {
  36. *ppv = (LPCOMPONENT)this;
  37. m_cRef++;
  38. return S_OK;
  39. }
  40. else if (IsEqualIID(riid, IID_IExtendContextMenu))
  41. {
  42. *ppv = (LPEXTENDCONTEXTMENU)this;
  43. m_cRef++;
  44. return S_OK;
  45. }
  46. else
  47. {
  48. *ppv = NULL;
  49. return E_NOINTERFACE;
  50. }
  51. }
  52. ULONG CRSOPSnapIn::AddRef (void)
  53. {
  54. return ++m_cRef;
  55. }
  56. ULONG CRSOPSnapIn::Release (void)
  57. {
  58. if (--m_cRef == 0) {
  59. delete this;
  60. return 0;
  61. }
  62. return m_cRef;
  63. }
  64. ///////////////////////////////////////////////////////////////////////////////
  65. // //
  66. // CRSOPSnapIn object implementation (IComponent) //
  67. // //
  68. ///////////////////////////////////////////////////////////////////////////////
  69. STDMETHODIMP CRSOPSnapIn::Initialize(LPCONSOLE lpConsole)
  70. {
  71. HRESULT hr;
  72. // Save the IConsole pointer
  73. m_pConsole = lpConsole;
  74. m_pConsole->AddRef();
  75. // QI for IConsole2
  76. hr = lpConsole->QueryInterface(IID_IConsole2, (LPVOID *)&m_pConsole2);
  77. if (FAILED(hr))
  78. {
  79. DebugMsg((DM_WARNING, TEXT("CRSOPSnapIn::Initialize: Failed to QI for IConsole2.")));
  80. m_pConsole->Release();
  81. m_pConsole = NULL;
  82. return hr;
  83. }
  84. hr = m_pConsole->QueryInterface(IID_IHeaderCtrl,
  85. reinterpret_cast<void**>(&m_pHeader));
  86. m_pConsole->QueryInterface(IID_IResultData,
  87. reinterpret_cast<void**>(&m_pResult));
  88. hr = m_pConsole->QueryConsoleVerb(&m_pConsoleVerb);
  89. hr = m_pConsole->QueryInterface(IID_IDisplayHelp,
  90. reinterpret_cast<void**>(&m_pDisplayHelp));
  91. return S_OK;
  92. }
  93. STDMETHODIMP CRSOPSnapIn::Destroy(MMC_COOKIE cookie)
  94. {
  95. if (m_pConsole2 != NULL)
  96. {
  97. m_pConsole2->Release();
  98. m_pConsole2 = NULL;
  99. }
  100. if (m_pConsole != NULL)
  101. {
  102. m_pConsole->Release();
  103. m_pConsole = NULL;
  104. }
  105. if (m_pHeader != NULL)
  106. {
  107. m_pHeader->Release();
  108. m_pHeader = NULL;
  109. }
  110. if (m_pResult != NULL)
  111. {
  112. m_pResult->Release();
  113. m_pResult = NULL;
  114. }
  115. if (m_pConsoleVerb != NULL)
  116. {
  117. m_pConsoleVerb->Release();
  118. m_pConsoleVerb = NULL;
  119. }
  120. if (m_pDisplayHelp != NULL)
  121. {
  122. m_pDisplayHelp->Release();
  123. m_pDisplayHelp = NULL;
  124. }
  125. return S_OK;
  126. }
  127. STDMETHODIMP CRSOPSnapIn::Notify(LPDATAOBJECT lpDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  128. {
  129. HRESULT hr = S_OK;
  130. switch(event)
  131. {
  132. case MMCN_COLUMNS_CHANGED:
  133. hr = S_OK;
  134. break;
  135. case MMCN_DBLCLICK:
  136. hr = S_FALSE;
  137. break;
  138. case MMCN_ADD_IMAGES:
  139. HBITMAP hbmp16x16;
  140. HBITMAP hbmp32x32;
  141. hbmp16x16 = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_16x16));
  142. if (hbmp16x16)
  143. {
  144. hbmp32x32 = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_32x32));
  145. if (hbmp32x32)
  146. {
  147. LPIMAGELIST pImageList = (LPIMAGELIST) arg;
  148. // Set the images
  149. pImageList->ImageListSetStrip(reinterpret_cast<LONG_PTR *>(hbmp16x16),
  150. reinterpret_cast<LONG_PTR *>(hbmp32x32),
  151. 0, RGB(255, 0, 255));
  152. DeleteObject(hbmp32x32);
  153. }
  154. DeleteObject(hbmp16x16);
  155. }
  156. break;
  157. case MMCN_SHOW:
  158. if ( m_pcd->IsNamespaceInitialized() )
  159. {
  160. if (arg == TRUE)
  161. {
  162. RESULTDATAITEM resultItem;
  163. LPRSOPDATAOBJECT pRSOPDataObject;
  164. MMC_COOKIE cookie;
  165. INT i;
  166. LPCONSOLE2 lpConsole2;
  167. //
  168. // Get the cookie of the scope pane item
  169. //
  170. hr = lpDataObject->QueryInterface(IID_IRSOPDataObject, (LPVOID *)&pRSOPDataObject);
  171. if (FAILED(hr))
  172. return S_OK;
  173. hr = pRSOPDataObject->GetCookie(&cookie);
  174. pRSOPDataObject->Release(); // release initial ref
  175. if (FAILED(hr))
  176. return S_OK;
  177. //
  178. // Prepare the view
  179. //
  180. m_pHeader->InsertColumn(0, m_column1, LVCFMT_LEFT, m_nColumnSize);
  181. m_pResult->SetViewMode(m_lViewMode);
  182. //
  183. // Add result pane items for this node
  184. //
  185. for (i = 0; i < g_RsopNameSpace[cookie].cResultItems; i++)
  186. {
  187. resultItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  188. resultItem.str = MMC_CALLBACK;
  189. resultItem.nImage = g_RsopNameSpace[cookie].pResultItems[i].iImage;
  190. resultItem.lParam = (LPARAM) &g_RsopNameSpace[cookie].pResultItems[i];
  191. m_pResult->InsertItem(&resultItem);
  192. }
  193. m_pResult->Sort(0, 0, -1);
  194. if (m_bExpand)
  195. {
  196. hr = m_pConsole->QueryInterface(IID_IConsole2,
  197. reinterpret_cast<void**>(&lpConsole2));
  198. if (SUCCEEDED(hr))
  199. {
  200. if ( m_pcd->GetMachineScope() )
  201. {
  202. lpConsole2->Expand( m_pcd->GetMachineScope() , TRUE);
  203. }
  204. if ( m_pcd->GetUserScope() )
  205. {
  206. lpConsole2->Expand( m_pcd->GetUserScope() , TRUE);
  207. }
  208. lpConsole2->Release();
  209. }
  210. m_bExpand = FALSE;
  211. }
  212. }
  213. else
  214. {
  215. m_pHeader->GetColumnWidth(0, &m_nColumnSize);
  216. m_pResult->GetViewMode(&m_lViewMode);
  217. }
  218. }
  219. else
  220. {
  221. if ( arg == TRUE )
  222. {
  223. IUnknown* pUnk = NULL;
  224. hr = m_pConsole2->QueryResultView( &pUnk );
  225. // QI for IMessageView
  226. IMessageView* pMessageView = NULL;
  227. hr = pUnk->QueryInterface(IID_IMessageView, (LPVOID *)&pMessageView );
  228. if (FAILED(hr))
  229. {
  230. DebugMsg((DM_WARNING, TEXT("CRSOPSnapIn::Notify: Failed to QI for IMessageView.")));
  231. pUnk->Release();
  232. }
  233. else
  234. {
  235. TCHAR szString[256];
  236. pUnk->Release();
  237. pMessageView->SetIcon( Icon_Warning );
  238. LoadString( g_hInstance, IDS_RSOP_UNINITIALIZED, szString, ARRAYSIZE(szString) );
  239. pMessageView->SetTitleText( szString );
  240. LoadString( g_hInstance, IDS_RSOP_UNINITIALIZEDDESC, szString, ARRAYSIZE(szString) );
  241. pMessageView->SetBodyText( szString );
  242. pMessageView->Release();
  243. }
  244. }
  245. }
  246. break;
  247. case MMCN_SELECT:
  248. if (m_pConsoleVerb)
  249. {
  250. LPRESULTITEM pItem;
  251. LPRSOPDATAOBJECT pRSOPDataObject;
  252. DATA_OBJECT_TYPES type;
  253. MMC_COOKIE cookie;
  254. //
  255. // Set the default verb to open
  256. //
  257. m_pConsoleVerb->SetDefaultVerb(MMC_VERB_OPEN);
  258. // First check if the snapin has been initialized
  259. if ( m_pcd->IsNamespaceInitialized() )
  260. {
  261. //
  262. // See if this is one of our items.
  263. //
  264. hr = lpDataObject->QueryInterface(IID_IRSOPDataObject, (LPVOID *)&pRSOPDataObject);
  265. if (FAILED(hr))
  266. break;
  267. pRSOPDataObject->GetType(&type);
  268. pRSOPDataObject->GetCookie(&cookie);
  269. pRSOPDataObject->Release();
  270. //
  271. // If this is a result pane item or the root of the namespace
  272. // nodes, enable the Properties menu item
  273. //
  274. if ((type == CCT_RESULT) ||
  275. ((type == CCT_SCOPE) && ((cookie == 0) || (cookie == 1) || (cookie == 2))))
  276. {
  277. m_pConsoleVerb->SetVerbState(MMC_VERB_PROPERTIES, ENABLED, TRUE);
  278. //
  279. // If this is a result pane item, then change the default
  280. // verb to Properties.
  281. //
  282. if (type == CCT_RESULT)
  283. m_pConsoleVerb->SetDefaultVerb(MMC_VERB_PROPERTIES);
  284. }
  285. }
  286. else
  287. {
  288. m_pConsoleVerb->SetVerbState(MMC_VERB_PROPERTIES, HIDDEN, TRUE);
  289. }
  290. }
  291. break;
  292. case MMCN_CONTEXTHELP:
  293. {
  294. if (m_pDisplayHelp)
  295. {
  296. LPOLESTR pszHelpTopic;
  297. LPRSOPDATAOBJECT pRSOPDataObject;
  298. MMC_COOKIE cookie;
  299. //
  300. // Get the cookie of the scope pane item
  301. //
  302. hr = lpDataObject->QueryInterface(IID_IRSOPDataObject, (LPVOID *)&pRSOPDataObject);
  303. if (FAILED(hr))
  304. return S_OK;
  305. hr = pRSOPDataObject->GetCookie(&cookie);
  306. pRSOPDataObject->Release(); // release initial ref
  307. if (FAILED(hr))
  308. return S_OK;
  309. ULONG ulNoChars = 50;
  310. pszHelpTopic = (LPOLESTR) CoTaskMemAlloc (ulNoChars * sizeof(WCHAR));
  311. if (pszHelpTopic)
  312. {
  313. hr = StringCchCopy (pszHelpTopic, ulNoChars, g_RsopNameSpace[cookie].lpHelpTopic);
  314. if (SUCCEEDED(hr))
  315. {
  316. m_pDisplayHelp->ShowTopic (pszHelpTopic);
  317. }
  318. }
  319. }
  320. }
  321. break;
  322. default:
  323. hr = E_UNEXPECTED;
  324. break;
  325. }
  326. return hr;
  327. }
  328. STDMETHODIMP CRSOPSnapIn::GetDisplayInfo(LPRESULTDATAITEM pResult)
  329. {
  330. if (pResult)
  331. {
  332. if (pResult->bScopeItem == TRUE)
  333. {
  334. if (pResult->mask & RDI_STR)
  335. {
  336. if (pResult->nCol == 0)
  337. {
  338. pResult->str = g_RsopNameSpace[pResult->lParam].szDisplayName;
  339. }
  340. else
  341. {
  342. pResult->str = L"";
  343. }
  344. }
  345. if (pResult->mask & RDI_IMAGE)
  346. {
  347. if ( m_pcd->IsNamespaceInitialized() )
  348. {
  349. INT iIcon;
  350. iIcon = g_RsopNameSpace[pResult->lParam].iIcon;
  351. if ((pResult->lParam == 1) && m_pcd->ComputerGPCoreErrorExists() )
  352. {
  353. iIcon = 12;
  354. }
  355. else if ((pResult->lParam == 1) && (m_pcd->ComputerCSEErrorExists() || m_pcd->ComputerGPCoreWarningExists()))
  356. {
  357. iIcon = 14;
  358. }
  359. else if ((pResult->lParam == 2) && m_pcd->UserGPCoreErrorExists() )
  360. {
  361. iIcon = 13;
  362. }
  363. else if ((pResult->lParam == 2) && (m_pcd->UserCSEErrorExists() || m_pcd->UserGPCoreWarningExists()))
  364. {
  365. iIcon = 15;
  366. }
  367. pResult->nImage = iIcon;
  368. }
  369. else
  370. pResult->nImage = 3;
  371. }
  372. }
  373. else
  374. {
  375. if (pResult->mask & RDI_STR)
  376. {
  377. if (pResult->nCol == 0)
  378. {
  379. LPRESULTITEM lpResultItem = (LPRESULTITEM)pResult->lParam;
  380. if (lpResultItem->szDisplayName[0] == TEXT('\0'))
  381. {
  382. LoadString (g_hInstance, lpResultItem->iStringID,
  383. lpResultItem->szDisplayName,
  384. MAX_DISPLAYNAME_SIZE);
  385. }
  386. pResult->str = lpResultItem->szDisplayName;
  387. }
  388. if (pResult->str == NULL)
  389. pResult->str = (LPOLESTR)L"";
  390. }
  391. }
  392. }
  393. return S_OK;
  394. }
  395. STDMETHODIMP CRSOPSnapIn::QueryDataObject(MMC_COOKIE cookie, DATA_OBJECT_TYPES type, LPDATAOBJECT *ppDataObject)
  396. {
  397. return m_pcd->QueryDataObject(cookie, type, ppDataObject);
  398. }
  399. STDMETHODIMP CRSOPSnapIn::GetResultViewType(MMC_COOKIE cookie, LPOLESTR *ppViewType,
  400. long *pViewOptions)
  401. {
  402. if ( m_pcd->IsNamespaceInitialized() )
  403. {
  404. return S_FALSE;
  405. }
  406. else
  407. {
  408. StringFromCLSID( CLSID_MessageView, ppViewType );
  409. return S_OK;
  410. }
  411. }
  412. STDMETHODIMP CRSOPSnapIn::CompareObjects(LPDATAOBJECT lpDataObjectA, LPDATAOBJECT lpDataObjectB)
  413. {
  414. HRESULT hr = S_FALSE;
  415. LPRSOPDATAOBJECT pRSOPDataObjectA, pRSOPDataObjectB;
  416. MMC_COOKIE cookie1, cookie2;
  417. if (lpDataObjectA == NULL || lpDataObjectB == NULL)
  418. return E_POINTER;
  419. //
  420. // QI for the private GPODataObject interface
  421. //
  422. if (FAILED(lpDataObjectA->QueryInterface(IID_IRSOPDataObject,
  423. (LPVOID *)&pRSOPDataObjectA)))
  424. {
  425. return S_FALSE;
  426. }
  427. if (FAILED(lpDataObjectB->QueryInterface(IID_IRSOPDataObject,
  428. (LPVOID *)&pRSOPDataObjectB)))
  429. {
  430. pRSOPDataObjectA->Release();
  431. return S_FALSE;
  432. }
  433. pRSOPDataObjectA->GetCookie(&cookie1);
  434. pRSOPDataObjectB->GetCookie(&cookie2);
  435. if (cookie1 == cookie2)
  436. {
  437. hr = S_OK;
  438. }
  439. pRSOPDataObjectA->Release();
  440. pRSOPDataObjectB->Release();
  441. return hr;
  442. }
  443. ///////////////////////////////////////////////////////////////////////////////
  444. // //
  445. // CRSOPSnapIn object implementation (IExtendContextMenu) //
  446. // //
  447. ///////////////////////////////////////////////////////////////////////////////
  448. STDMETHODIMP CRSOPSnapIn::AddMenuItems(LPDATAOBJECT piDataObject,
  449. LPCONTEXTMENUCALLBACK pCallback,
  450. LONG *pInsertionAllowed)
  451. {
  452. return m_pcd->AddMenuItems (piDataObject, pCallback, pInsertionAllowed);
  453. }
  454. STDMETHODIMP CRSOPSnapIn::Command(LONG lCommandID, LPDATAOBJECT piDataObject)
  455. {
  456. return m_pcd->Command (lCommandID, piDataObject);
  457. }