Source code of Windows XP (NT5)
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.

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