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.

372 lines
10 KiB

  1. // stdcdata.cpp : Implementation of CComponentData
  2. #include "stdcdata.h"
  3. #include "guidhelp.h" // ExtractObjectTypeGUID
  4. #include "stddtobj.h" // CDataObject::m_CFRawCookie
  5. #include "stdrsrc.h" // IDS_FRAMEWORK_*
  6. //
  7. // CComponentData
  8. //
  9. CComponentData::CComponentData()
  10. : m_pConsole( NULL ),
  11. m_pConsoleNameSpace( NULL )
  12. {
  13. }
  14. CComponentData::~CComponentData()
  15. {
  16. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  17. ASSERT(NULL == m_pConsole && NULL == m_pConsoleNameSpace); // object should be Destroy()ed before being Release()d
  18. SAFE_RELEASE( m_pConsole );
  19. SAFE_RELEASE( m_pConsoleNameSpace );
  20. }
  21. HRESULT CComponentData::OnNotifyExpand(LPDATAOBJECT /*lpDataObject*/, BOOL /*bExpanding*/, HSCOPEITEM /*hParent*/)
  22. {
  23. return S_OK;
  24. }
  25. HRESULT CComponentData::OnNotifyPreload(LPDATAOBJECT /*lpDataObject*/, HSCOPEITEM /*hRootScopeItem*/)
  26. {
  27. return S_OK;
  28. }
  29. HRESULT CComponentData::OnNotifyRename(LPDATAOBJECT /*lpDataObject*/, LPARAM arg, LPARAM /*param*/)
  30. {
  31. // by default, do not allow rename
  32. return (0 == arg) ? S_FALSE : S_OK;
  33. }
  34. HRESULT CComponentData::OnNotifyDelete(LPDATAOBJECT /*lpDataObject*/)
  35. {
  36. ASSERT(FALSE); // Why did you enable the Delete command if you won't handle it?
  37. return S_OK;
  38. }
  39. HRESULT CComponentData::OnNotifyRelease(LPDATAOBJECT /*lpDataObject*/, HSCOPEITEM /*hItem*/)
  40. {
  41. ASSERT(FALSE); // You will leak memory if you don't handle this
  42. return S_OK;
  43. }
  44. /////////////////////////////////////////////////////////////////////
  45. // Virtual function called by CComponentData::IComponentData::Notify(MMCN_PROPERTY_CHANGE)
  46. // OnPropertyChange() is generated by MMCPropertyChangeNotify( param )
  47. HRESULT CComponentData::OnPropertyChange( LPARAM /*param*/ )
  48. {
  49. return S_OK;
  50. }
  51. /*
  52. * IComponentData
  53. */
  54. STDMETHODIMP CComponentData::Initialize(LPUNKNOWN pUnknown)
  55. {
  56. MFC_TRY;
  57. // ISSUE-2002/03/28-JonN handle NULL parameter
  58. ASSERT(pUnknown != NULL);
  59. // MMC should only call ::Initialize once!
  60. ASSERT(NULL == m_pConsoleNameSpace);
  61. SAFE_RELEASE( m_pConsoleNameSpace );
  62. HRESULT hr = pUnknown->QueryInterface(IID_IConsoleNameSpace,
  63. reinterpret_cast<void**>(&m_pConsoleNameSpace));
  64. ASSERT( SUCCEEDED(hr) );
  65. // add the images for the scope tree
  66. // ISSUE-2002/03/28-JonN use smartpointer
  67. LPIMAGELIST lpScopeImage = NULL;
  68. ASSERT(NULL == m_pConsole);
  69. SAFE_RELEASE( m_pConsole );
  70. hr = pUnknown->QueryInterface(IID_IConsole, reinterpret_cast<void**>(&m_pConsole));
  71. ASSERT( SUCCEEDED(hr) );
  72. hr = m_pConsole->QueryScopeImageList(&lpScopeImage);
  73. ASSERT( SUCCEEDED(hr) );
  74. hr = LoadIcons(lpScopeImage, FALSE);
  75. ASSERT( SUCCEEDED(hr) );
  76. lpScopeImage->Release();
  77. MFC_CATCH;
  78. return S_OK;
  79. }
  80. STDMETHODIMP CComponentData::Notify(LPDATAOBJECT lpDataObject, MMC_NOTIFY_TYPE event, LPARAM arg, LPARAM param)
  81. {
  82. HRESULT hr = S_OK;
  83. MFC_TRY;
  84. switch(event)
  85. {
  86. case MMCN_EXPAND:
  87. hr = OnNotifyExpand( lpDataObject, (BOOL)arg, (HSCOPEITEM)param );
  88. break;
  89. case MMCN_RENAME:
  90. hr = OnNotifyRename( lpDataObject, arg, param );
  91. break;
  92. case MMCN_DELETE:
  93. hr = OnNotifyDelete( lpDataObject );
  94. break;
  95. case MMCN_REMOVE_CHILDREN:
  96. hr = OnNotifyRelease( lpDataObject, arg );
  97. break;
  98. case MMCN_PRELOAD:
  99. hr = OnNotifyPreload (lpDataObject, (HSCOPEITEM) arg);
  100. break;
  101. case MMCN_PROPERTY_CHANGE:
  102. // CODEWORK arg is "fScopePane", should this be passed on?
  103. hr = OnPropertyChange( param );
  104. break;
  105. default:
  106. TRACE1("INFO: CComponentData::Notify () - Unknown Event %d.\n", event); // add new method for this notification
  107. break;
  108. }
  109. MFC_CATCH;
  110. return hr;
  111. }
  112. STDMETHODIMP CComponentData::Destroy()
  113. {
  114. MFC_TRY;
  115. SAFE_RELEASE(m_pConsoleNameSpace);
  116. SAFE_RELEASE(m_pConsole);
  117. MFC_CATCH;
  118. return S_OK;
  119. }
  120. STDMETHODIMP CComponentData::GetDisplayInfo(SCOPEDATAITEM* pScopeDataItem)
  121. {
  122. MFC_TRY;
  123. // ISSUE-2002/03/28-JonN handle pSDI==NULL and pSDI->lParam==NULL
  124. // WARNING cookie cast
  125. CCookie* pcookie = reinterpret_cast<CCookie*>(pScopeDataItem->lParam);
  126. ASSERT(NULL != pcookie);
  127. ASSERT( NULL != pScopeDataItem ); // result items never have NULL cookie
  128. if (SDI_STR & pScopeDataItem->mask)
  129. {
  130. pScopeDataItem->displayname = QueryResultColumnText( *pcookie, 0 );
  131. if ( NULL == pScopeDataItem->displayname )
  132. pScopeDataItem->displayname = L""; // just in case
  133. }
  134. if ( (SDI_IMAGE|SDI_OPENIMAGE) & pScopeDataItem->mask )
  135. {
  136. pScopeDataItem->nImage = QueryImage(
  137. *pcookie, !!(SDI_OPENIMAGE & pScopeDataItem->mask) );
  138. }
  139. MFC_CATCH;
  140. return S_OK;
  141. /*
  142. ASSERT(pScopeDataItem->mask == TVIF_TEXT);
  143. pScopeDataItem->displayname = QueryResultColumnText(*pcookie,0);
  144. ASSERT(pScopeDataItem->displayname != NULL);
  145. return S_OK;
  146. */
  147. }
  148. STDMETHODIMP CComponentData::CompareObjects(
  149. LPDATAOBJECT lpDataObjectA,
  150. LPDATAOBJECT lpDataObjectB)
  151. {
  152. int nResult = COMPARESIMILARCOOKIE_FULL; // a full cookie comparison is desired.
  153. MFC_TRY;
  154. GUID guidA, guidB;
  155. HRESULT hr = ::ExtractObjectTypeGUID( lpDataObjectA, &guidA );
  156. if ( FAILED(hr) )
  157. return hr;
  158. hr = ::ExtractObjectTypeGUID( lpDataObjectB, &guidB );
  159. if ( FAILED(hr) )
  160. return hr;
  161. if ( 0 != ::memcmp( &guidA, &guidB, sizeof(GUID) ) )
  162. return S_FALSE; // different nodetypes
  163. // If the two nodetypes are the same, both of these objects
  164. // must belong to this snapin
  165. // Extract cookies
  166. // WARNING cookie cast
  167. CCookie* pcookieA = NULL;
  168. hr = ExtractData( lpDataObjectA,
  169. CDataObject::m_CFRawCookie,
  170. &pcookieA,
  171. sizeof(pcookieA) );
  172. if ( FAILED(hr) )
  173. {
  174. ASSERT( FALSE );
  175. return hr;
  176. }
  177. pcookieA = ActiveBaseCookie(pcookieA);
  178. // WARNING cookie cast
  179. CCookie* pcookieB = NULL;
  180. hr = ExtractData( lpDataObjectB,
  181. CDataObject::m_CFRawCookie,
  182. &pcookieB,
  183. sizeof(pcookieB) );
  184. if ( FAILED(hr) )
  185. {
  186. ASSERT( FALSE );
  187. return hr;
  188. }
  189. pcookieB = ActiveBaseCookie(pcookieB);
  190. // Compare cookies
  191. if (pcookieA == pcookieB)
  192. return S_OK;
  193. hr = pcookieA->CompareSimilarCookies( pcookieB, &nResult );
  194. if( FAILED(hr) )
  195. return hr;
  196. MFC_CATCH;
  197. return (0 == nResult) ? S_OK : S_FALSE;
  198. }
  199. int
  200. GetErrorMsg(
  201. IN DWORD dwError,
  202. OUT PTSTR* pptzMsg
  203. )
  204. {
  205. // ISSUE-2002/03/28-JonN check for NULL ptr
  206. ASSERT(dwError != ERROR_SUCCESS);
  207. int cch = FormatMessage(
  208. FORMAT_MESSAGE_ALLOCATE_BUFFER
  209. | FORMAT_MESSAGE_FROM_SYSTEM,
  210. NULL, dwError,
  211. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  212. (PTSTR)pptzMsg, 0, NULL);
  213. if (0 == cch)
  214. {
  215. static HMODULE g_hNetMod = 0;
  216. if (0 == g_hNetMod)
  217. g_hNetMod = GetModuleHandle (L"netmsg.dll");
  218. if (g_hNetMod)
  219. cch = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
  220. | FORMAT_MESSAGE_FROM_HMODULE,
  221. g_hNetMod, dwError,
  222. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  223. (PTSTR)pptzMsg, 0, NULL);
  224. }
  225. return cch;
  226. }
  227. INT CComponentData::DoPopup( INT nResourceID, DWORD dwErrorNumber, LPCTSTR pszInsertionString, UINT fuStyle )
  228. {
  229. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  230. CString strTitle;
  231. VERIFY( strTitle.LoadString(
  232. (MB_ICONSTOP == (fuStyle & 0x000000F0))
  233. ? IDS_FRAMEWORK_TITLE_ERROR : IDS_FRAMEWORK_TITLE_WARNING ) );
  234. CString strError;
  235. if (0 != dwErrorNumber || NULL != pszInsertionString)
  236. {
  237. CString strFormat;
  238. VERIFY( strFormat.LoadString( nResourceID ) );
  239. // ISSUE-2002/03/28-JonN FormatMessage would be safer than Format
  240. strError.Format( strFormat, dwErrorNumber, pszInsertionString );
  241. if (0 != dwErrorNumber)
  242. {
  243. PTSTR pszErrorMsg = NULL;
  244. int cch = GetErrorMsg(dwErrorNumber, &pszErrorMsg);
  245. if (cch > 0)
  246. {
  247. strError += _T("\n\n");
  248. strError += pszErrorMsg;
  249. LocalFree(pszErrorMsg);
  250. }
  251. }
  252. }
  253. else
  254. {
  255. VERIFY( strError.LoadString( nResourceID ) );
  256. }
  257. INT iRetval = 0;
  258. USES_CONVERSION;
  259. HRESULT hr = m_pConsole->MessageBox(
  260. T2OLE(const_cast<LPTSTR>((LPCTSTR)strError)),
  261. T2OLE(const_cast<LPTSTR>((LPCTSTR)strTitle)),
  262. fuStyle,
  263. &iRetval );
  264. ASSERT( SUCCEEDED(hr) );
  265. return iRetval;
  266. }
  267. /////////////////////////////////////////////////////////////////////////////
  268. // CMyComputerComponentData::ISnapinHelp members
  269. // Help on IComponentData just returns the file and no particular topic
  270. STDMETHODIMP CComponentData::GetHelpTopic(LPOLESTR* lpCompiledHelpFile)
  271. {
  272. MFC_TRY;
  273. if (lpCompiledHelpFile == NULL)
  274. return E_INVALIDARG;
  275. CString szHelpFilePath;
  276. HRESULT hr = GetHtmlHelpFilePath( szHelpFilePath );
  277. if ( FAILED(hr) )
  278. return hr;
  279. *lpCompiledHelpFile = reinterpret_cast <LPOLESTR> (
  280. CoTaskMemAlloc ((szHelpFilePath.GetLength () + 1) * sizeof (wchar_t)));
  281. if ( NULL == *lpCompiledHelpFile )
  282. return E_OUTOFMEMORY;
  283. // ISSUE-2002/03/28-JonN remove USES_CONVERSION
  284. USES_CONVERSION;
  285. wcscpy (*lpCompiledHelpFile, T2OLE ((LPTSTR)(LPCTSTR) szHelpFilePath));
  286. MFC_CATCH;
  287. return S_OK;
  288. }
  289. STDMETHODIMP CComponentData::GetLinkedTopics(LPOLESTR* /*lpCompiledHelpFiles*/)
  290. {
  291. return E_NOTIMPL;
  292. }
  293. HRESULT CComponentData::GetHtmlHelpFilePath( CString& strref ) const
  294. {
  295. if ( GetHtmlHelpFileName().IsEmpty () )
  296. return E_NOTIMPL;
  297. UINT nLen = ::GetSystemWindowsDirectory (strref.GetBufferSetLength(2 * MAX_PATH), 2 * MAX_PATH);
  298. strref.ReleaseBuffer();
  299. if (0 == nLen)
  300. {
  301. ASSERT(FALSE);
  302. return E_FAIL;
  303. }
  304. strref += L"\\help\\";
  305. strref += GetHtmlHelpFileName();
  306. return S_OK;
  307. }
  308. /* no taskpads
  309. STDMETHODIMP CComponentData::ExpandAndGet(
  310. HSCOPEITEM hsiStartFrom,
  311. LPDATAOBJECT pDataObject,
  312. HSCOPEITEM* phScopeItem )
  313. {
  314. ASSERT(FALSE);
  315. return E_NOTIMPL;
  316. }
  317. */