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.

342 lines
11 KiB

  1. //==============================================================;
  2. //
  3. // This source code is only intended as a supplement to existing Microsoft documentation.
  4. //
  5. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  6. // KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. // IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  8. // PURPOSE.
  9. //
  10. // Copyright (C) 1999 Microsoft Corporation. All Rights Reserved.
  11. //==============================================================;
  12. #include "stdafx.h"
  13. #include "ExtSnap.h"
  14. #include "ClassExtSnap.h"
  15. #include "Comp.h"
  16. #include "DataObj.h"
  17. #include "globals.h"
  18. #include "resource.h"
  19. #include "node1.h"
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CClassExtSnap
  22. //Here are the definitions for the clipboard formats that the
  23. //CClassExtSnap needs to be aware of for extending Computer Management
  24. #define _T_CCF_SNAPIN_CLASSID _T("CCF_SNAPIN_CLASSID")
  25. #define _T_MMC_SNAPIN_MACHINE_NAME _T("MMC_SNAPIN_MACHINE_NAME")
  26. #define _T_CCF_NODETYPE _T("CCF_NODETYPE")
  27. UINT CClassExtSnap::s_cfSnapInCLSID = RegisterClipboardFormat(_T_CCF_SNAPIN_CLASSID);
  28. UINT CClassExtSnap::s_cfMachineName = RegisterClipboardFormat (_T_MMC_SNAPIN_MACHINE_NAME);
  29. UINT CClassExtSnap::s_cfNodeType = RegisterClipboardFormat(_T_CCF_NODETYPE);
  30. const GUID CClassExtSnap::structuuidNodetypeServerApps = { 0x476e6449, 0xaaff, 0x11d0, { 0xb9, 0x44, 0x0, 0xc0, 0x4f, 0xd8, 0xd5, 0xb0 } };
  31. HBITMAP CClassExtSnap::m_pBMapSm = NULL;
  32. HBITMAP CClassExtSnap::m_pBMapLg = NULL;
  33. CClassExtSnap::CClassExtSnap()
  34. : m_cref(0), bExpanded(FALSE), m_ipConsoleNameSpace2(NULL), m_ipConsole2(NULL)
  35. {
  36. if (NULL == m_pBMapSm || NULL == m_pBMapLg)
  37. LoadBitmaps();
  38. }
  39. CClassExtSnap::~CClassExtSnap()
  40. {
  41. }
  42. ///////////////////////////////
  43. // Interface IComponentData
  44. ///////////////////////////////
  45. HRESULT CClassExtSnap::Initialize(
  46. /* [in] */ LPUNKNOWN pUnknown)
  47. {
  48. HRESULT hr;
  49. //
  50. // Get pointer to name space interface
  51. //
  52. hr = pUnknown->QueryInterface(IID_IConsoleNameSpace2, (void **)&m_ipConsoleNameSpace2);
  53. _ASSERT( S_OK == hr );
  54. //
  55. // Get pointer to console interface
  56. //
  57. hr = pUnknown->QueryInterface(IID_IConsole2, (void **)&m_ipConsole2);
  58. _ASSERT( S_OK == hr );
  59. IImageList *pImageList;
  60. m_ipConsole2->QueryScopeImageList(&pImageList);
  61. _ASSERT( S_OK == hr );
  62. hr = pImageList->ImageListSetStrip( (long *)m_pBMapSm, // pointer to a handle
  63. (long *)m_pBMapLg, // pointer to a handle
  64. 0, // index of the first image in the strip
  65. RGB(0, 128, 128) // color of the icon mask
  66. );
  67. _ASSERT( S_OK == hr );
  68. pImageList->Release();
  69. _ASSERT( S_OK == hr );
  70. return hr;
  71. }
  72. HRESULT CClassExtSnap::CreateComponent(
  73. /* [out] */ LPCOMPONENT __RPC_FAR *ppComponent)
  74. {
  75. *ppComponent = NULL;
  76. CComponent *pComponent = new CComponent(this);
  77. if (NULL == pComponent)
  78. return E_OUTOFMEMORY;
  79. return pComponent->QueryInterface(IID_IComponent, (void **)ppComponent);
  80. }
  81. HRESULT CClassExtSnap::Notify(
  82. /* [in] */ LPDATAOBJECT lpDataObject,
  83. /* [in] */ MMC_NOTIFY_TYPE event,
  84. /* [in] */ LPARAM arg,
  85. /* [in] */ LPARAM param)
  86. {
  87. MMCN_Crack(TRUE, lpDataObject, this, NULL, event, arg, param);
  88. HRESULT hr = S_FALSE;
  89. if (NULL == lpDataObject)
  90. return hr;
  91. switch (event)
  92. {
  93. case MMCN_EXPAND:
  94. {
  95. _TCHAR szMachineName[MAX_PATH]; //Current machine name.
  96. //Your child scope items should cache this
  97. GUID myGuid;
  98. // extract GUID of the the currently selected node type from the data object
  99. hr = ExtractObjectTypeGUID(lpDataObject, &myGuid);
  100. _ASSERT( S_OK == hr );
  101. // compare node type GUIDs of currently selected node and the node type
  102. // we want to extend. If they are are equal, currently selected node
  103. // is the type we want to extend, so we add our items underneath it
  104. if (IsEqualGUID(myGuid, getPrimaryNodeType()))
  105. {
  106. //Get the current targeted machine's name using the MMC_SNAPIN_MACHINE_NAME
  107. //clipboard format.
  108. //Note that each time the user retargets Computer Management, MMC will
  109. //send the MMCN_EXPAND notification to the SAME IComponentData instance.
  110. //Therefore, szMachineName always hold the current machine name.
  111. hr = ExtractString(lpDataObject, s_cfMachineName, szMachineName, (MAX_PATH+1)*sizeof(WCHAR) );
  112. hr = CreateChildNode(m_ipConsoleNameSpace2, m_ipConsole2, (HSCOPEITEM)param, szMachineName);
  113. }
  114. else
  115. // currently selected node is one of ours instead
  116. {
  117. CDelegationBase *base = GetOurDataObject(lpDataObject)->GetBaseNodeObject();
  118. hr = base->OnExpand(m_ipConsoleNameSpace2, m_ipConsole2, (HSCOPEITEM)param);
  119. }
  120. break;
  121. }
  122. case MMCN_REMOVE_CHILDREN:
  123. for (int n = 0; n < NUMBER_OF_CHILDREN; n++)
  124. if (children[n]) {
  125. delete children[n];
  126. children[n] = NULL;
  127. }
  128. hr = S_OK;
  129. break;
  130. }
  131. return hr;
  132. }
  133. HRESULT CClassExtSnap::Destroy( void)
  134. {
  135. // Free interfaces
  136. if (m_ipConsoleNameSpace2) {
  137. m_ipConsoleNameSpace2->Release();
  138. m_ipConsoleNameSpace2 = NULL;
  139. }
  140. if (m_ipConsole2) {
  141. m_ipConsole2->Release();
  142. m_ipConsole2 = NULL;
  143. }
  144. return S_OK;
  145. }
  146. HRESULT CClassExtSnap::QueryDataObject(
  147. /* [in] */ MMC_COOKIE cookie,
  148. /* [in] */ DATA_OBJECT_TYPES type,
  149. /* [out] */ LPDATAOBJECT *ppDataObject)
  150. {
  151. CDataObject *pObj = NULL;
  152. //cookie always != 0 for namespace extensions)
  153. //if (cookie == 0) //static node
  154. // pObj = new CDataObject((MMC_COOKIE)this, type);
  155. //else
  156. pObj = new CDataObject(cookie, type);
  157. if (!pObj)
  158. return E_OUTOFMEMORY;
  159. pObj->QueryInterface(IID_IDataObject, (void **)ppDataObject);
  160. return S_OK;
  161. }
  162. HRESULT CClassExtSnap::GetDisplayInfo(
  163. /* [out][in] */ SCOPEDATAITEM *pScopeDataItem)
  164. {
  165. LPOLESTR pszW = NULL;
  166. HRESULT hr = S_FALSE;
  167. // if they are asking for the SDI_STR we have one of those to give
  168. if (pScopeDataItem->lParam) {
  169. CDelegationBase *base = (CDelegationBase *)pScopeDataItem->lParam;
  170. if (pScopeDataItem->mask & SDI_STR) {
  171. LPCTSTR pszT = base->GetDisplayName();
  172. AllocOleStr(&pszW, (LPTSTR)pszT);
  173. pScopeDataItem->displayname = pszW;
  174. }
  175. if (pScopeDataItem->mask & SDI_IMAGE) {
  176. pScopeDataItem->nImage = base->GetBitmapIndex();
  177. }
  178. hr = S_OK;
  179. }
  180. return hr;
  181. }
  182. HRESULT CClassExtSnap::CompareObjects(
  183. /* [in] */ LPDATAOBJECT lpDataObjectA,
  184. /* [in] */ LPDATAOBJECT lpDataObjectB)
  185. {
  186. return S_FALSE;
  187. }
  188. ///////////////////////////////
  189. // CClassExtSnap::CreateChildNode
  190. ///////////////////////////////
  191. HRESULT CClassExtSnap::CreateChildNode(IConsoleNameSpace *pConsoleNameSpace,
  192. IConsole *pConsole, HSCOPEITEM parent, _TCHAR *pszMachineName)
  193. {
  194. _ASSERT(NULL != pszMachineName);
  195. if (!bExpanded) {
  196. //first create the CNode1 objects, one for each inserted item
  197. for (int i = 0; i < NUMBER_OF_CHILDREN; i++) {
  198. children[i] = new CNode1(i + 1, pszMachineName);
  199. }
  200. //now fill an SCOPEDATAITEM for each item and then insert it
  201. SCOPEDATAITEM sdi;
  202. // create the child nodes, then expand them
  203. for (int n = 0; n < NUMBER_OF_CHILDREN; n++) {
  204. ZeroMemory(&sdi, sizeof(SCOPEDATAITEM) );
  205. sdi.mask = SDI_STR | // Displayname is valid
  206. SDI_PARAM | // lParam is valid
  207. SDI_IMAGE | // nImage is valid
  208. SDI_OPENIMAGE | // nOpenImage is valid
  209. SDI_PARENT | // relativeID is valid
  210. SDI_CHILDREN; // cChildren is valid
  211. sdi.relativeID = (HSCOPEITEM)parent;
  212. sdi.nImage = children[n]->GetBitmapIndex();
  213. sdi.nOpenImage = INDEX_OPENFOLDER;
  214. sdi.displayname = MMC_CALLBACK;
  215. sdi.lParam = (LPARAM)children[n]; // The cookie
  216. sdi.cChildren = 0;
  217. HRESULT hr = pConsoleNameSpace->InsertItem( &sdi );
  218. _ASSERT( SUCCEEDED(hr) );
  219. }
  220. }
  221. return S_OK;
  222. }
  223. HRESULT CClassExtSnap::ExtractData( IDataObject* piDataObject,
  224. CLIPFORMAT cfClipFormat,
  225. BYTE* pbData,
  226. DWORD cbData )
  227. {
  228. HRESULT hr = S_OK;
  229. FORMATETC formatetc = {cfClipFormat, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
  230. STGMEDIUM stgmedium = {TYMED_HGLOBAL, NULL};
  231. stgmedium.hGlobal = ::GlobalAlloc(GPTR, cbData);
  232. do // false loop
  233. {
  234. if (NULL == stgmedium.hGlobal)
  235. {
  236. hr = E_OUTOFMEMORY;
  237. break;
  238. }
  239. hr = piDataObject->GetDataHere( &formatetc, &stgmedium );
  240. if ( FAILED(hr) )
  241. {
  242. break;
  243. }
  244. BYTE* pbNewData = reinterpret_cast<BYTE*>(stgmedium.hGlobal);
  245. if (NULL == pbNewData)
  246. {
  247. hr = E_UNEXPECTED;
  248. break;
  249. }
  250. ::memcpy( pbData, pbNewData, cbData );
  251. } while (FALSE); // false loop
  252. if (NULL != stgmedium.hGlobal)
  253. {
  254. ::GlobalFree(stgmedium.hGlobal);
  255. }
  256. return hr;
  257. } // ExtractData()
  258. HRESULT CClassExtSnap::ExtractString( IDataObject *piDataObject,
  259. CLIPFORMAT cfClipFormat,
  260. _TCHAR *pstr,
  261. DWORD cchMaxLength)
  262. {
  263. return ExtractData( piDataObject, cfClipFormat, (PBYTE)pstr, cchMaxLength );
  264. }
  265. HRESULT CClassExtSnap::ExtractSnapInCLSID( IDataObject* piDataObject, CLSID* pclsidSnapin )
  266. {
  267. return ExtractData( piDataObject, s_cfSnapInCLSID, (PBYTE)pclsidSnapin, sizeof(CLSID) );
  268. }
  269. HRESULT CClassExtSnap::ExtractObjectTypeGUID( IDataObject* piDataObject, GUID* pguidObjectType )
  270. {
  271. return ExtractData( piDataObject, s_cfNodeType, (PBYTE)pguidObjectType, sizeof(GUID) );
  272. }