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.

934 lines
27 KiB

  1. // compdata.cpp : Implementation of CMyComputerComponentData
  2. #include "stdafx.h"
  3. #include <lmerr.h> // For Lan Manager API error codes and return value types.
  4. #include <lmcons.h> // For Lan Manager API constants.
  5. #include <lmapibuf.h> // For NetApiBufferFree.
  6. #include <lmdfs.h> // For DFS APIs.
  7. #include <lmserver.h> // For getting a domain of a server.
  8. #include "macros.h"
  9. USE_HANDLE_MACROS("MMCFMGMT(compdata.cpp)")
  10. #include "dataobj.h"
  11. #include "compdata.h"
  12. #include "cookie.h"
  13. #include "snapmgr.h"
  14. #include "stdutils.h" // IsLocalComputername
  15. #include "chooser2.h"
  16. #ifdef _DEBUG
  17. #define new DEBUG_NEW
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21. #include "stdcdata.cpp" // CComponentData implementation
  22. #include "chooser2.cpp" // CHOOSER2_PickTargetComputer implementation
  23. // Helper function to convert message in NETMSG.DLL
  24. int DisplayNetMsgError (
  25. HWND hWndParent,
  26. const CString& computerName,
  27. NET_API_STATUS dwErr,
  28. CString& displayedMessage);
  29. //
  30. // CMyComputerComponentData
  31. //
  32. CMyComputerComponentData::CMyComputerComponentData()
  33. : m_pRootCookie( NULL )
  34. , m_dwFlagsPersist( 0 )
  35. , m_fAllowOverrideMachineName( FALSE )
  36. , m_bCannotConnect (false)
  37. , m_bMessageView (false)
  38. {
  39. //
  40. // We must refcount the root cookie, since a dataobject for it
  41. // might outlive the IComponentData. JonN 9/2/97
  42. //
  43. m_pRootCookie = new CMyComputerCookie( MYCOMPUT_COMPUTER );
  44. ASSERT(NULL != m_pRootCookie);
  45. // JonN 10/27/98 All CRefcountedObject's start with refcount==1
  46. // m_pRootCookie->AddRef();
  47. SetHtmlHelpFileName (L"compmgmt.chm");
  48. }
  49. CMyComputerComponentData::~CMyComputerComponentData()
  50. {
  51. m_pRootCookie->Release();
  52. m_pRootCookie = NULL;
  53. }
  54. DEFINE_FORWARDS_MACHINE_NAME( CMyComputerComponentData, m_pRootCookie )
  55. CCookie& CMyComputerComponentData::QueryBaseRootCookie()
  56. {
  57. // ISSUE-2002/02/25-JonN handle better -- could return non-NULL bad ptr
  58. ASSERT(NULL != m_pRootCookie);
  59. return (CCookie&)*m_pRootCookie;
  60. }
  61. STDMETHODIMP CMyComputerComponentData::CreateComponent(LPCOMPONENT* ppComponent)
  62. {
  63. MFC_TRY;
  64. // ISSUE 2002/02/25-JonN should be TEST_NON_NULL_PTR_PARAMS
  65. ASSERT(ppComponent != NULL);
  66. CComObject<CMyComputerComponent>* pObject;
  67. CComObject<CMyComputerComponent>::CreateInstance(&pObject);
  68. // ISSUE 2002/02/25-JonN handle pObject == NULL
  69. ASSERT(pObject != NULL);
  70. pObject->SetComponentDataPtr( (CMyComputerComponentData*)this );
  71. return pObject->QueryInterface(IID_IComponent,
  72. reinterpret_cast<void**>(ppComponent));
  73. MFC_CATCH;
  74. }
  75. HRESULT CMyComputerComponentData::LoadIcons(LPIMAGELIST pImageList, BOOL /*fLoadLargeIcons*/)
  76. {
  77. AFX_MANAGE_STATE(AfxGetStaticModuleState( )); // 2002/03/22-JonN 572859
  78. HINSTANCE hInstance = AfxGetInstanceHandle();
  79. // ISSUE 2002/02/25-JonN handle hInstance == NULL
  80. ASSERT(hInstance != NULL);
  81. // Structure to map a Resource ID to an index of icon
  82. struct RESID2IICON
  83. {
  84. UINT uIconId; // Icon resource ID
  85. int iIcon; // Index of the icon in the image list
  86. };
  87. const static RESID2IICON rgzLoadIconList[] =
  88. {
  89. // Misc icons
  90. { IDI_COMPUTER, iIconComputer },
  91. { IDI_COMPFAIL, iIconComputerFail },
  92. { IDI_SYSTEMTOOLS, iIconSystemTools },
  93. { IDI_STORAGE, iIconStorage },
  94. { IDI_SERVERAPPS, iIconServerApps },
  95. { 0, 0} // Must be last
  96. };
  97. for (int i = 0; rgzLoadIconList[i].uIconId != 0; i++)
  98. {
  99. HICON hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(rgzLoadIconList[i].uIconId));
  100. ASSERT(NULL != hIcon && "Icon ID not found in resources");
  101. HRESULT hr = pImageList->ImageListSetIcon((PLONG_PTR)hIcon, rgzLoadIconList[i].iIcon);
  102. ASSERT(SUCCEEDED(hr) && "Unable to add icon to ImageList");
  103. }
  104. return S_OK;
  105. }
  106. CString g_strMyComputer;
  107. CString g_strSystemTools;
  108. CString g_strServerApps;
  109. CString g_strStorage;
  110. BOOL g_fScopeStringsLoaded = FALSE;
  111. void LoadGlobalCookieStrings()
  112. {
  113. if (!g_fScopeStringsLoaded )
  114. {
  115. g_fScopeStringsLoaded = TRUE;
  116. VERIFY( g_strMyComputer.LoadString(IDS_SCOPE_MYCOMPUTER) );
  117. VERIFY( g_strSystemTools.LoadString(IDS_SCOPE_SYSTEMTOOLS) );
  118. VERIFY( g_strServerApps.LoadString(IDS_SCOPE_SERVERAPPS) );
  119. VERIFY( g_strStorage.LoadString(IDS_SCOPE_STORAGE) );
  120. }
  121. }
  122. // returns TRUE iff the child nodes should be added
  123. // CODEWORK this is probably no longer necessary, we always return true
  124. bool CMyComputerComponentData::ValidateMachine(const CString &sName, bool bDisplayErr)
  125. {
  126. CWaitCursor waitCursor;
  127. int iRetVal = IDYES;
  128. SERVER_INFO_101* psvInfo101 = 0;
  129. DWORD dwr = ERROR_SUCCESS;
  130. DWORD dwServerType = SV_TYPE_NT;
  131. m_bMessageView = false;
  132. // passed-in name is not the same as local machine
  133. if ( !IsLocalComputername(sName) )
  134. {
  135. dwr = ::NetServerGetInfo((LPTSTR)(LPCTSTR)sName,
  136. 101, (LPBYTE*)&psvInfo101);
  137. if (dwr == ERROR_SUCCESS)
  138. {
  139. ASSERT( NULL != psvInfo101 );
  140. dwServerType = psvInfo101->sv101_type;
  141. ::NetApiBufferFree (psvInfo101);
  142. }
  143. if (bDisplayErr && (dwr != ERROR_SUCCESS || !(SV_TYPE_NT & dwServerType)) )
  144. {
  145. CString computerName (sName);
  146. if ( computerName.IsEmpty () )
  147. {
  148. // ISSUE 2002/02/25-JonN handle failure
  149. DWORD dwSize = MAX_COMPUTERNAME_LENGTH + 1 ;
  150. VERIFY (::GetComputerName (
  151. computerName.GetBufferSetLength (dwSize),
  152. &dwSize));
  153. computerName.ReleaseBuffer ();
  154. }
  155. else
  156. {
  157. // Strip off the leading whack-whack
  158. if ( computerName.Find (L"\\\\") == 0 )
  159. {
  160. computerName = computerName.GetBuffer (computerName.GetLength ()) + 2;
  161. computerName.ReleaseBuffer ();
  162. }
  163. }
  164. CString text;
  165. CString caption;
  166. bool bMessageDisplayed = false;
  167. switch (dwr)
  168. {
  169. case ERROR_NETWORK_UNREACHABLE:
  170. text.FormatMessage (IDS_CANT_CONNECT_TO_MACHINE_NETWORK_UNREACHABLE,
  171. computerName);
  172. break;
  173. case ERROR_NETNAME_DELETED:
  174. text.FormatMessage (IDS_CANT_CONNECT_TO_MACHINE_NETNAME_DELETED,
  175. computerName);
  176. break;
  177. case ERROR_SUCCESS:
  178. ASSERT( !(SV_TYPE_NT & dwServerType) );
  179. text.FormatMessage (IDS_CANT_CONNECT_TO_NON_NT_COMPUTER,
  180. computerName);
  181. dwr = ERROR_BAD_NETPATH;
  182. break;
  183. case ERROR_ACCESS_DENIED:
  184. // We will interpret this as success.
  185. return true;
  186. case ERROR_BAD_NETPATH:
  187. default:
  188. {
  189. HWND hWndParent = 0;
  190. m_pConsole->GetMainWindow (&hWndParent);
  191. iRetVal = DisplayNetMsgError (hWndParent, computerName,
  192. dwr, m_strMessageViewMsg);
  193. bMessageDisplayed = true;
  194. m_bMessageView = true;
  195. return false;
  196. }
  197. break;
  198. }
  199. if ( !bMessageDisplayed )
  200. {
  201. VERIFY (caption.LoadString (IDS_TASKPADTITLE_COMPUTER));
  202. m_pConsole->MessageBox (text, caption,
  203. MB_ICONINFORMATION | MB_YESNO, &iRetVal);
  204. }
  205. }
  206. }
  207. if (IDYES != iRetVal)
  208. {
  209. // revert to local computer focus
  210. QueryRootCookie().SetMachineName (L"");
  211. // Set the persistent name. If we are managing the local computer
  212. // this name should be empty.
  213. m_strMachineNamePersist = L"";
  214. VERIFY (SUCCEEDED (ChangeRootNodeName (L"")) );
  215. iRetVal = IDYES;
  216. dwr = ERROR_SUCCESS;
  217. }
  218. m_bCannotConnect = (ERROR_SUCCESS != dwr);
  219. // Change root node icon
  220. SCOPEDATAITEM item;
  221. ::ZeroMemory (&item, sizeof (SCOPEDATAITEM));
  222. item.mask = SDI_IMAGE | SDI_OPENIMAGE;
  223. item.nImage = (m_bCannotConnect) ? iIconComputerFail : iIconComputer;
  224. item.nOpenImage = item.nImage;
  225. item.ID = QueryBaseRootCookie ().m_hScopeItem;
  226. VERIFY (SUCCEEDED (m_pConsoleNameSpace->SetItem (&item)));
  227. return true;
  228. }
  229. HRESULT CMyComputerComponentData::OnNotifyExpand(LPDATAOBJECT lpDataObject, BOOL bExpanding, HSCOPEITEM hParent)
  230. {
  231. // ISSUE 2002/02/25-JonN handle NULL
  232. ASSERT( NULL != lpDataObject &&
  233. NULL != hParent &&
  234. NULL != m_pConsoleNameSpace );
  235. if (!bExpanding)
  236. return S_OK;
  237. //
  238. // CODEWORK This code will not work if My Computer becomes an extension,
  239. // since the RawCookie format will not be available.
  240. // WARNING cookie cast
  241. //
  242. CCookie* pBaseParentCookie = NULL;
  243. HRESULT hr = ExtractData( lpDataObject,
  244. CMyComputerDataObject::m_CFRawCookie,
  245. reinterpret_cast<PBYTE>(&pBaseParentCookie),
  246. sizeof(pBaseParentCookie) );
  247. ASSERT( SUCCEEDED(hr) );
  248. CMyComputerCookie* pParentCookie = ActiveCookie(pBaseParentCookie);
  249. // ISSUE 2002/02/25-JonN handle pParentCookie == NULL
  250. ASSERT( NULL != pParentCookie );
  251. // save the HSCOPEITEM of the root node
  252. if ( NULL == pParentCookie->m_hScopeItem )
  253. {
  254. pParentCookie->m_hScopeItem = hParent;
  255. // Ensure root node name is formatted correctly.
  256. CString machineName = pParentCookie->QueryNonNULLMachineName ();
  257. hr = ChangeRootNodeName (machineName);
  258. }
  259. else
  260. {
  261. ASSERT( pParentCookie->m_hScopeItem == hParent );
  262. }
  263. switch ( pParentCookie->m_objecttype )
  264. {
  265. // This node type has a child
  266. case MYCOMPUT_COMPUTER:
  267. break;
  268. // This node type has no children in this snapin but may have dynamic extensions
  269. case MYCOMPUT_SERVERAPPS:
  270. return ExpandServerApps( hParent, pParentCookie );
  271. // These node types have no children
  272. case MYCOMPUT_SYSTEMTOOLS:
  273. case MYCOMPUT_STORAGE:
  274. return S_OK;
  275. default:
  276. TRACE( "CMyComputerComponentData::EnumerateScopeChildren bad parent type\n" );
  277. ASSERT( FALSE );
  278. return S_OK;
  279. }
  280. if ( NULL == hParent ||
  281. !((pParentCookie->m_listScopeCookieBlocks).IsEmpty()) )
  282. {
  283. ASSERT(FALSE);
  284. return S_OK;
  285. }
  286. LoadGlobalCookieStrings();
  287. hr = AddScopeNodes (hParent, *pParentCookie);
  288. return S_OK;
  289. }
  290. ///////////////////////////////////////////////////////////////////////////////
  291. //
  292. // AddScopeNodes
  293. //
  294. // Purpose: Add the nodes that appear immediately below the root node
  295. //
  296. ///////////////////////////////////////////////////////////////////////////////
  297. HRESULT CMyComputerComponentData::AddScopeNodes (HSCOPEITEM hParent, CMyComputerCookie& rParentCookie)
  298. {
  299. if ( !(rParentCookie.m_listScopeCookieBlocks.IsEmpty()) )
  300. {
  301. return S_OK; // scope cookies already present
  302. }
  303. HRESULT hr = S_OK;
  304. LPCWSTR lpcszMachineName = rParentCookie.QueryNonNULLMachineName();
  305. // ISSUE 2002/02/25-JonN handle lpcszMachineName == NULL
  306. if ( ValidateMachine (lpcszMachineName, true) )
  307. {
  308. SCOPEDATAITEM tSDItem;
  309. ::ZeroMemory(&tSDItem,sizeof(tSDItem));
  310. tSDItem.mask = SDI_STR | SDI_IMAGE | SDI_OPENIMAGE | SDI_STATE | SDI_PARAM | SDI_PARENT;
  311. tSDItem.displayname = MMC_CALLBACK;
  312. tSDItem.relativeID = hParent;
  313. tSDItem.nState = 0;
  314. // Create new cookies
  315. CMyComputerCookie* pNewCookie = new CMyComputerCookie(
  316. MYCOMPUT_SYSTEMTOOLS,
  317. lpcszMachineName );
  318. // ISSUE 2002/02/25-JonN handle pNewCookie == NULL
  319. rParentCookie.m_listScopeCookieBlocks.AddHead(
  320. (CBaseCookieBlock*)pNewCookie );
  321. // WARNING cookie cast
  322. tSDItem.lParam = reinterpret_cast<LPARAM>((CCookie*)pNewCookie);
  323. tSDItem.nImage = QueryImage( *pNewCookie, FALSE );
  324. tSDItem.nOpenImage = QueryImage( *pNewCookie, TRUE );
  325. hr = m_pConsoleNameSpace->InsertItem(&tSDItem);
  326. ASSERT(SUCCEEDED(hr));
  327. pNewCookie->m_hScopeItem = tSDItem.ID;
  328. ASSERT( NULL != pNewCookie->m_hScopeItem );
  329. pNewCookie = new CMyComputerCookie(
  330. MYCOMPUT_STORAGE,
  331. lpcszMachineName );
  332. // ISSUE 2002/02/25-JonN handle pNewCookie == NULL
  333. rParentCookie.m_listScopeCookieBlocks.AddHead(
  334. (CBaseCookieBlock*)pNewCookie );
  335. // WARNING cookie cast
  336. tSDItem.lParam = reinterpret_cast<LPARAM>((CCookie*)pNewCookie);
  337. tSDItem.nImage = QueryImage( *pNewCookie, FALSE );
  338. tSDItem.nOpenImage = QueryImage( *pNewCookie, TRUE );
  339. hr = m_pConsoleNameSpace->InsertItem(&tSDItem);
  340. ASSERT(SUCCEEDED(hr));
  341. pNewCookie->m_hScopeItem = tSDItem.ID;
  342. ASSERT( NULL != pNewCookie->m_hScopeItem );
  343. pNewCookie = new CMyComputerCookie(
  344. MYCOMPUT_SERVERAPPS,
  345. lpcszMachineName );
  346. // ISSUE 2002/02/25-JonN handle pNewCookie == NULL
  347. rParentCookie.m_listScopeCookieBlocks.AddHead(
  348. (CBaseCookieBlock*)pNewCookie );
  349. // WARNING cookie cast
  350. tSDItem.lParam = reinterpret_cast<LPARAM>((CCookie*)pNewCookie);
  351. tSDItem.nImage = QueryImage( *pNewCookie, FALSE );
  352. tSDItem.nOpenImage = QueryImage( *pNewCookie, TRUE );
  353. hr = m_pConsoleNameSpace->InsertItem(&tSDItem);
  354. ASSERT(SUCCEEDED(hr));
  355. pNewCookie->m_hScopeItem = tSDItem.ID;
  356. ASSERT( NULL != pNewCookie->m_hScopeItem );
  357. }
  358. else
  359. hr = S_FALSE;
  360. return hr;
  361. }
  362. HRESULT CMyComputerComponentData::OnNotifyDelete(LPDATAOBJECT /*lpDataObject*/)
  363. {
  364. // CODEWORK The user hit the Delete key, I should deal with this
  365. return S_OK;
  366. }
  367. HRESULT CMyComputerComponentData::OnNotifyRelease(LPDATAOBJECT /*lpDataObject*/, HSCOPEITEM /*hItem*/)
  368. {
  369. // JonN 01/26/00: COMPMGMT is never an extension
  370. return S_OK;
  371. }
  372. HRESULT CMyComputerComponentData::OnNotifyPreload(LPDATAOBJECT /*lpDataObject*/, HSCOPEITEM hRootScopeItem)
  373. {
  374. ASSERT (m_fAllowOverrideMachineName);
  375. QueryBaseRootCookie ().m_hScopeItem = hRootScopeItem;
  376. CString machineName = QueryRootCookie ().QueryNonNULLMachineName();
  377. return ChangeRootNodeName (machineName);
  378. }
  379. // global space to store the string handed back to GetDisplayInfo()
  380. // CODEWORK should use "CComBSTR" for ANSI-ization
  381. CString g_strResultColumnText;
  382. BSTR CMyComputerComponentData::QueryResultColumnText(
  383. CCookie& basecookieref,
  384. int nCol )
  385. {
  386. CMyComputerCookie& cookieref = (CMyComputerCookie&)basecookieref;
  387. #ifndef UNICODE
  388. #error not ANSI-enabled
  389. #endif
  390. switch ( cookieref.m_objecttype )
  391. {
  392. case MYCOMPUT_COMPUTER:
  393. if (COLNUM_COMPUTER_NAME == nCol)
  394. return const_cast<BSTR>(((LPCTSTR)g_strMyComputer));
  395. break;
  396. case MYCOMPUT_SYSTEMTOOLS:
  397. if (COLNUM_COMPUTER_NAME == nCol)
  398. return const_cast<BSTR>(((LPCTSTR)g_strSystemTools));
  399. break;
  400. case MYCOMPUT_SERVERAPPS:
  401. if (COLNUM_COMPUTER_NAME == nCol)
  402. return const_cast<BSTR>(((LPCTSTR)g_strServerApps));
  403. break;
  404. case MYCOMPUT_STORAGE:
  405. if (COLNUM_COMPUTER_NAME == nCol)
  406. return const_cast<BSTR>(((LPCTSTR)g_strStorage));
  407. break;
  408. default:
  409. TRACE( "CMyComputerComponentData::EnumerateScopeChildren bad parent type\n" );
  410. ASSERT( FALSE );
  411. break;
  412. }
  413. return L"";
  414. }
  415. int CMyComputerComponentData::QueryImage(CCookie& basecookieref, BOOL /*fOpenImage*/)
  416. {
  417. CMyComputerCookie& cookieref = (CMyComputerCookie&)basecookieref;
  418. switch ( cookieref.m_objecttype )
  419. {
  420. case MYCOMPUT_COMPUTER:
  421. if ( m_bCannotConnect )
  422. return iIconComputerFail;
  423. else
  424. return iIconComputer;
  425. case MYCOMPUT_SYSTEMTOOLS:
  426. return iIconSystemTools;
  427. case MYCOMPUT_SERVERAPPS:
  428. return iIconServerApps;
  429. case MYCOMPUT_STORAGE:
  430. return iIconStorage;
  431. default:
  432. TRACE( "CMyComputerComponentData::QueryImage bad parent type\n" );
  433. ASSERT( FALSE );
  434. break;
  435. }
  436. return iIconComputer;
  437. }
  438. ///////////////////////////////////////////////////////////////////////////////
  439. /// IExtendPropertySheet
  440. STDMETHODIMP CMyComputerComponentData::QueryPagesFor(LPDATAOBJECT pDataObject)
  441. {
  442. MFC_TRY;
  443. if (NULL == pDataObject)
  444. {
  445. ASSERT(FALSE);
  446. return E_POINTER;
  447. }
  448. DATA_OBJECT_TYPES dataobjecttype = CCT_SCOPE;
  449. HRESULT hr = ExtractData( pDataObject, CMyComputerDataObject::m_CFDataObjectType, (PBYTE)&dataobjecttype, sizeof(dataobjecttype) );
  450. if ( FAILED(hr) )
  451. return hr;
  452. if (CCT_SNAPIN_MANAGER == dataobjecttype)
  453. return S_OK; // Snapin Manager dialog
  454. CCookie* pBaseParentCookie = NULL;
  455. hr = ExtractData( pDataObject,
  456. CMyComputerDataObject::m_CFRawCookie,
  457. reinterpret_cast<PBYTE>(&pBaseParentCookie),
  458. sizeof(pBaseParentCookie) );
  459. ASSERT( SUCCEEDED(hr) );
  460. CMyComputerCookie* pParentCookie = ActiveCookie(pBaseParentCookie);
  461. ASSERT( NULL != pParentCookie );
  462. if ( MYCOMPUT_COMPUTER == pParentCookie->m_objecttype )
  463. return S_OK; // allow extensibility
  464. return S_FALSE;
  465. MFC_CATCH;
  466. }
  467. STDMETHODIMP CMyComputerComponentData::CreatePropertyPages(
  468. LPPROPERTYSHEETCALLBACK pCallBack,
  469. LONG_PTR /*handle*/, // This handle must be saved in the property page object to notify the parent when modified
  470. LPDATAOBJECT pDataObject)
  471. {
  472. MFC_TRY;
  473. if (NULL == pCallBack || NULL == pDataObject)
  474. {
  475. ASSERT(FALSE);
  476. return E_POINTER;
  477. }
  478. DATA_OBJECT_TYPES dataobjecttype = CCT_SCOPE;
  479. HRESULT hr = ExtractData( pDataObject, CMyComputerDataObject::m_CFDataObjectType, (PBYTE)&dataobjecttype, sizeof(dataobjecttype) );
  480. if (CCT_SNAPIN_MANAGER != dataobjecttype)
  481. {
  482. CCookie* pBaseParentCookie = NULL;
  483. hr = ExtractData( pDataObject,
  484. CMyComputerDataObject::m_CFRawCookie,
  485. reinterpret_cast<PBYTE>(&pBaseParentCookie),
  486. sizeof(pBaseParentCookie) );
  487. ASSERT( SUCCEEDED(hr) );
  488. CMyComputerCookie* pParentCookie = ActiveCookie(pBaseParentCookie);
  489. ASSERT( NULL != pParentCookie );
  490. if ( MYCOMPUT_COMPUTER == pParentCookie->m_objecttype )
  491. return S_OK; // allow extensibility
  492. return S_FALSE;
  493. }
  494. //
  495. // Note that once we have established that this is a CCT_SNAPIN_MANAGER cookie,
  496. // we don't care about its other properties. A CCT_SNAPIN_MANAGER cookie is
  497. // equivalent to a BOOL flag asking for the Node Properties page instead of a
  498. // managed object property page. JonN 10/9/96
  499. //
  500. CMyComputerGeneral * pPage = new CMyComputerGeneral();
  501. // ISSUE 2002/02/25-JonN handle pPage == NULL
  502. pPage->SetCaption(IDS_SCOPE_MYCOMPUTER);
  503. // Initialize state of object
  504. ASSERT(NULL != m_pRootCookie);
  505. pPage->InitMachineName(m_pRootCookie->QueryTargetServer());
  506. pPage->SetOutputBuffers(
  507. OUT &m_strMachineNamePersist,
  508. OUT &m_fAllowOverrideMachineName,
  509. OUT &m_pRootCookie->m_strMachineName); // Effective machine name
  510. HPROPSHEETPAGE hPage=CreatePropertySheetPage(&pPage->m_psp);
  511. hr = pCallBack->AddPage(hPage);
  512. ASSERT( SUCCEEDED(hr) );
  513. return S_OK;
  514. MFC_CATCH;
  515. }
  516. STDMETHODIMP CMyComputerComponentData::AddMenuItems(
  517. IDataObject* piDataObject,
  518. IContextMenuCallback* piCallback,
  519. long* pInsertionAllowed)
  520. {
  521. MFC_TRY;
  522. TRACE_METHOD(CMyComputerComponent,AddMenuItems);
  523. TEST_NONNULL_PTR_PARAM(piDataObject);
  524. TEST_NONNULL_PTR_PARAM(piCallback);
  525. TEST_NONNULL_PTR_PARAM(pInsertionAllowed);
  526. TRACE( "CMyComputerComponentData: extending menu\n" );
  527. CCookie* pBaseParentCookie = NULL;
  528. HRESULT hr = ExtractData (piDataObject,
  529. CMyComputerDataObject::m_CFRawCookie,
  530. reinterpret_cast<PBYTE>(&pBaseParentCookie),
  531. sizeof(pBaseParentCookie) );
  532. if ( FAILED (hr) )
  533. {
  534. ASSERT (FALSE);
  535. return S_OK;
  536. }
  537. CMyComputerCookie* pCookie = ActiveCookie (pBaseParentCookie);
  538. if ( !pCookie )
  539. {
  540. ASSERT (FALSE);
  541. return S_OK;
  542. }
  543. if ( *pInsertionAllowed & CCM_INSERTIONALLOWED_TOP )
  544. {
  545. if ( MYCOMPUT_COMPUTER == pCookie->m_objecttype &&
  546. QueryBaseRootCookie ().m_hScopeItem)
  547. {
  548. hr = ::LoadAndAddMenuItem (piCallback,
  549. IDM_CHANGE_COMPUTER_TOP, IDM_CHANGE_COMPUTER_TOP,
  550. CCM_INSERTIONPOINTID_PRIMARY_TOP,
  551. 0,
  552. AfxGetInstanceHandle (),
  553. _T("ChangeComputerTop") );
  554. // ISSUE 2002/02/25-JonN handle FAILED(hr)
  555. }
  556. }
  557. if ( *pInsertionAllowed & CCM_INSERTIONALLOWED_TASK )
  558. {
  559. if ( MYCOMPUT_COMPUTER == pCookie->m_objecttype &&
  560. QueryBaseRootCookie ().m_hScopeItem)
  561. {
  562. hr = ::LoadAndAddMenuItem (piCallback,
  563. IDM_CHANGE_COMPUTER_TASK, IDM_CHANGE_COMPUTER_TASK,
  564. CCM_INSERTIONPOINTID_PRIMARY_TASK,
  565. 0,
  566. AfxGetInstanceHandle (),
  567. _T("ChangeComputerTask") );
  568. // ISSUE 2002/02/25-JonN handle FAILED(hr)
  569. }
  570. }
  571. // ISSUE 2002/02/25-JonN don't return failure even if items not added
  572. return hr;
  573. MFC_CATCH;
  574. } // CMyComputerComponentData::AddMenuItems()
  575. STDMETHODIMP CMyComputerComponentData::Command(
  576. LONG lCommandID,
  577. IDataObject* piDataObject )
  578. {
  579. MFC_TRY;
  580. TRACE_METHOD(CMyComputerComponentData,Command);
  581. TEST_NONNULL_PTR_PARAM(piDataObject);
  582. TRACE( "CMyComputerComponentData::Command: command %ld selected\n", lCommandID );
  583. switch (lCommandID)
  584. {
  585. case IDM_CHANGE_COMPUTER_TASK:
  586. case IDM_CHANGE_COMPUTER_TOP:
  587. {
  588. ASSERT (QueryBaseRootCookie ().m_hScopeItem);
  589. return OnChangeComputer (piDataObject);
  590. }
  591. break;
  592. default:
  593. ASSERT(FALSE);
  594. break;
  595. }
  596. return S_OK;
  597. MFC_CATCH;
  598. } // CMyComputerComponentData::Command()
  599. ///////////////////////////////////////////////////////////////////////////////
  600. //
  601. // OnChangeComputer ()
  602. //
  603. // Purpose: Change the machine managed by the snapin
  604. //
  605. // Input: piDataObject - the selected node. This should be the root node
  606. // the snapin.
  607. // Output: Returns S_OK on success
  608. //
  609. ///////////////////////////////////////////////////////////////////////////////
  610. // 1. Launch object picker and get new computer name
  611. // 2. Change root node text
  612. // 3. Save new computer name to persistent name
  613. // 4. Delete subordinate nodes
  614. // 5. Re-add subordinate nodes
  615. HRESULT CMyComputerComponentData::OnChangeComputer(IDataObject * piDataObject)
  616. {
  617. MFC_TRY;
  618. #ifdef FIX538345
  619. // 538345-2002/06/18-JonN Only permit retarget if no property sheet
  620. // is currently displayed
  621. {
  622. CComQIPtr<IPropertySheetProvider,
  623. &IID_IPropertySheetProvider> pISP = m_pConsole;
  624. ASSERT(!!pISP);
  625. if (pISP)
  626. {
  627. HRESULT hr2 = pISP->FindPropertySheet( 0L, // cookie
  628. NULL, // IComponent
  629. piDataObject);
  630. if (S_OK == hr2)
  631. return S_OK; // BUGBUG BUGBUG
  632. }
  633. }
  634. #endif
  635. HWND hWndParent = NULL;
  636. HRESULT hr = m_pConsole->GetMainWindow (&hWndParent);
  637. CComBSTR sbstrTargetComputer;
  638. //
  639. // JonN 12/7/99 using CHOOSER2
  640. //
  641. if ( CHOOSER2_PickTargetComputer( AfxGetInstanceHandle(),
  642. hWndParent,
  643. &sbstrTargetComputer ) )
  644. {
  645. CString strTargetComputer = sbstrTargetComputer;
  646. strTargetComputer.MakeUpper ();
  647. // added IsLocalComputername 1/27/99 JonN
  648. // If the user chooses the local computer, treat that as if they had chosen
  649. // "Local Computer" in Snapin Manager. This means that there is no way to
  650. // reset the snapin to target explicitly at this computer without either
  651. // reloading the snapin from Snapin Manager, or going to a different computer.
  652. // When the Choose Target Computer UI is revised, we can make this more
  653. // consistent with Snapin Manager.
  654. if ( IsLocalComputername( strTargetComputer ) )
  655. strTargetComputer = L"";
  656. QueryRootCookie().SetMachineName (strTargetComputer);
  657. // Set the persistent name. If we are managing the local computer
  658. // this name should be empty.
  659. m_strMachineNamePersist = strTargetComputer;
  660. hr = ChangeRootNodeName (strTargetComputer);
  661. if ( SUCCEEDED(hr) )
  662. {
  663. // Delete subordinates and re-add
  664. HSCOPEITEM hRootScopeItem = QueryBaseRootCookie ().m_hScopeItem;
  665. MMC_COOKIE lCookie = 0;
  666. HSCOPEITEM hChild = 0;
  667. do {
  668. hr = m_pConsoleNameSpace->GetChildItem (hRootScopeItem,
  669. &hChild, &lCookie);
  670. if ( S_OK != hr )
  671. break;
  672. hr = m_pConsoleNameSpace->DeleteItem (hChild, TRUE);
  673. ASSERT (SUCCEEDED (hr));
  674. if ( !SUCCEEDED(hr) )
  675. break;
  676. CMyComputerCookie* pCookie =
  677. reinterpret_cast <CMyComputerCookie*> (lCookie);
  678. if ( !pCookie )
  679. continue;
  680. CMyComputerCookie& rootCookie = QueryRootCookie();
  681. POSITION pos1 = 0;
  682. POSITION pos2 = 0;
  683. CBaseCookieBlock* pScopeCookie = 0;
  684. for ( pos1 = rootCookie.m_listScopeCookieBlocks.GetHeadPosition ();
  685. ( pos2 = pos1) != NULL;
  686. )
  687. {
  688. pScopeCookie = rootCookie.m_listScopeCookieBlocks.GetNext (pos1);
  689. ASSERT (pScopeCookie);
  690. if ( pScopeCookie == pCookie )
  691. {
  692. rootCookie.m_listScopeCookieBlocks.RemoveAt (pos2);
  693. pScopeCookie->Release ();
  694. }
  695. }
  696. } while (S_OK == hr);
  697. hr = AddScopeNodes (hRootScopeItem, QueryRootCookie ());
  698. hr = m_pConsole->UpdateAllViews (piDataObject, 0, HINT_SELECT_ROOT_NODE);
  699. }
  700. }
  701. return hr;
  702. MFC_CATCH;
  703. }
  704. ///////////////////////////////////////////////////////////////////////////////
  705. //
  706. // ChangeRootNodeName ()
  707. //
  708. // Purpose: Change the text of the root node
  709. //
  710. // Input: newName - the new machine name that the snapin manages
  711. // Output: Returns S_OK on success
  712. //
  713. ///////////////////////////////////////////////////////////////////////////////
  714. HRESULT CMyComputerComponentData::ChangeRootNodeName(const CString & newName)
  715. {
  716. MFC_TRY;
  717. ASSERT (QueryBaseRootCookie ().m_hScopeItem);
  718. if ( !QueryBaseRootCookie ().m_hScopeItem )
  719. return E_UNEXPECTED;
  720. CString machineName (newName);
  721. CString formattedName = FormatDisplayName (machineName);
  722. SCOPEDATAITEM item;
  723. ::ZeroMemory (&item, sizeof (SCOPEDATAITEM));
  724. item.mask = SDI_STR;
  725. item.displayname = (LPTSTR) (LPCTSTR) formattedName;
  726. item.ID = QueryBaseRootCookie ().m_hScopeItem;
  727. return m_pConsoleNameSpace->SetItem (&item);
  728. MFC_CATCH;
  729. }
  730. CString FormatDisplayName(CString machineName)
  731. {
  732. CString formattedName;
  733. // If strDisplayName is empty, then this manages the local machine. Get
  734. // the local machine name. Then format the computer name with the snapin
  735. // name
  736. if (machineName.IsEmpty())
  737. {
  738. VERIFY (formattedName.LoadString (IDS_SCOPE_MYCOMPUTER_LOCAL_MACHINE));
  739. }
  740. else
  741. {
  742. // strip off the leading whackWhack
  743. if ( machineName.Find (L"\\\\") == 0 )
  744. {
  745. machineName = machineName.GetBuffer (machineName.GetLength ()) + 2;
  746. machineName.ReleaseBuffer ();
  747. }
  748. machineName.MakeUpper ();
  749. formattedName.FormatMessage (IDS_SCOPE_MYCOMPUTER_ON_MACHINE, machineName);
  750. }
  751. return formattedName;
  752. }
  753. int DisplayNetMsgError (HWND hWndParent, const CString& computerName, NET_API_STATUS dwErr, CString& displayedMessage)
  754. {
  755. int retVal = IDNO;
  756. AFX_MANAGE_STATE (AfxGetStaticModuleState ());
  757. LPVOID lpMsgBuf = 0;
  758. HMODULE hNetMsgDLL = ::LoadLibrary (L"netmsg.dll");
  759. if ( hNetMsgDLL )
  760. {
  761. ::FormatMessage (
  762. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_FROM_HMODULE,
  763. hNetMsgDLL,
  764. dwErr,
  765. MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  766. (LPTSTR) &lpMsgBuf, 0, NULL);
  767. // Display the string.
  768. CString text;
  769. CString caption;
  770. text.FormatMessage (IDS_CANT_CONNECT_TO_MACHINE, (LPCWSTR) computerName, (LPTSTR) lpMsgBuf);
  771. VERIFY (caption.LoadString (IDS_TASKPADTITLE_COMPUTER));
  772. retVal = ::MessageBox (hWndParent, text, caption, MB_ICONWARNING | MB_OK);
  773. displayedMessage = text;
  774. // Free the buffer.
  775. ::LocalFree (lpMsgBuf);
  776. ::FreeLibrary (hNetMsgDLL);
  777. }
  778. return retVal;
  779. }