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.

597 lines
15 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. #ifndef __WMISNAPIN_H_
  3. #define __WMISNAPIN_H_
  4. #include "..\resource.h"
  5. #include "atlwin.h"
  6. #include "atlsnap.h"
  7. #include "CHString1.h"
  8. #include "UIHelpers.h"
  9. #include "..\datasrc.h"
  10. #include "WMICtrSysmonDlg.h"
  11. #include "WmiCtrsDlg.h"
  12. #include <util.h>
  13. #define DBG_PRINTFA( a ) { char pBuff[256]; sprintf a ; OutputDebugStringA(pBuff); }
  14. class CWMISnapinExtData;
  15. class CWMISnapinExtData;
  16. class CWMISnapinData : public CSnapInItemImpl<CWMISnapinData>,
  17. public CComObject<CSnapInDataObjectImpl>
  18. {
  19. public:
  20. static const GUID* m_NODETYPE;
  21. static const OLECHAR* m_SZNODETYPE;
  22. static const OLECHAR* m_SZDISPLAY_NAME;
  23. static const CLSID* m_SNAPIN_CLASSID;
  24. bool m_extension;
  25. DataSource *g_DS;
  26. wchar_t m_initMachineName[MAXCOMPUTER_NAME + 1];
  27. TCHAR m_MachineName[MAXCOMPUTER_NAME + 1];
  28. CWMISnapinData(bool extension = false);
  29. ~CWMISnapinData()
  30. {
  31. if(g_DS)
  32. {
  33. g_DS->Release();
  34. g_DS = NULL;
  35. }
  36. }
  37. SNAPINMENUID(IDR_MENU_MENU);
  38. /* const UINT GetMenuID()
  39. {
  40. return m_MenuId;
  41. }
  42. void SetMenuID(UINT menuId)
  43. {
  44. m_MenuId = menuId;
  45. }
  46. */
  47. STDMETHOD(AddMenuItems)(LPCONTEXTMENUCALLBACK piCallback,
  48. long *pInsertionAllowed,
  49. DATA_OBJECT_TYPES type)
  50. {
  51. if(m_myID == 0 || m_extension)
  52. {
  53. //return CSnapInItemImpl<CWMISnapinData>::AddMenuItems(piCallback, pInsertionAllowed,type);
  54. return S_OK;
  55. }
  56. else
  57. {
  58. return CSnapInItemImpl<CWMISnapinData>::AddMenuItems(piCallback, pInsertionAllowed,type);
  59. }
  60. }
  61. STDMETHOD(Command)(long lCommandID,
  62. CSnapInObjectRootBase* pObj,
  63. DATA_OBJECT_TYPES type)
  64. {
  65. HRESULT hr = S_OK;
  66. if(type == CCT_SCOPE)
  67. {
  68. switch(lCommandID)
  69. {
  70. case ID_TOP_RECONNECT:
  71. {
  72. if(m_myID == 0)
  73. {
  74. }
  75. else
  76. {
  77. TCHAR name[MAXCOMPUTER_NAME + 1] = {0};
  78. bool isLocal = g_DS->IsLocal();
  79. LOGIN_CREDENTIALS *credentials = g_DS->GetCredentials();
  80. INT_PTR x = CUIHelpers::DisplayCompBrowser(NULL,
  81. name,
  82. sizeof(name)/sizeof(name[0]),
  83. &isLocal,
  84. credentials);
  85. if(x == IDOK)
  86. {
  87. if (g_DS)
  88. {
  89. g_DS->ClosePropSheet();
  90. g_DS->Disconnect();
  91. }
  92. if(isLocal)
  93. {
  94. // an empty string will cause a local connection.
  95. name[0] = '\0';
  96. }
  97. _tcscpy(m_MachineName,name);
  98. g_DS->SetMachineName(CHString1(name));
  99. CComQIPtr<IConsoleNameSpace, &IID_IConsoleNameSpace> spConsoleNameSpace(m_spConsole);
  100. SCOPEDATAITEM scope;
  101. TCHAR name[MAXCOMPUTER_NAME + 40] = {0};
  102. BuildRootNodeName(name);
  103. memset(&scope, 0, sizeof(SCOPEDATAITEM));
  104. scope.mask = SDI_STR;
  105. scope.displayname = name;
  106. scope.ID = m_myID;
  107. HRESULT z = spConsoleNameSpace->SetItem(&scope);
  108. }
  109. }
  110. }
  111. break;
  112. case ID_PERF_COUNTERS:
  113. {
  114. try
  115. {
  116. CWMICtrSysmonDlg *test;
  117. test = new CWMICtrSysmonDlg((LPCTSTR)g_DS->m_whackedMachineName);
  118. test->DoModal();
  119. eStatusInfo st = test->GetStatus();
  120. if(st == Status_CounterNotFound)
  121. {
  122. TCHAR strTemp[1024];
  123. if(::LoadString(_Module.GetModuleInstance(),IDC_RETRY_TEXT,strTemp,1024) <= 0)
  124. {
  125. _tcscpy(strTemp,_T("Could Not Connect to WMI Using Logged on User. WMIControl will try to display the counters as Text"));
  126. }
  127. TCHAR strHead[1024];
  128. if(::LoadString(_Module.GetModuleInstance(),IDC_ERROR_CAPTION,strHead,1024) <= 0)
  129. {
  130. _tcscpy(strHead,_T("WMIControl - Error"));
  131. }
  132. MessageBox(NULL,strTemp,strHead,MB_OK);
  133. CWmiCtrsDlg *CtrsDlg; //g_DS
  134. CtrsDlg = new CWmiCtrsDlg((LPCTSTR)g_DS->m_whackedMachineName,g_DS->GetCredentials());
  135. CtrsDlg->DoModal(NULL);
  136. }
  137. }
  138. catch(...)
  139. {
  140. MessageBox(NULL,_T("Error Occured"),_T("Exception"),MB_OK);
  141. }
  142. }
  143. default: break;
  144. }
  145. }
  146. return hr;
  147. }
  148. STDMETHOD (BuildRootNodeName)(TCHAR *name)
  149. {
  150. HRESULT hr = E_FAIL;
  151. LoadString(_Module.GetResourceInstance(),
  152. IDS_DISPLAY_NAME,
  153. name, MAXCOMPUTER_NAME + 40);
  154. if(g_DS)
  155. {
  156. if(g_DS->IsLocal())
  157. {
  158. TCHAR local[20] = {0};
  159. LoadString(_Module.GetResourceInstance(),
  160. IDS_LOCAL_COMPUTER,
  161. local, 20);
  162. _tcscat(name, local);
  163. }
  164. else
  165. {
  166. TCHAR machine[MAXCOMPUTER_NAME + 1] = {0};
  167. _tcscpy(machine, (LPCTSTR)g_DS->m_whackedMachineName);
  168. _tcsupr(machine);
  169. _tcscat(name, _T(" ("));
  170. // NOTE: skip the leading whacks
  171. _tcscat(name, (LPCTSTR)&machine[2]);
  172. _tcscat(name, _T(")"));
  173. }
  174. hr = S_OK;
  175. }
  176. return hr;
  177. }
  178. STDMETHOD (FillData)(CLIPFORMAT cf, LPSTREAM pStream)
  179. {
  180. HRESULT hr = DV_E_CLIPFORMAT;
  181. if(cf == m_CCF_DISPLAY_NAME)
  182. {
  183. ULONG uWritten;
  184. TCHAR name[MAXCOMPUTER_NAME + 20] = {0};
  185. BuildRootNodeName(name);
  186. hr = pStream->Write(name, (ocslen((OLECHAR*)name) + 1) * sizeof(OLECHAR), &uWritten);
  187. }
  188. else
  189. {
  190. hr = CSnapInItemImpl<CWMISnapinData>::FillData(cf, pStream);
  191. }
  192. return hr;
  193. }
  194. STDMETHOD(GetScopePaneInfo)(SCOPEDATAITEM *pScopeDataItem);
  195. STDMETHOD(GetResultPaneInfo)(RESULTDATAITEM *pResultDataItem);
  196. STDMETHOD(GetResultViewType)(LPOLESTR *ppViewType,
  197. long *pViewOptions)
  198. {
  199. *pViewOptions = MMC_VIEW_OPTIONS_NOLISTVIEWS;
  200. LPOLESTR psz = NULL;
  201. StringFromCLSID(CLSID_MessageView, &psz);
  202. *ppViewType = (LPOLESTR)CoTaskMemAlloc(sizeof(OLECHAR)*(lstrlen(psz)+1));
  203. if (!*ppViewType)
  204. return E_OUTOFMEMORY;
  205. lstrcpy(*ppViewType, psz);
  206. return S_OK;
  207. }
  208. STDMETHOD(Notify)( MMC_NOTIFY_TYPE event,
  209. LPARAM arg,
  210. LPARAM param,
  211. IComponentData* pComponentData,
  212. IComponent* pComponent,
  213. DATA_OBJECT_TYPES type);
  214. LPOLESTR GetResultPaneColInfo(int nCol);
  215. STDMETHOD(CreatePropertyPages)(LPPROPERTYSHEETCALLBACK lpProvider,
  216. LONG_PTR handle,
  217. IUnknown* pUnk,
  218. DATA_OBJECT_TYPES type);
  219. STDMETHOD(QueryPagesFor)(DATA_OBJECT_TYPES type)
  220. {
  221. return S_OK;
  222. }
  223. CWMISnapinExtData *m_parent;
  224. CComPtr<IConsole> m_spConsole;
  225. LPPROPERTYSHEETCALLBACK m_lpProvider;
  226. wchar_t m_nodeType[50];
  227. wchar_t m_nodeDesc[100];
  228. wchar_t m_descBar[100];
  229. HSCOPEITEM m_myID;
  230. UINT m_MenuId;
  231. };
  232. class CWMISnapinExtData : public CSnapInItemImpl<CWMISnapinExtData, TRUE>,
  233. public CComObject<CSnapInDataObjectImpl>
  234. {
  235. private:
  236. wchar_t m_nodeName[50];
  237. CWMISnapinData *m_pScopeItem;
  238. public:
  239. static const GUID* m_NODETYPE;
  240. static const OLECHAR* m_SZNODETYPE;
  241. static const OLECHAR* m_SZDISPLAY_NAME;
  242. static const CLSID* m_SNAPIN_CLASSID;
  243. CWMISnapinExtData()
  244. {
  245. memset(&m_scopeDataItem, 0, sizeof(SCOPEDATAITEM));
  246. m_scopeDataItem.mask = SDI_STR | SDI_IMAGE | SDI_OPENIMAGE | SDI_PARAM;
  247. m_scopeDataItem.displayname = MMC_CALLBACK;
  248. m_scopeDataItem.nImage = IDI_WMICNTL; // May need modification
  249. m_scopeDataItem.nOpenImage = IDI_WMICNTL; // May need modification
  250. m_scopeDataItem.lParam = (LPARAM) this;
  251. memset(&m_resultDataItem, 0, sizeof(RESULTDATAITEM));
  252. m_resultDataItem.mask = RDI_STR | RDI_IMAGE | RDI_PARAM;
  253. m_resultDataItem.str = MMC_CALLBACK;
  254. m_resultDataItem.nImage = IDI_WMICNTL; // May need modification
  255. m_resultDataItem.lParam = (LPARAM) this;
  256. m_pScopeItem = NULL;
  257. memset(m_nodeName, 0, 50 * sizeof(wchar_t));
  258. if(::LoadString(_Module.GetModuleInstance(), IDS_DISPLAY_NAME, m_nodeName, 50) == 0)
  259. {
  260. wcscpy(m_nodeName, L"WMI Control");
  261. }
  262. }
  263. ~CWMISnapinExtData()
  264. {
  265. }
  266. STDMETHOD(QueryPagesFor)(DATA_OBJECT_TYPES type)
  267. {
  268. return S_OK;
  269. }
  270. IDataObject* m_pDataObject;
  271. virtual void InitDataClass(IDataObject* pDataObject, CSnapInItem* pDefault)
  272. {
  273. m_pDataObject = pDataObject;
  274. // The default code stores off the pointer to the Dataobject the class is wrapping
  275. // at the time.
  276. // Alternatively you could convert the dataobject to the internal format
  277. // it represents and store that information
  278. }
  279. CSnapInItem* GetExtNodeObject(IDataObject* pDataObject, CSnapInItem* pDefault)
  280. {
  281. // Modify to return a different CSnapInItem* pointer.
  282. return pDefault;
  283. }
  284. STDMETHOD(Notify)( MMC_NOTIFY_TYPE event,
  285. LPARAM arg,
  286. LPARAM param,
  287. IComponentData* pComponentData,
  288. IComponent* pComponent,
  289. DATA_OBJECT_TYPES type);
  290. STDMETHOD(GetDataHere)(FORMATETC* pformatetc, STGMEDIUM* pmedium);
  291. wchar_t* MachineName();
  292. wchar_t m_MachineName[255];
  293. };
  294. class CWMISnapin;
  295. class CWMISnapinComponent : public CComObjectRootEx<CComSingleThreadModel>,
  296. public CSnapInObjectRoot<2, CWMISnapin >,
  297. public IExtendPropertySheetImpl<CWMISnapinComponent>,
  298. public IComponentImpl<CWMISnapinComponent>
  299. {
  300. public:
  301. BEGIN_COM_MAP(CWMISnapinComponent)
  302. COM_INTERFACE_ENTRY(IComponent)
  303. COM_INTERFACE_ENTRY(IExtendPropertySheet)
  304. END_COM_MAP()
  305. public:
  306. CWMISnapinComponent()
  307. {
  308. }
  309. STDMETHOD(Notify)(LPDATAOBJECT lpDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  310. {
  311. if (lpDataObject != NULL && lpDataObject != DOBJ_CUSTOMOCX)
  312. return IComponentImpl<CWMISnapinComponent>::Notify(lpDataObject, event, arg, param);
  313. // TODO : Add code to handle notifications that set lpDataObject == NULL.
  314. return E_NOTIMPL;
  315. }
  316. };
  317. class CWMISnapin : public CComObjectRootEx<CComSingleThreadModel>,
  318. public CSnapInObjectRoot<1, CWMISnapin>,
  319. public IComponentDataImpl<CWMISnapin, CWMISnapinComponent>,
  320. public IExtendPropertySheetImpl<CWMISnapin>,
  321. public IExtendContextMenuImpl<CWMISnapin>,
  322. public CComCoClass<CWMISnapin, &CLSID_WMISnapin>,
  323. public IPersistStream,
  324. public ISnapinHelp
  325. {
  326. public:
  327. CWMISnapin() : m_bDirty(false)
  328. {
  329. m_pNode = new CWMISnapinData;
  330. _ASSERTE(m_pNode != NULL);
  331. m_pComponentData = this;
  332. }
  333. ~CWMISnapin()
  334. {
  335. delete m_pNode;
  336. m_pNode = NULL;
  337. }
  338. EXTENSION_SNAPIN_DATACLASS(CWMISnapinExtData)
  339. BEGIN_EXTENSION_SNAPIN_NODEINFO_MAP(CWMISnapin)
  340. EXTENSION_SNAPIN_NODEINFO_ENTRY(CWMISnapinExtData)
  341. END_EXTENSION_SNAPIN_NODEINFO_MAP()
  342. BEGIN_COM_MAP(CWMISnapin)
  343. COM_INTERFACE_ENTRY(IComponentData)
  344. COM_INTERFACE_ENTRY(IExtendPropertySheet)
  345. COM_INTERFACE_ENTRY(IExtendContextMenu)
  346. COM_INTERFACE_ENTRY(ISnapinHelp)
  347. COM_INTERFACE_ENTRY(IPersistStream)
  348. END_COM_MAP()
  349. // NOTE: using UpdateRegistry() directly so I can localize for "PRETTYNAME".
  350. //DECLARE_REGISTRY_RESOURCEID(IDR_WMISNAPIN)
  351. DECLARE_NOT_AGGREGATABLE(CWMISnapin)
  352. static HRESULT WINAPI UpdateRegistry(BOOL bRegister)
  353. {
  354. HRESULT hr;
  355. TCHAR pName[100] = {0};
  356. if(::LoadString(_Module.GetModuleInstance(), IDS_DISPLAY_NAME,
  357. pName, 100) <= 0)
  358. {
  359. wcscpy(pName, _T("WMI Control"));
  360. }
  361. TCHAR dispName[_MAX_PATH+102] = {0};
  362. TCHAR szModule[_MAX_PATH+1];
  363. szModule[_MAX_PATH] = 0;
  364. ::GetModuleFileName(_Module.GetModuleInstance(), szModule, _MAX_PATH);
  365. _stprintf( dispName,_T("@%s,-%d"), szModule, IDS_DISPLAY_NAME);
  366. _ATL_REGMAP_ENTRY regMap[] = {{ OLESTR("PRETTYNAME"), pName },
  367. { OLESTR("NAMESTRINGINDIRECT"),dispName},
  368. { 0, 0 }};
  369. hr = _Module.UpdateRegistryFromResourceD(IDR_WMISNAPIN, TRUE, regMap);
  370. return hr;
  371. }
  372. STDMETHOD(Initialize)(LPUNKNOWN pUnknown);
  373. static void WINAPI ObjectMain(bool bStarting)
  374. {
  375. if (bStarting)
  376. CSnapInItem::Init();
  377. }
  378. STDMETHOD(GetHelpTopic)(LPOLESTR *lpCompiledHelpFile)
  379. {
  380. if(lpCompiledHelpFile == NULL)
  381. return E_POINTER;
  382. wchar_t helpDir[_MAX_PATH + 50];
  383. memset(helpDir, 0, (_MAX_PATH + 50) * sizeof(wchar_t));
  384. if(GetSystemWindowsDirectory(helpDir, _MAX_PATH+1) != 0)
  385. {
  386. wcscat(helpDir, L"\\Help");
  387. wcscat(helpDir, L"\\newfeat1.chm");
  388. *lpCompiledHelpFile = reinterpret_cast<LPOLESTR>(
  389. CoTaskMemAlloc((wcslen(helpDir) + 1) *
  390. sizeof(wchar_t)));
  391. if(*lpCompiledHelpFile == NULL)
  392. return E_OUTOFMEMORY;
  393. USES_CONVERSION;
  394. wcscpy(*lpCompiledHelpFile, T2OLE((LPTSTR)(LPCTSTR)helpDir));
  395. return S_OK;
  396. }
  397. return E_UNEXPECTED;
  398. }
  399. // IPersistStream methods
  400. STDMETHOD(GetClassID)(CLSID *pClassID);
  401. STDMETHOD(IsDirty)();
  402. STDMETHOD(Load)(IStream *pStm);
  403. STDMETHOD(Save)(IStream *pStm, BOOL fClearDirty);
  404. STDMETHOD(GetSizeMax)(ULARGE_INTEGER *pcbSize);
  405. STDMETHOD(CompareObjects)( LPDATAOBJECT lpDataObjectA,
  406. LPDATAOBJECT lpDataObjectB)
  407. {
  408. CSnapInItem *alpha = NULL, *beta = NULL;
  409. DATA_OBJECT_TYPES able, baker;
  410. HRESULT hr = GetDataClass(lpDataObjectA, &alpha, &able);
  411. hr = GetDataClass(lpDataObjectB, &beta, &baker);
  412. return (alpha == beta ? S_OK : S_FALSE);
  413. }
  414. private:
  415. HRESULT ReadStream(IStream *pStm, void *data, ULONG *size);
  416. bool m_bDirty; // for the IPersistStream
  417. HRESULT LoadIcon(CComPtr<IImageList> &spImageList,
  418. UINT resID);
  419. };
  420. class ATL_NO_VTABLE CWMISnapinAbout : public ISnapinAbout,
  421. public CComObjectRoot,
  422. public CComCoClass< CWMISnapinAbout, &CLSID_WMISnapinAbout>
  423. {
  424. public:
  425. DECLARE_REGISTRY(CWMISnapinAbout, _T("WMISnapinAbout.1"), _T("WMISnapinAbout.1"), IDS_WMISNAPIN_DESC, THREADFLAGS_BOTH);
  426. BEGIN_COM_MAP(CWMISnapinAbout)
  427. COM_INTERFACE_ENTRY(ISnapinAbout)
  428. END_COM_MAP()
  429. STDMETHOD(GetSnapinDescription)(LPOLESTR *lpDescription)
  430. {
  431. USES_CONVERSION;
  432. TCHAR szBuf[256] = {0};
  433. if (::LoadString(_Module.GetResourceInstance(), IDS_WMISNAPIN_DESC, szBuf, 256) == 0)
  434. return E_FAIL;
  435. *lpDescription = (LPOLESTR)CoTaskMemAlloc((lstrlen(szBuf) + 1) * sizeof(OLECHAR));
  436. if (*lpDescription == NULL)
  437. return E_OUTOFMEMORY;
  438. ocscpy(*lpDescription, T2OLE(szBuf));
  439. return S_OK;
  440. }
  441. STDMETHOD(GetProvider)(LPOLESTR *lpName)
  442. {
  443. USES_CONVERSION;
  444. TCHAR szBuf[256] = {0};
  445. if (::LoadString(_Module.GetResourceInstance(), IDS_WMISNAPIN_PROVIDER, szBuf, 256) == 0)
  446. return E_FAIL;
  447. *lpName = (LPOLESTR)CoTaskMemAlloc((lstrlen(szBuf) + 1) * sizeof(OLECHAR));
  448. if (*lpName == NULL)
  449. return E_OUTOFMEMORY;
  450. ocscpy(*lpName, T2OLE(szBuf));
  451. return S_OK;
  452. }
  453. STDMETHOD(GetSnapinVersion)(LPOLESTR *lpVersion)
  454. {
  455. USES_CONVERSION;
  456. TCHAR szBuf[256] = {0};
  457. if (::LoadString(_Module.GetResourceInstance(), IDS_WMISNAPIN_VERSION, szBuf, 256) == 0)
  458. return E_FAIL;
  459. *lpVersion = (LPOLESTR)CoTaskMemAlloc((lstrlen(szBuf) + 1) * sizeof(OLECHAR));
  460. if (*lpVersion == NULL)
  461. return E_OUTOFMEMORY;
  462. ocscpy(*lpVersion, T2OLE(szBuf));
  463. return S_OK;
  464. }
  465. STDMETHOD(GetSnapinImage)(HICON *hAppIcon)
  466. {
  467. *hAppIcon = LoadIcon(_Module.GetResourceInstance(),
  468. MAKEINTRESOURCE(IDI_WMICNTL));
  469. return S_OK;
  470. }
  471. STDMETHOD(GetStaticFolderImage)(HBITMAP *hSmallImage,
  472. HBITMAP *hSmallImageOpen,
  473. HBITMAP *hLargeImage,
  474. COLORREF *cMask)
  475. {
  476. *hSmallImageOpen = LoadBitmap(_Module.GetResourceInstance(),
  477. MAKEINTRESOURCE(IDB_WMISNAPIN_16));
  478. *hSmallImage = LoadBitmap(_Module.GetResourceInstance(),
  479. MAKEINTRESOURCE(IDB_WMISNAPIN_16));
  480. *hLargeImage = LoadBitmap(_Module.GetResourceInstance(),
  481. MAKEINTRESOURCE(IDB_WMISNAPIN_32));
  482. *cMask = RGB(255,255,255);
  483. return S_OK;
  484. }
  485. };
  486. #endif