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.

500 lines
13 KiB

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