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.

1798 lines
44 KiB

  1. // Copyright (c) 1999 Microsoft Corporation
  2. #include "precomp.h"
  3. #include "DataSrc.h"
  4. #include "..\common\util.h"
  5. #include "resource.h"
  6. #include <stdio.h>
  7. #include "si.h"
  8. #include <cominit.h>
  9. #include "ShlWapi.h"
  10. #include "AsyncObjectSink.h"
  11. #include "UIHelpers.h"
  12. #define FULLACCTSIZE 100
  13. //---------------------------------------------------------
  14. DataSource::DataSource():m_cRef(1)
  15. {
  16. CoInitialize(NULL);
  17. m_OSType = 0; //UNKNOWN;
  18. m_user.authIdent = NULL;
  19. m_user.currUser = true;
  20. memset(m_user.fullAcct, 0, FULLACCTSIZE * sizeof(TCHAR));
  21. m_hImageList = 0;
  22. m_folderIcon = 0;
  23. m_earthIcon = 0;
  24. m_classIcon = 0;
  25. m_instanceIcon = 0;
  26. m_scopeInstanceIcon = 0;
  27. m_scopeClassIcon = 0;
  28. m_sessionID = 0;
  29. m_NSCache.display = 0;
  30. m_NSCache.fullPath = 0;
  31. m_NSCache.ns = 0;
  32. m_hwndPropSheet = NULL;
  33. //Initialize return codes to failed state and namespaces to disconnected state
  34. m_securityHr = E_FAIL;
  35. m_osHr = E_FAIL;
  36. m_cpuHr = E_FAIL;
  37. m_settingHr = E_FAIL;
  38. m_rootSecNS.DisconnectServer();
  39. m_cimv2NS.DisconnectServer();
  40. }
  41. //---------------------------------------------------------
  42. DataSource::~DataSource()
  43. {
  44. CancelAllAsyncCalls();
  45. Disconnect();
  46. CoUninitialize();
  47. }
  48. //---------------------------------------------------------
  49. // connecting.
  50. void DataSource::SetMachineName(CHString1 &machine)
  51. {
  52. TCHAR curComp[256] = {0};
  53. DWORD size = ARRAYSIZE(curComp);
  54. // check the whacks
  55. if (GetComputerName(curComp, &size))
  56. {
  57. // if local...
  58. if((machine.GetLength() == 0) ||
  59. (machine == curComp))
  60. {
  61. m_whackedMachineName = "";
  62. }
  63. else if(machine[0] == _T('\\')) // its whacked.
  64. {
  65. m_whackedMachineName = machine;
  66. }
  67. else //its not whacked.
  68. {
  69. m_whackedMachineName = _T("\\\\");
  70. m_whackedMachineName += machine;
  71. }
  72. }
  73. else //failed to get local computer name
  74. m_whackedMachineName = "";
  75. }
  76. //---------------------------------------------------------
  77. // connecting.
  78. HRESULT DataSource::Connect(LOGIN_CREDENTIALS *credentials, HWND notify)
  79. {
  80. // start the connection thread.
  81. if(m_rootThread.Connect((bstr_t)m_whackedMachineName, "root", true, credentials, notify))
  82. {
  83. }
  84. m_sessionID++;
  85. return m_rootThread.m_hr;
  86. }
  87. //----------------------------------------------------------
  88. // closes property sheet if displated
  89. void DataSource::ClosePropSheet()
  90. {
  91. if (m_hwndPropSheet)
  92. {
  93. SendMessage(m_hwndPropSheet, WM_CLOSE, 0, 0 );
  94. m_hwndPropSheet = NULL;
  95. }
  96. }
  97. //----------------------------------------------------------
  98. bool DataSource::IsNewConnection(long *sessionID)
  99. {
  100. bool retval = false;
  101. if(m_sessionID != *sessionID)
  102. {
  103. *sessionID = m_sessionID;
  104. retval = true;
  105. }
  106. return retval;
  107. }
  108. //----------------------------------------------------------
  109. HRESULT DataSource::Reconnect(void)
  110. {
  111. Disconnect();
  112. Connect(GetCredentials());
  113. return m_rootThread.m_hr;
  114. }
  115. //----------------------------------------------------------
  116. HRESULT DataSource::Initialize(IWbemServices *pServices)
  117. {
  118. IWbemClassObject *pInst = NULL;
  119. HRESULT retval = S_OK;
  120. m_securityHr = S_OK;
  121. m_osHr = S_OK;
  122. m_cpuHr = S_OK;
  123. m_settingHr = S_OK;
  124. if(pServices == 0) return E_FAIL;
  125. try
  126. {
  127. m_rootThread.m_WbemServices.DisconnectServer();
  128. m_rootThread.m_WbemServices = pServices;
  129. m_rootThread.m_WbemServices.m_authIdent = m_user.authIdent;
  130. if(m_rootThread.m_status == WbemServiceThread::ready)
  131. {
  132. m_cimv2NS.DisconnectServer();
  133. // we'll use some general info from root\cimv2
  134. m_cimv2NS = m_rootThread.m_WbemServices.OpenNamespace("cimv2");
  135. m_cimv2NS.m_authIdent = m_user.authIdent;
  136. if((bool)m_cimv2NS)
  137. {
  138. if((pInst = m_cimv2NS.FirstInstanceOf("Win32_OperatingSystem")) != NULL)
  139. {
  140. m_OS = pInst;
  141. m_OSType = (short)m_OS.GetLong("OSType");
  142. }
  143. m_osHr = m_cimv2NS.m_hr;
  144. if((pInst = m_cimv2NS.FirstInstanceOf("Win32_Processor")) != NULL)
  145. {
  146. m_cpu = pInst;
  147. }
  148. m_cpuHr = m_cimv2NS.m_hr;
  149. m_winMgmt = m_cimv2NS.GetObject("Win32_WMISetting=@");
  150. // if the wmisetting class doesn't even exist....
  151. if(!(bool)m_winMgmt)
  152. {
  153. // create what we can on the fly.
  154. UpdateOldBuild();
  155. // try again.
  156. m_winMgmt = m_cimv2NS.GetObject("Win32_WMISetting=@");
  157. }
  158. m_settingHr = m_cimv2NS.m_hr;
  159. }
  160. else
  161. {
  162. m_osHr = m_rootThread.m_WbemServices.m_hr;
  163. m_cpuHr = m_rootThread.m_WbemServices.m_hr;
  164. m_settingHr = m_rootThread.m_WbemServices.m_hr;
  165. }
  166. // find security...
  167. CWbemClassObject sysSec = m_rootThread.m_WbemServices.GetObject("__SystemSecurity=@");
  168. if((bool)sysSec)
  169. {
  170. // its the new SD security
  171. m_NSSecurity = true;
  172. }
  173. else
  174. {
  175. // its old fashioned namespace security.
  176. m_rootSecNS = m_rootThread.m_WbemServices.OpenNamespace("security");
  177. m_NSSecurity = false;
  178. }
  179. m_securityHr = m_rootThread.m_WbemServices.m_hr;
  180. }
  181. }
  182. catch ( ... )
  183. {
  184. retval = WBEM_E_INITIALIZATION_FAILURE;
  185. }
  186. return retval;
  187. }
  188. //----------------------------------------------------------
  189. #include "mofstr.inc"
  190. #include "mofsec.inc"
  191. HRESULT DataSource::UpdateOldBuild(void)
  192. {
  193. HRESULT hr = S_OK;
  194. UINT mofSize = strlen(CLASSMOF);
  195. mofSize += strlen(INSTMOF);
  196. char *mofStr = new char[mofSize + 2];
  197. memset(mofStr, 0, mofSize + 2);
  198. strcpy(mofStr, CLASSMOF);
  199. strcat(mofStr, INSTMOF);
  200. wchar_t svrNS[100] = {0};
  201. wcscpy(svrNS,(LPCWSTR)m_rootThread.m_nameSpace); // this will point to \root.
  202. wcscat(svrNS, L"\\cimv2");
  203. IMofCompiler *pCompiler = NULL;
  204. hr = CoCreateInstance(CLSID_MofCompiler, 0, CLSCTX_INPROC_SERVER,
  205. IID_IMofCompiler, (LPVOID *) &pCompiler);
  206. hr = pCompiler->CompileBuffer(strlen(mofStr), (LPBYTE)mofStr,
  207. svrNS, //ServerAndNamespace,
  208. NULL, //User,
  209. NULL, //Authority,
  210. NULL, //Password,
  211. 0, 0, 0, NULL); //lOptionFlags,lClassFlags, lInstanceFlags
  212. delete[] mofStr;
  213. // now for the security trick.
  214. mofSize = strlen(SECMOF);
  215. mofStr = new char[mofSize + 2];
  216. strcpy(mofStr, SECMOF);
  217. wcscpy(svrNS,(LPCWSTR)m_rootThread.m_nameSpace); // this will point to \root.
  218. wcscat(svrNS, L"\\security");
  219. hr = pCompiler->CompileBuffer(strlen(mofStr), (LPBYTE)mofStr,
  220. svrNS, //ServerAndNamespace,
  221. NULL, //User,
  222. NULL, //Authority,
  223. NULL, //Password,
  224. 0, 0, 0, NULL); //lOptionFlags,lClassFlags, lInstanceFlags
  225. delete[] mofStr;
  226. pCompiler->Release();
  227. return hr;
  228. }
  229. //----------------------------------------------------------
  230. HRESULT DataSource::Disconnect(bool clearCredentials)
  231. {
  232. if(m_user.authIdent && clearCredentials)
  233. {
  234. m_rootThread.m_WbemServices.m_authIdent = 0;
  235. m_cimv2NS.m_authIdent = 0;
  236. m_rootSecNS.m_authIdent = 0;
  237. WbemFreeAuthIdentity(m_user.authIdent);
  238. m_user.authIdent = 0;
  239. m_user.currUser = true;
  240. memset(m_user.fullAcct, 0, FULLACCTSIZE * sizeof(TCHAR));
  241. }
  242. if(IsConnected())
  243. {
  244. m_rootSecNS.DisconnectServer();
  245. m_cimv2NS.DisconnectServer();
  246. // this is the root NS.
  247. m_rootThread.DisconnectServer();
  248. m_sessionID++;
  249. }
  250. if ((bool)m_winMgmt)
  251. m_winMgmt = (IWbemClassObject *)NULL;
  252. if ((bool)m_OS)
  253. m_OS = (IWbemClassObject *)NULL;
  254. if ((bool)m_cpu)
  255. m_cpu = (IWbemClassObject *)NULL;
  256. m_securityHr = E_FAIL;
  257. m_osHr = E_FAIL;
  258. m_cpuHr = E_FAIL;
  259. m_settingHr = E_FAIL;
  260. return ERROR_SUCCESS;
  261. }
  262. //----------------------------------------------------------
  263. bool DataSource::IsConnected(void) const
  264. {
  265. return (m_rootThread.m_status == WbemServiceThread::ready);
  266. }
  267. //----------------------------------------------------------
  268. bool DataSource::IsLocal(void) const
  269. {
  270. return (m_whackedMachineName.GetLength() == 0);
  271. }
  272. //----------------------------------------------------------
  273. bool DataSource::IsAncient(void) const
  274. {
  275. return (!m_NSSecurity);
  276. }
  277. //----------------------------------------------------------
  278. LOGIN_CREDENTIALS *DataSource::GetCredentials(void)
  279. {
  280. return &m_user;
  281. }
  282. //----------------------------------------------------------
  283. bstr_t DataSource::GetRootNS(void)
  284. {
  285. return m_rootSecNS.m_path;
  286. }
  287. //----------------------------------------------------------
  288. ISecurityInformation *DataSource::GetSI(struct NSNODE *nsNode)
  289. {
  290. ISecurityInformation *si = NULL;
  291. // hacky fix for when we have a broken WMI for some reason.
  292. // we can edit locally if we're on NT.
  293. if(m_NSSecurity &&
  294. ((m_OSType == OSTYPE_WINNT) || (IsNT() && IsLocal())))
  295. {
  296. // access the acl methods.
  297. //Check whether the namespace is already opened
  298. if(nsNode->nsLoaded == false)
  299. {
  300. if(nsNode->sType == TYPE_SCOPE_INSTANCE)
  301. {
  302. }
  303. else
  304. {
  305. //Connect to the namespace now...
  306. *(nsNode->ns) = nsNode->ns->OpenNamespace(nsNode->display);
  307. if(nsNode->sType == TYPE_STATIC_INSTANCE)
  308. {
  309. //Now open the WbemClassObject with flags to read the __SD
  310. if(nsNode->pclsObj != NULL)
  311. {
  312. delete nsNode->pclsObj;
  313. }
  314. nsNode->pclsObj = new CWbemClassObject();
  315. *(nsNode->pclsObj) = nsNode->ns->GetObject(nsNode->relPath/*,Flag*/);
  316. }
  317. }
  318. nsNode->nsLoaded = true;
  319. }
  320. bstr_t server = m_cpu.GetString("__SERVER");
  321. si = new CSDSecurity(nsNode,server,IsLocal());
  322. /* ns, m_user.authIdent,
  323. path, display,
  324. server, IsLocal());
  325. */
  326. }
  327. return si;
  328. }
  329. //----------------------------------------------------------
  330. // general tab.
  331. HRESULT DataSource::GetCPU(CHString1 &cpu)
  332. {
  333. HRESULT hr = m_cpuHr;
  334. if((bool)m_cpu)
  335. {
  336. cpu = (LPCTSTR)m_cpu.GetString("Name");
  337. if(cpu.GetLength() != 0)
  338. {
  339. hr = S_OK;
  340. }
  341. }
  342. return hr;
  343. }
  344. //----------------------------------------------------------
  345. HRESULT DataSource::GetOS(CHString1 &os)
  346. {
  347. HRESULT hr = m_osHr;
  348. if((bool)m_OS)
  349. {
  350. os = (LPCTSTR)m_OS.GetString("Caption");
  351. if(os.GetLength() != 0)
  352. {
  353. hr = S_OK;
  354. }
  355. }
  356. return hr;
  357. }
  358. //----------------------------------------------------
  359. HRESULT DataSource::GetOSVersion(CHString1 &ver)
  360. {
  361. HRESULT hr = m_osHr;
  362. if((bool)m_OS)
  363. {
  364. TCHAR _scr1[100] = {0};
  365. TCHAR _scr2[100] = {0};
  366. // Build and set the serial number string
  367. if (m_OS.GetBool("Debug"))
  368. {
  369. _scr1[0] = TEXT(' ');
  370. LoadString(HINST_THISDLL, IDS_DEBUG, &(_scr1[1]), ARRAYSIZE(_scr1)-1);
  371. }
  372. else
  373. {
  374. _scr1[0] = TEXT('\0');
  375. }
  376. // Version.buildNumber (DEBUG).
  377. _tcscpy(_scr2, (LPCTSTR)m_OS.GetString("Version"));
  378. _sntprintf(_scr2, ARRAYSIZE(_scr2)-1, TEXT("%s%s"), _scr2, _scr1);
  379. _scr2[ARRAYSIZE(_scr2)-1] = 0;
  380. ver = (LPCTSTR)_scr2;
  381. hr = S_OK;
  382. }
  383. return hr;
  384. }
  385. //----------------------------------------------------
  386. HRESULT DataSource::GetServicePackNumber(CHString1 &ServPack)
  387. {
  388. HRESULT hr = m_osHr;
  389. if((bool)m_OS)
  390. {
  391. TCHAR _scr1[100] = {0};
  392. _sntprintf(_scr1, ARRAYSIZE(_scr1)-1, TEXT("%ld.%ld"), m_OS.GetLong("ServicePackMajorVersion"), m_OS.GetLong("ServicePackMinorVersion"));
  393. _scr1[ARRAYSIZE(_scr1)-1] = 0;
  394. ServPack = (LPCTSTR)_scr1;
  395. hr = S_OK;
  396. }
  397. return hr;
  398. }
  399. //----------------------------------------------------------
  400. HRESULT DataSource::GetBldNbr(CHString1 &bldNbr)
  401. {
  402. HRESULT hr = m_settingHr;
  403. if((bool)m_winMgmt)
  404. {
  405. bldNbr = (LPCTSTR)m_winMgmt.GetString("BuildVersion");
  406. if(bldNbr.GetLength() != 0)
  407. {
  408. hr = S_OK;
  409. }
  410. }
  411. return hr;
  412. }
  413. //----------------------------------------------------------
  414. HRESULT DataSource::GetInstallDir(CHString1 &dir)
  415. {
  416. bstr_t value;
  417. HRESULT hr = m_settingHr;
  418. if((bool)m_winMgmt)
  419. {
  420. dir = (LPCTSTR)m_winMgmt.GetString("InstallationDirectory");
  421. if(dir.GetLength() != 0)
  422. {
  423. hr = S_OK;
  424. }
  425. }
  426. return hr;
  427. }
  428. //----------------------------------------------------------
  429. HRESULT DataSource::GetDBDir(CHString1 &dir)
  430. {
  431. bstr_t value;
  432. HRESULT hr = m_settingHr;
  433. if((bool)m_winMgmt)
  434. {
  435. dir = (LPCTSTR)m_winMgmt.GetString("DatabaseDirectory");
  436. if(dir.GetLength() != 0)
  437. {
  438. hr = S_OK;
  439. }
  440. }
  441. return hr;
  442. }
  443. //----------------------------------------------------------
  444. HRESULT DataSource::GetBackupInterval(UINT &interval)
  445. {
  446. HRESULT hr = m_settingHr;
  447. if((bool)m_winMgmt)
  448. {
  449. interval = m_winMgmt.GetLongEx("BackupInterval");
  450. hr = S_OK;
  451. }
  452. return hr;
  453. }
  454. //----------------------------------------------------------
  455. HRESULT DataSource::SetBackupInterval(UINT interval)
  456. {
  457. CHString1 value;
  458. HRESULT hr = m_settingHr;
  459. if((bool)m_winMgmt)
  460. {
  461. hr = m_winMgmt.PutEx("BackupInterval", (long)interval);
  462. }
  463. return hr;
  464. }
  465. //----------------------------------------------------------
  466. HRESULT DataSource::GetLastBackup(CHString1 &time)
  467. {
  468. HRESULT hr = m_settingHr;
  469. bstr_t dmtf;
  470. if((bool)m_winMgmt)
  471. {
  472. hr = m_winMgmt.GetDateTimeFormat("BackupLastTime", dmtf);
  473. if(SUCCEEDED(hr))
  474. time = (LPCTSTR)dmtf;
  475. }
  476. return hr;
  477. }
  478. //----------------------------------------------------------
  479. // logging tab.
  480. HRESULT DataSource::GetLoggingStatus(LOGSTATUS &status)
  481. {
  482. bstr_t temp;
  483. HRESULT hr = m_settingHr;
  484. if((bool)m_winMgmt)
  485. {
  486. status = (LOGSTATUS)m_winMgmt.GetLongEx("LoggingLevel");
  487. hr = S_OK;
  488. }
  489. return hr;
  490. }
  491. //----------------------------------------------------------
  492. HRESULT DataSource::SetLoggingStatus(LOGSTATUS status)
  493. {
  494. CHString1 value;
  495. bstr_t temp;
  496. HRESULT hr = m_settingHr;
  497. if((bool)m_winMgmt)
  498. {
  499. hr = m_winMgmt.PutEx("LoggingLevel", (long)status);
  500. }
  501. return hr;
  502. }
  503. //----------------------------------------------------------
  504. HRESULT DataSource::GetLoggingSize(ULONG &size)
  505. {
  506. bstr_t value;
  507. HRESULT hr = m_settingHr;
  508. if((bool)m_winMgmt)
  509. {
  510. size = m_winMgmt.GetLongEx("MaxLogFileSize");
  511. hr = S_OK;
  512. }
  513. return hr;
  514. }
  515. //----------------------------------------------------------
  516. HRESULT DataSource::SetLoggingSize(const ULONG size)
  517. {
  518. CHString1 value;
  519. HRESULT hr = m_settingHr;
  520. if((bool)m_winMgmt)
  521. {
  522. hr = m_winMgmt.PutEx("MaxLogFileSize", (long)size);
  523. }
  524. return hr;
  525. }
  526. //----------------------------------------------------------
  527. HRESULT DataSource::GetDBLocation(CHString1 &dir)
  528. {
  529. bstr_t value;
  530. HRESULT hr = m_settingHr;
  531. if((bool)m_winMgmt)
  532. {
  533. dir = (LPCTSTR)m_winMgmt.GetString("DatabaseDirectory");
  534. if(dir.GetLength() != 0)
  535. {
  536. hr = S_OK;
  537. }
  538. }
  539. return hr;
  540. }
  541. //----------------------------------------------------------
  542. HRESULT DataSource::GetLoggingLocation(CHString1 &dir)
  543. {
  544. bstr_t value;
  545. HRESULT hr = m_settingHr;
  546. if((bool)m_winMgmt)
  547. {
  548. CHString1 dir2;
  549. dir2 = (LPCTSTR)m_winMgmt.GetString("LoggingDirectory");
  550. TCHAR temp[_MAX_PATH] = {0};
  551. _tcscpy(temp, (LPCTSTR)dir2);
  552. PathAddBackslash(temp);
  553. dir = temp;
  554. hr = S_OK;
  555. }
  556. return hr;
  557. }
  558. //----------------------------------------------------------
  559. HRESULT DataSource::SetLoggingLocation(CHString1 dir)
  560. {
  561. HRESULT hr = m_settingHr;
  562. bstr_t value = dir;
  563. if((bool)m_winMgmt && dir.GetLength() != 0)
  564. {
  565. hr = m_winMgmt.Put("LoggingDirectory", (bstr_t)(LPCTSTR)dir);
  566. }
  567. return hr;
  568. }
  569. #define WIN32_DIRECTORY _T("Win32_directory=\"")
  570. //----------------------------------------------------------
  571. bool DataSource::IsValidDir(CHString1 &dir)
  572. {
  573. bool retval = true;
  574. if((bool)m_cimv2NS)
  575. {
  576. // double the whacks because wmi has bad syntax.
  577. TCHAR cooked[_MAX_PATH * 2] = {0};
  578. TCHAR input[_MAX_PATH] = {0};
  579. TCHAR path[_MAX_PATH * 2 + ARRAYSIZE(WIN32_DIRECTORY) + 1] = {0};
  580. int len = dir.GetLength();
  581. _tcscpy(input, (LPCTSTR)dir);
  582. _tcscpy(path, WIN32_DIRECTORY);
  583. for(int x = 0; x < len; x++)
  584. {
  585. _tcsncat(cooked, &input[x], 1);
  586. // if its a whack...
  587. if(input[x] == _T('\\'))
  588. {
  589. // have another pleeb.
  590. _tcscat(cooked, _T("\\"));
  591. }
  592. } //endfor
  593. _tcscat(path, cooked);
  594. path[_tcslen(path) - 2] = 0;
  595. _tcscat(path, _T("\""));
  596. CWbemClassObject inst = m_cimv2NS.GetObject(path);
  597. retval = (bool)inst;
  598. }
  599. else //can't check, assume it's valid and let it through
  600. {
  601. // //warn & maybe.
  602. // retval = false;
  603. }
  604. return retval;
  605. }
  606. #define CIM_LOGICALFILE _T("CIM_LogicalFile=\"")
  607. //----------------------------------------------------------
  608. bool DataSource::IsValidFile(LPCTSTR szDir)
  609. {
  610. TCHAR szBuffer[MAX_PATH * 2 + ARRAYSIZE(CIM_LOGICALFILE) + 1]; //size should accommodate additional escapes
  611. bool retval = true;
  612. if((bool)m_cimv2NS)
  613. {
  614. _tcscpy(szBuffer, CIM_LOGICALFILE);
  615. TCHAR * psz = szBuffer + ARRAYSIZE(CIM_LOGICALFILE) - 1;
  616. while (*psz = *szDir)
  617. {
  618. if (*szDir == _T('\\'))
  619. {
  620. *psz++ = _T('\\');
  621. *psz = _T('\\');
  622. }
  623. psz++;
  624. szDir++;
  625. }
  626. *psz++ = _T('\"');
  627. *psz = _T('\0');
  628. CWbemClassObject inst = m_cimv2NS.GetObject(szBuffer);
  629. retval = (bool)inst;
  630. }
  631. else //can't check, assume it's valid and let it through
  632. {
  633. // //warn & maybe.
  634. // retval = false;
  635. }
  636. return retval;
  637. }
  638. //----------------------------------------------------------
  639. bool DataSource::CanBrowseFS(void) const
  640. {
  641. return IsLocal();
  642. }
  643. //----------------------------------------------------------
  644. // advanced tab.
  645. HRESULT DataSource::GetScriptASPEnabled(bool &enabled)
  646. {
  647. HRESULT hr = m_settingHr;
  648. if((bool)m_winMgmt)
  649. {
  650. enabled = m_winMgmt.GetBoolEx("ASPScriptEnabled");
  651. hr = S_OK;
  652. }
  653. return hr;
  654. }
  655. //----------------------------------------------------------
  656. HRESULT DataSource::SetScriptASPEnabled(bool &enabled)
  657. {
  658. HRESULT hr = m_settingHr;
  659. if((bool)m_winMgmt)
  660. {
  661. m_winMgmt.PutEx("ASPScriptEnabled", enabled);
  662. hr = S_OK;
  663. }
  664. return hr;
  665. }
  666. //----------------------------------------------------------
  667. HRESULT DataSource::GetAnonConnections(bool &enabled)
  668. {
  669. HRESULT hr = m_settingHr;
  670. if((bool)m_winMgmt)
  671. {
  672. enabled = m_winMgmt.GetBoolEx("EnableAnonWin9xConnections");
  673. hr = S_OK;
  674. }
  675. return hr;
  676. }
  677. //----------------------------------------------------------
  678. HRESULT DataSource::SetAnonConnections(bool &enabled)
  679. {
  680. HRESULT hr = m_settingHr;
  681. if((bool)m_winMgmt)
  682. {
  683. m_winMgmt.PutEx("EnableAnonWin9xConnections", enabled);
  684. hr = S_OK;
  685. }
  686. return hr;
  687. }
  688. //----------------------------------------------------------
  689. HRESULT DataSource::GetScriptDefNS(CHString1 &ns)
  690. {
  691. HRESULT hr = m_settingHr;
  692. bstr_t value;
  693. if((bool)m_winMgmt)
  694. {
  695. ns = (LPCTSTR)m_winMgmt.GetString("ASPScriptDefaultNamespace");
  696. if(ns.GetLength() != 0)
  697. {
  698. hr = S_OK;
  699. }
  700. }
  701. return hr;
  702. }
  703. //----------------------------------------------------------
  704. HRESULT DataSource::SetScriptDefNS(LPCTSTR ns)
  705. {
  706. HRESULT hr = m_settingHr;
  707. if((bool)m_winMgmt)
  708. {
  709. hr = m_winMgmt.Put("ASPScriptDefaultNamespace", (bstr_t)(LPCTSTR)ns);
  710. }
  711. return hr;
  712. }
  713. //----------------------------------------------------------
  714. HRESULT DataSource::GetRestart(RESTART &restart)
  715. {
  716. bstr_t value;
  717. HRESULT hr = m_settingHr;
  718. if((bool)m_winMgmt)
  719. {
  720. restart = (RESTART)m_winMgmt.GetLongEx("AutoStartWin9X");
  721. hr = S_OK;
  722. }
  723. return hr;
  724. }
  725. //----------------------------------------------------------
  726. HRESULT DataSource::SetRestart(RESTART restart)
  727. {
  728. CHString1 value;
  729. HRESULT hr = m_settingHr;
  730. if((bool)m_winMgmt)
  731. {
  732. hr = m_winMgmt.PutEx("AutoStartWin9X", (long)restart);
  733. }
  734. return hr;
  735. }
  736. //----------------------------------------------------------
  737. HRESULT DataSource::PutWMISetting(BOOL refresh)
  738. {
  739. HRESULT hr = m_cimv2NS.PutInstance(m_winMgmt);
  740. if(refresh)
  741. m_winMgmt = m_cimv2NS.GetObject("Win32_WMISetting=@");
  742. return hr;
  743. }
  744. //----------------------------------------------------------
  745. //----------------------------------------------------------
  746. // NAMESPACE CACHE -----------------------------------------
  747. LPTSTR DataSource::CopyString( LPTSTR pszSrc )
  748. {
  749. LPTSTR pszDst = NULL;
  750. if (pszSrc != NULL)
  751. {
  752. pszDst = new TCHAR[(lstrlen(pszSrc) + 1)];
  753. if (pszDst)
  754. {
  755. lstrcpy( pszDst, pszSrc );
  756. }
  757. }
  758. return pszDst;
  759. }
  760. //---------------------------------------------------------------------------
  761. void DataSource::DeleteAllNodes(void)
  762. {
  763. DeleteNode(&m_NSCache);
  764. }
  765. //---------------------------------------------------------------------------
  766. void DataSource::DeleteNode(NSNODE *node)
  767. {
  768. if(node)
  769. {
  770. delete[] node->display;
  771. delete[] node->fullPath;
  772. node->ns = 0;
  773. int size = node->children.GetSize();
  774. // walk the children.
  775. for(int x = 0; x < size; x++)
  776. {
  777. struct NSNODE *child = node->children[x];
  778. DeleteNode(child);
  779. }
  780. if(node != &m_NSCache)
  781. {
  782. delete node;
  783. }
  784. else
  785. {
  786. m_NSCache.children.RemoveAll();
  787. }
  788. }
  789. }
  790. //---------------------------------------------------------------------------
  791. void DataSource::LoadImageList(HWND hTree)
  792. {
  793. if(m_hImageList == 0)
  794. {
  795. // create an empty imagelist.
  796. m_hImageList = ImageList_Create(16, 16, ILC_COLOR8|ILC_MASK, 3, 0);
  797. // add an icon
  798. m_folderIcon = ImageList_AddIcon(m_hImageList,
  799. LoadIcon(_Module.GetModuleInstance(),
  800. MAKEINTRESOURCE(IDI_CLSD_FOLDER)));
  801. m_earthIcon = ImageList_AddIcon(m_hImageList,
  802. LoadIcon(_Module.GetModuleInstance(),
  803. MAKEINTRESOURCE(IDI_EARTH)));
  804. m_classIcon = ImageList_AddIcon(m_hImageList,
  805. LoadIcon(_Module.GetModuleInstance(),
  806. MAKEINTRESOURCE(IDI_CLSD_CLASS)));
  807. m_instanceIcon = ImageList_AddIcon(m_hImageList,
  808. LoadIcon(_Module.GetModuleInstance(),
  809. MAKEINTRESOURCE(IDI_CLSD_INSTANCE)));
  810. m_scopeInstanceIcon = ImageList_AddIcon(m_hImageList,
  811. LoadIcon(_Module.GetModuleInstance(),
  812. MAKEINTRESOURCE(IDI_CLSD_SCOPEINSTANCE)));
  813. m_scopeClassIcon = ImageList_AddIcon(m_hImageList,
  814. LoadIcon(_Module.GetModuleInstance(),
  815. MAKEINTRESOURCE(IDI_CLSD_SCOPECLASS)));
  816. }
  817. // sent it to the tree.
  818. TreeView_SetImageList(hTree, m_hImageList, TVSIL_NORMAL);
  819. }
  820. //---------------------------------------------------------------------------
  821. HRESULT DataSource::LoadNode(HWND hTree, HTREEITEM hItem /* = TVI_ROOT */,
  822. int flags)
  823. {
  824. HRESULT hr = E_FAIL;
  825. // loading the root?
  826. if(hItem == TVI_ROOT)
  827. {
  828. // initialize the node.
  829. m_NSCache.display = CopyString(_T("Root"));
  830. m_NSCache.fullPath = CopyString(_T("Root"));
  831. m_NSCache.ns = &m_rootThread.m_WbemServices;
  832. m_NSCache.hideMe = false;
  833. m_NSCache.sType = TYPE_NAMESPACE;
  834. ITEMEXTRA *extra = new ITEMEXTRA;
  835. if(extra == NULL)
  836. return E_FAIL;
  837. extra->nsNode = &m_NSCache;
  838. extra->loaded = false;
  839. // initialize the invariant parts.
  840. TVINSERTSTRUCT tvInsert;
  841. tvInsert.hParent = TVI_ROOT;
  842. tvInsert.hInsertAfter = TVI_SORT;
  843. tvInsert.item.mask = TVIF_TEXT | TVIF_CHILDREN |TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
  844. tvInsert.item.hItem = 0;
  845. tvInsert.item.state = 0;
  846. tvInsert.item.iImage = FolderIcon();
  847. tvInsert.item.iSelectedImage = FolderIcon();
  848. tvInsert.item.stateMask = 0;
  849. tvInsert.item.cChildren = (flags == ROOT_ONLY? 0: 1);
  850. tvInsert.item.pszText = CopyString(m_NSCache.display);
  851. if (!tvInsert.item.pszText)
  852. {
  853. delete extra;
  854. return E_FAIL;
  855. }
  856. tvInsert.item.cchTextMax = _tcslen(tvInsert.item.pszText);
  857. tvInsert.item.lParam = (LPARAM)extra;
  858. HTREEITEM hItem2;
  859. hItem2 = TreeView_InsertItem(hTree, &tvInsert);
  860. hr = (hItem != 0 ? S_OK : E_FAIL);
  861. }
  862. else if(flags != ROOT_ONLY) // expanding an existing node.
  863. {
  864. TV_ITEM item;
  865. struct NSNODE *node = NULL;
  866. // find the cached node.
  867. item.mask = TVIF_PARAM | TVIF_CHILDREN;
  868. item.hItem = hItem;
  869. BOOL x = TreeView_GetItem(hTree, &item);
  870. ITEMEXTRA *extra = (ITEMEXTRA *)item.lParam;
  871. node = extra->nsNode;
  872. // are the kids in the cache??
  873. /* int size = node->children.GetSize();
  874. ATLTRACE(_T("NODE STATE: %d, %d\n"), size, extra->loaded);
  875. if((size == 0) &&
  876. (extra->loaded == false))
  877. */
  878. if(extra->loaded == false)
  879. {
  880. //Now delete all the children in the cache as they might be due to a previously cancelled enumeration
  881. node->children.RemoveAll();
  882. // nope!!! Enum that 'node' to the cache.
  883. // ShowControls(true);
  884. hr = PopulateCacheNode(hTree,hItem,extra);
  885. // NOTE: empties will be WBEM_E_NOT_FOUND here.
  886. }
  887. /* // got kids now?
  888. size = node->children.GetSize();
  889. if(size == 0)
  890. {
  891. // get rid of the plus sign.
  892. item.mask = TVIF_CHILDREN;
  893. item.cChildren = 0;
  894. TreeView_SetItem(hTree, &item);
  895. }
  896. else if(extra->loaded == false) // && size != 0
  897. {
  898. // load the tree now.
  899. hr = PopulateTreeNode(hTree, hItem, node, flags);
  900. extra->loaded = true;
  901. }
  902. */
  903. }
  904. return hr;
  905. }
  906. //---------------------------------------------------------------------------
  907. bool DataSource::MFLNamepace(LPTSTR name)
  908. {
  909. bool t1 = (_tcslen(name) == 6); // just the right length...
  910. bool t2 = (_tcsnicmp(name, _T("MS_"), 3) == 0); // starts right...
  911. int scan = _tcsspn(&name[3], _T("0123456789"));
  912. bool t3 = (scan == 3);
  913. return t1 && t2 && t3;
  914. }
  915. //---------------------------------------------------------------------------
  916. HRESULT DataSource::PopulateCacheNode(HWND hTreeWnd,HTREEITEM hItem,struct ITEMEXTRA *extra)
  917. {
  918. // load up the principals.
  919. IWbemClassObject *inst = NULL;
  920. IEnumWbemClassObject *nsEnum = NULL;
  921. ULONG uReturned = 0;
  922. HRESULT hr = E_FAIL;
  923. HRESULT hr1 = E_FAIL;
  924. struct NSNODE *parent= extra->nsNode;
  925. CWbemServices *ns = NULL;
  926. extra->loaded = true;
  927. switch(parent->sType)
  928. {
  929. case TYPE_NAMESPACE:
  930. {
  931. //Check whether the namespace is already opened
  932. if(parent->nsLoaded == false)
  933. {
  934. //Connect to the namespace now...
  935. *(parent->ns) = parent->ns->OpenNamespace(parent->display);
  936. parent->nsLoaded = true;
  937. }
  938. ns = parent->ns;
  939. //Create the Namespace Enum
  940. CAsyncObjectSink *objSinkNS;
  941. objSinkNS = new CAsyncObjectSink(hTreeWnd,hItem,parent,this,ENUM_NAMESPACE);
  942. if (objSinkNS != NULL)
  943. {
  944. IWbemObjectSink *pSyncStubNS = NULL;
  945. hr = GetAsyncSinkStub(objSinkNS,&pSyncStubNS);
  946. if (SUCCEEDED(hr))
  947. {
  948. objSinkNS->SetSinkStub(pSyncStubNS);
  949. hr = ns->CreateInstanceEnumAsync(L"__namespace",pSyncStubNS);
  950. pSyncStubNS->Release();
  951. if (parent->objSink != NULL)
  952. {
  953. ((CAsyncObjectSink *)parent->objSink)->SetSinkStub(NULL);
  954. parent->objSink->Release();
  955. }
  956. parent->objSink = NULL;
  957. if (parent->objSinkNS != NULL)
  958. {
  959. ((CAsyncObjectSink *)parent->objSinkNS)->SetSinkStub(NULL);
  960. parent->objSinkNS->Release();
  961. }
  962. parent->objSinkNS = objSinkNS;
  963. parent->objSinkNS->AddRef();
  964. }
  965. else
  966. delete objSinkNS;
  967. }
  968. else
  969. hr = E_OUTOFMEMORY;
  970. if(SUCCEEDED(hr))
  971. {
  972. asyncList.push_back(extra);
  973. }
  974. else
  975. {
  976. //Some problem with the enumerations. So remove the Plus sign as no nodes will be populated
  977. RemovePlus(hTreeWnd,hItem);
  978. }
  979. break;
  980. }
  981. case TYPE_SCOPE_CLASS:
  982. case TYPE_STATIC_CLASS:
  983. {
  984. //Check whether the namespace is already opened
  985. if(parent->nsLoaded == false)
  986. {
  987. //Connect to the namespace now...
  988. *(parent->ns) = parent->ns->OpenNamespace(parent->display);
  989. parent->nsLoaded = true;
  990. }
  991. ns = parent->ns;
  992. //Since we can set the secutiry for the static Instances Enumerate the Instances of this static class now
  993. CAsyncObjectSink *objSink;
  994. objSink = new CAsyncObjectSink(hTreeWnd,hItem,parent,this,ENUM_INSTANCE);
  995. if (objSink != NULL)
  996. {
  997. IWbemObjectSink *pSyncStub = NULL;
  998. hr = GetAsyncSinkStub(objSink,&pSyncStub);
  999. if (SUCCEEDED(hr))
  1000. {
  1001. objSink->SetSinkStub(pSyncStub);
  1002. hr = ns->CreateInstanceEnumAsync(parent->display,pSyncStub);
  1003. pSyncStub->Release();
  1004. if (parent->objSink != NULL)
  1005. {
  1006. ((CAsyncObjectSink *)parent->objSink)->SetSinkStub(NULL);
  1007. parent->objSink->Release();
  1008. }
  1009. parent->objSink = objSink;
  1010. parent->objSink->AddRef();
  1011. }
  1012. else
  1013. delete objSink;
  1014. }
  1015. else
  1016. hr = E_OUTOFMEMORY;
  1017. if(FAILED(hr))
  1018. {
  1019. //Some problem with the enumeration. So remove the Plus sign as no nodes will be populated
  1020. RemovePlus(hTreeWnd,hItem);
  1021. }
  1022. else
  1023. {
  1024. asyncList.push_back(extra);
  1025. }
  1026. break;
  1027. }
  1028. case TYPE_SCOPE_INSTANCE:
  1029. {
  1030. //Check whether the namespace is already opened
  1031. /* if(parent->nsLoaded == false)
  1032. {
  1033. IWbemServices *pServ = NULL;
  1034. // IWbemServicesEx *pServEx = NULL,*pServEx1 = NULL;
  1035. //Connect to the scope now.
  1036. pServ = m_rootThread.m_WbemServices.m_pService;
  1037. // hr = pServ->QueryInterface(IID_IWbemServicesEx,(void **)&pServEx);
  1038. // if(SUCCEEDED(hr))
  1039. {
  1040. // parent->pServicesEx = NULL;
  1041. TCHAR strTemp[1024];
  1042. _tcscpy(strTemp,_T("ScopeClass.Name=\"ScopeInst1\""));
  1043. //Now open the scope
  1044. hr = pServEx->Open(strTemp,0,0,NULL,&pServEx1,NULL);
  1045. // hr = pServEx->Open(parent->fullPath,NULL,0,NULL,&pServEx1,NULL);
  1046. if(SUCCEEDED(hr))
  1047. {
  1048. //Now we have opened the scope
  1049. parent->nsLoaded = true;
  1050. //Enumerate the Instances in the scope now
  1051. CAsyncObjectSink *objSink;
  1052. parent->objSink = new CAsyncObjectSink(hTreeWnd,hItem,parent,this,ENUM_SCOPE_INSTANCE);
  1053. IWbemObjectSink *pSyncStub = NULL;
  1054. GetAsyncSinkStub(parent->objSink,&pSyncStub);
  1055. objSink = (CAsyncObjectSink *)parent->objSink;
  1056. objSink->SetSinkStub(pSyncStub);
  1057. hr = parent->pServicesEx->CreateInstanceEnumAsync(L"",0,NULL,pSyncStub);
  1058. pSyncStub->Release();
  1059. if(FAILED(hr))
  1060. {
  1061. //Some problem with the enumeration. So remove the Plus sign as no nodes will be populated
  1062. RemovePlus(hTreeWnd,hItem);
  1063. }
  1064. else
  1065. {
  1066. asyncList.push_back(extra);
  1067. }
  1068. }
  1069. }
  1070. }
  1071. if(parent->nsLoaded == false)
  1072. {
  1073. //Do we have to display an error message here???
  1074. MessageBox(NULL,_T("Unable to open scope"),_T("NULL"),MB_OK);
  1075. }
  1076. */
  1077. break;
  1078. }
  1079. case TYPE_DYNAMIC_CLASS:
  1080. {
  1081. //The control should not come here. Even if it comes we won't do anything
  1082. break;
  1083. }
  1084. case TYPE_STATIC_INSTANCE:
  1085. {
  1086. break;
  1087. }
  1088. }
  1089. return hr;
  1090. }
  1091. //---------------------------------------------------------------------------
  1092. HRESULT DataSource::PopulateTreeNode(HWND hTree, HTREEITEM hParentItem,
  1093. struct NSNODE *parent,
  1094. int flags)
  1095. {
  1096. HRESULT hr = E_FAIL;
  1097. if(parent)
  1098. {
  1099. // initialize the invariant parts.
  1100. TVINSERTSTRUCT tvInsert;
  1101. tvInsert.hParent = hParentItem;
  1102. tvInsert.hInsertAfter = TVI_SORT;
  1103. tvInsert.item.hItem = 0;
  1104. tvInsert.item.state = 0;
  1105. tvInsert.item.iImage = 1;
  1106. tvInsert.item.stateMask = 0;
  1107. int size = parent->children.GetSize();
  1108. if(size == 0)
  1109. return WBEM_E_NOT_FOUND;
  1110. hr = E_FAIL; // in case we bounce right over the for(...).
  1111. // walk the children.
  1112. for(int x = 0; x < size; x++)
  1113. {
  1114. struct NSNODE *child = parent->children[x];
  1115. if(!((flags == HIDE_SOME) && child->hideMe) )
  1116. {
  1117. ATLTRACE(_T("NStree: %s %s\n"), parent->display, child->display);
  1118. tvInsert.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
  1119. tvInsert.item.pszText = CopyString(child->display);
  1120. if (!tvInsert.item.pszText)
  1121. return E_FAIL;
  1122. tvInsert.item.cchTextMax = _tcslen(tvInsert.item.pszText);
  1123. ITEMEXTRA *extra = new ITEMEXTRA;
  1124. if(extra == NULL)
  1125. return E_FAIL;
  1126. extra->nsNode = child;
  1127. extra->loaded = false;
  1128. tvInsert.item.lParam = (LPARAM)extra;
  1129. tvInsert.item.cChildren = 1;
  1130. if(MFLNamepace(child->display))
  1131. {
  1132. tvInsert.item.iImage = EarthIcon();
  1133. tvInsert.item.iSelectedImage = EarthIcon();
  1134. }
  1135. else
  1136. {
  1137. if((child->sType == TYPE_DYNAMIC_CLASS) || (child->sType == TYPE_STATIC_CLASS))
  1138. {
  1139. tvInsert.item.iImage = ClassIcon();
  1140. tvInsert.item.iSelectedImage = ClassIcon();
  1141. }
  1142. else
  1143. { //Defaulted to Namespace
  1144. tvInsert.item.iImage = FolderIcon();
  1145. tvInsert.item.iSelectedImage = FolderIcon();
  1146. }
  1147. }
  1148. // Insert principal into list.
  1149. HTREEITEM hItem2;
  1150. hItem2 = TreeView_InsertItem(hTree, &tvInsert);
  1151. }
  1152. } //endwhile
  1153. hr = S_OK;
  1154. } //endif (bool)ns
  1155. return hr;
  1156. }
  1157. void DataSource::InsertNamespaceNode(HWND hTreeWnd,HTREEITEM hItem,struct NSNODE *parent, IWbemClassObject *pclsObj)
  1158. {
  1159. std::auto_ptr<NSNODE> AutoNode(new struct NSNODE);
  1160. NSNODE *node = AutoNode.get();
  1161. if (NULL == node ) return;
  1162. CWbemClassObject easy(pclsObj);
  1163. bstr_t name = easy.GetString("Name");
  1164. bstr_t path = easy.GetString("__NAMESPACE");
  1165. bstr_t full = path + _T("\\");
  1166. full += name;
  1167. bstr_t relPath = easy.GetString("__RELPATH");
  1168. node->fullPath = CopyString(full);
  1169. node->relPath = CopyString(relPath);
  1170. node->display = CopyString(name);
  1171. node->hideMe = false; //HideableNode(node->display);
  1172. node->nsLoaded = false;
  1173. node->sType = TYPE_NAMESPACE;
  1174. parent->children.Add(node);
  1175. AutoNode.release();
  1176. node->ns = new CWbemServices(*(parent->ns));
  1177. //Now add the node to the tree
  1178. TVINSERTSTRUCT tvInsert;
  1179. tvInsert.hParent = hItem;
  1180. tvInsert.hInsertAfter = TVI_SORT;
  1181. tvInsert.item.hItem = 0;
  1182. tvInsert.item.state = 0;
  1183. tvInsert.item.iImage = 1;
  1184. tvInsert.item.stateMask = 0;
  1185. ITEMEXTRA *extra = new ITEMEXTRA;
  1186. if(extra == NULL)
  1187. return;
  1188. extra->nsNode = node;
  1189. extra->loaded = false;
  1190. tvInsert.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
  1191. tvInsert.item.pszText = CopyString(node->display);
  1192. tvInsert.item.cchTextMax = _tcslen(tvInsert.item.pszText);
  1193. tvInsert.item.lParam = (LPARAM)extra;
  1194. tvInsert.item.cChildren = 1;
  1195. if(MFLNamepace(node->display))
  1196. {
  1197. tvInsert.item.iImage = EarthIcon();
  1198. tvInsert.item.iSelectedImage = EarthIcon();
  1199. }
  1200. else
  1201. {
  1202. tvInsert.item.iImage = FolderIcon();
  1203. tvInsert.item.iSelectedImage = FolderIcon();
  1204. }
  1205. // Insert principal into list.
  1206. HTREEITEM hItem2;
  1207. hItem2 = TreeView_InsertItem(hTreeWnd, &tvInsert);
  1208. }
  1209. void DataSource::InsertClassNode(HWND hTreeWnd,HTREEITEM hItem,struct NSNODE *parent, IWbemClassObject *pclsObj)
  1210. {
  1211. IWbemQualifierSet *qSet;
  1212. pclsObj->GetQualifierSet(&qSet);
  1213. VARIANT vt;
  1214. VariantInit(&vt);
  1215. HRESULT hr = qSet->Get(L"Abstract",0,&vt,NULL);
  1216. if(hr != WBEM_E_NOT_FOUND)
  1217. {
  1218. if(vt.boolVal == VARIANT_TRUE)
  1219. {
  1220. qSet->Release();
  1221. return;
  1222. }
  1223. }
  1224. std::auto_ptr<NSNODE> AutoNode(new struct NSNODE);
  1225. NSNODE *node = AutoNode.get();
  1226. if( NULL == node ) return;
  1227. CWbemClassObject easy(pclsObj);
  1228. bstr_t name = easy.GetString("__CLASS");
  1229. bstr_t path = easy.GetString("__PATH");
  1230. bstr_t relPath = easy.GetString("__RELPATH");
  1231. node->fullPath = CopyString(path);
  1232. node->relPath = CopyString(relPath);
  1233. node->display = CopyString(name);
  1234. node->hideMe = false;
  1235. node->nsLoaded = true;
  1236. VariantClear(&vt);
  1237. hr = qSet->Get(L"Dynamic",0,&vt,NULL);
  1238. if((hr != WBEM_E_NOT_FOUND) && (vt.boolVal == VARIANT_TRUE))
  1239. {
  1240. node->sType = TYPE_DYNAMIC_CLASS;
  1241. node->ns = NULL;
  1242. }
  1243. else
  1244. {
  1245. //Now check whether it is a scope class
  1246. hr = qSet->Get(L"Scope",0,&vt,NULL);
  1247. if((hr != WBEM_E_NOT_FOUND) && (vt.boolVal == VARIANT_TRUE))
  1248. {
  1249. //This class is marked as scope. So all instances of this class can be scopes
  1250. node->sType = TYPE_SCOPE_CLASS;
  1251. }
  1252. else
  1253. {
  1254. //It is a static class
  1255. node->sType = TYPE_STATIC_CLASS;
  1256. }
  1257. node->ns = new CWbemServices(*(parent->ns));
  1258. node->pclsObj = new CWbemClassObject(pclsObj);
  1259. }
  1260. parent->children.Add(node);
  1261. AutoNode.release();
  1262. //Now Add the node to the Tree
  1263. TVINSERTSTRUCT tvInsert;
  1264. tvInsert.hParent = hItem;
  1265. tvInsert.hInsertAfter = TVI_SORT;
  1266. tvInsert.item.hItem = 0;
  1267. tvInsert.item.state = 0;
  1268. tvInsert.item.iImage = 1;
  1269. tvInsert.item.stateMask = 0;
  1270. ITEMEXTRA *extra = new ITEMEXTRA;
  1271. if(extra == NULL)
  1272. return;
  1273. extra->nsNode = node;
  1274. extra->loaded = false;
  1275. tvInsert.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
  1276. tvInsert.item.pszText = CopyString(node->display);
  1277. tvInsert.item.cchTextMax = _tcslen(tvInsert.item.pszText);
  1278. tvInsert.item.lParam = (LPARAM)extra;
  1279. if(node->sType == TYPE_DYNAMIC_CLASS)
  1280. {
  1281. //Remove the plus sign for the Dynamic Classes
  1282. tvInsert.item.cChildren = 0;
  1283. }
  1284. else
  1285. {
  1286. tvInsert.item.cChildren = 1;
  1287. }
  1288. if(node->sType == TYPE_SCOPE_CLASS)
  1289. {
  1290. tvInsert.item.iImage = ScopeClassIcon();
  1291. tvInsert.item.iSelectedImage = ScopeClassIcon();
  1292. }
  1293. else
  1294. {
  1295. tvInsert.item.iImage = ClassIcon();
  1296. tvInsert.item.iSelectedImage = ClassIcon();
  1297. }
  1298. // Insert principal into list.
  1299. HTREEITEM hItem2;
  1300. hItem2 = TreeView_InsertItem(hTreeWnd, &tvInsert);
  1301. }
  1302. void DataSource::InsertInstanceNode(HWND hTreeWnd,HTREEITEM hItem,struct NSNODE *parent, IWbemClassObject *pclsObj)
  1303. {
  1304. TCHAR strKey[10240];
  1305. struct NSNODE *node = new struct NSNODE;
  1306. CWbemClassObject easy(pclsObj);
  1307. HRESULT hr = E_FAIL;
  1308. _bstr_t strName;
  1309. _variant_t vtValue;
  1310. VARIANT Value;
  1311. _tcscpy(strKey,_T(""));
  1312. VariantInit(&Value);
  1313. bool bfirstTime = true;
  1314. //Now Enumerate the Keys and for a name like "key1,key2,key3,..."
  1315. if(SUCCEEDED(hr = pclsObj->BeginEnumeration(WBEM_FLAG_KEYS_ONLY)))
  1316. {
  1317. while(pclsObj->Next(0,NULL,&Value,NULL,NULL) != WBEM_S_NO_MORE_DATA)
  1318. {
  1319. vtValue = Value;
  1320. if(bfirstTime)
  1321. {
  1322. bfirstTime = false;
  1323. }
  1324. else
  1325. {
  1326. _tcscat(strKey,_T(","));
  1327. }
  1328. vtValue.ChangeType(VT_BSTR,NULL);
  1329. _bstr_t temp = _bstr_t(vtValue); //for PREFIX
  1330. if (!temp)
  1331. return;
  1332. _tcscat(strKey,temp);
  1333. VariantClear(&Value);
  1334. }
  1335. }
  1336. if(_tcscmp(strKey,_T("")) == 0)
  1337. {
  1338. //Some Problem or it is the instance of a singleton class
  1339. _tcscpy(strKey,_T("@"));
  1340. }
  1341. bstr_t name = strKey;
  1342. bstr_t path = easy.GetString("__PATH");
  1343. bstr_t relPath = easy.GetString("__RELPATH");
  1344. node->fullPath = CopyString(path);
  1345. node->relPath = CopyString(relPath);
  1346. node->display = CopyString(name);
  1347. node->hideMe = false;
  1348. node->nsLoaded = false;
  1349. node->ns = new CWbemServices(*(parent->ns));
  1350. if(parent->sType == TYPE_SCOPE_CLASS)
  1351. {
  1352. node->sType = TYPE_SCOPE_INSTANCE;
  1353. }
  1354. else
  1355. {
  1356. node->sType = TYPE_STATIC_INSTANCE;
  1357. }
  1358. node->pclsObj = new CWbemClassObject(pclsObj);
  1359. parent->children.Add(node);
  1360. TVITEM item;
  1361. item.mask = TVIF_CHILDREN | TVIF_HANDLE;
  1362. item.hItem = hItem;
  1363. item.cChildren = 1;
  1364. TreeView_SetItem(hTreeWnd, &item);
  1365. //Now Add the node to the Tree
  1366. TVINSERTSTRUCT tvInsert;
  1367. tvInsert.hParent = hItem;
  1368. tvInsert.hInsertAfter = TVI_SORT;
  1369. tvInsert.item.hItem = 0;
  1370. tvInsert.item.state = 0;
  1371. tvInsert.item.iImage = 1;
  1372. tvInsert.item.stateMask = 0;
  1373. ITEMEXTRA *extra = new ITEMEXTRA;
  1374. if(extra == NULL)
  1375. return;
  1376. extra->nsNode = node;
  1377. extra->loaded = false;
  1378. tvInsert.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_PARAM|TVIF_IMAGE|TVIF_SELECTEDIMAGE;
  1379. tvInsert.item.pszText = CopyString(node->display);
  1380. tvInsert.item.cchTextMax = _tcslen(tvInsert.item.pszText);
  1381. tvInsert.item.lParam = (LPARAM)extra;
  1382. if(node->sType == TYPE_SCOPE_INSTANCE)
  1383. {
  1384. tvInsert.item.iImage = ScopeInstanceIcon();
  1385. tvInsert.item.iSelectedImage = ScopeInstanceIcon();
  1386. tvInsert.item.cChildren = 1;
  1387. }
  1388. else
  1389. {
  1390. tvInsert.item.iImage = InstanceIcon();
  1391. tvInsert.item.iSelectedImage = InstanceIcon();
  1392. tvInsert.item.cChildren = 0; //Since these are regular static instances, remove the plus sign
  1393. }
  1394. // Insert principal into list.
  1395. HTREEITEM hItem2;
  1396. hItem2 = TreeView_InsertItem(hTreeWnd, &tvInsert);
  1397. }
  1398. void DataSource::InsertScopeInstanceNode(HWND hTreeWnd,HTREEITEM hItem,struct NSNODE *parent, IWbemClassObject *pclsObj)
  1399. {
  1400. }
  1401. void DataSource::RemovePlus(HWND hTreeWnd,HTREEITEM hItem)
  1402. {
  1403. // get rid of the plus sign.
  1404. TVITEM item;
  1405. item.mask = TVIF_CHILDREN | TVIF_HANDLE;
  1406. item.hItem = hItem;
  1407. item.cChildren = 0;
  1408. TreeView_SetItem(hTreeWnd, &item);
  1409. }
  1410. void DataSource::CancelAllAsyncCalls()
  1411. {
  1412. struct NSNODE *pNode;
  1413. ASYNCLIST::iterator iter = asyncList.begin();
  1414. TV_ITEM item;
  1415. ITEMEXTRA *extra;
  1416. CAsyncObjectSink *objSink;
  1417. for(;iter != asyncList.end();iter++)
  1418. {
  1419. extra = *iter;
  1420. pNode = extra->nsNode;
  1421. if(pNode->objSinkNS != NULL)
  1422. {
  1423. objSink = (CAsyncObjectSink *)pNode->objSinkNS;
  1424. if (objSink->GetSinkStub() != NULL)
  1425. {
  1426. HRESULT hr = pNode->ns->CancelAsyncCall(objSink->GetSinkStub());
  1427. if((hr == WBEM_E_FAILED) || (hr == WBEM_E_INVALID_PARAMETER) || (hr == WBEM_E_OUT_OF_MEMORY) || (hr == WBEM_E_TRANSPORT_FAILURE))
  1428. {
  1429. hr = E_FAIL;
  1430. }
  1431. else
  1432. {
  1433. objSink->SetSinkStub(NULL);
  1434. extra->loaded = false;
  1435. }
  1436. }
  1437. }
  1438. if(pNode->objSink != NULL)
  1439. {
  1440. objSink = (CAsyncObjectSink *)pNode->objSink;
  1441. if (objSink->GetSinkStub() != NULL)
  1442. {
  1443. HRESULT hr = pNode->ns->CancelAsyncCall(objSink->GetSinkStub());
  1444. if((hr == WBEM_E_FAILED) || (hr == WBEM_E_INVALID_PARAMETER) || (hr == WBEM_E_OUT_OF_MEMORY) || (hr == WBEM_E_TRANSPORT_FAILURE))
  1445. {
  1446. hr = E_FAIL;
  1447. }
  1448. else
  1449. {
  1450. objSink->SetSinkStub(NULL);
  1451. extra->loaded = false;
  1452. }
  1453. }
  1454. }
  1455. }
  1456. asyncList.clear();
  1457. }
  1458. void DataSource::ProcessEndEnumAsync(IWbemObjectSink *pSink)
  1459. {
  1460. //First delete the node for this enum
  1461. // OutputDebugString(_T("End Enum Received!!!\n"));
  1462. struct ITEMEXTRA *extra;
  1463. struct NSNODE *pNode;
  1464. if(asyncList.empty() == true)
  1465. return;
  1466. ASYNCLIST::iterator iter = asyncList.begin();
  1467. CAsyncObjectSink *objSink;
  1468. for(;iter != asyncList.end();iter++)
  1469. {
  1470. extra = *iter;
  1471. pNode = extra->nsNode;
  1472. if(pNode->objSinkNS == pSink)
  1473. {
  1474. objSink = (CAsyncObjectSink *)pNode->objSinkNS;
  1475. objSink->SetSinkStub(NULL);
  1476. pNode->objSinkNS->Release();
  1477. pNode->objSinkNS = NULL;
  1478. break;
  1479. }
  1480. if(pNode->objSink == pSink)
  1481. {
  1482. objSink = (CAsyncObjectSink *)pNode->objSink;
  1483. objSink->SetSinkStub(NULL);
  1484. pNode->objSink->Release();
  1485. pNode->objSink = NULL;
  1486. break;
  1487. }
  1488. }
  1489. if((pNode->objSinkNS == NULL) && (pNode->objSink == NULL))
  1490. {
  1491. asyncList.remove(*iter);
  1492. extra->loaded = true;
  1493. }
  1494. if(asyncList.empty())
  1495. {
  1496. //Now Hide the Windows
  1497. // ShowControls(false);
  1498. }
  1499. }
  1500. void DataSource::SetControlHandles(HWND hwndStatic, HWND hwndButton)
  1501. {
  1502. m_hWndStatic = hwndStatic;
  1503. m_hWndButton = hwndButton;
  1504. }
  1505. void DataSource::ShowControls(bool bShow)
  1506. {
  1507. if(bShow == true)
  1508. {
  1509. ShowWindow(m_hWndStatic,SW_SHOW);
  1510. ShowWindow(m_hWndButton,SW_SHOW);
  1511. }
  1512. else
  1513. {
  1514. ShowWindow(m_hWndStatic,SW_HIDE);
  1515. ShowWindow(m_hWndButton,SW_HIDE);
  1516. }
  1517. }
  1518. HRESULT DataSource::GetAsyncSinkStub(IWbemObjectSink *pSink, IWbemObjectSink **pStubSink)
  1519. {
  1520. HRESULT hr;
  1521. IUnsecuredApartment* pUnsecApp = NULL;
  1522. if (pSink != NULL)
  1523. {
  1524. hr = CoCreateInstance(CLSID_UnsecuredApartment,
  1525. NULL,
  1526. CLSCTX_LOCAL_SERVER,
  1527. IID_IUnsecuredApartment,
  1528. (void**)&pUnsecApp);
  1529. if (SUCCEEDED(hr))
  1530. {
  1531. IUnknown* pStubUnk = NULL;
  1532. hr = pUnsecApp->CreateObjectStub(pSink, &pStubUnk);
  1533. if (SUCCEEDED(hr))
  1534. {
  1535. *pStubSink = NULL;
  1536. hr = pStubUnk->QueryInterface(IID_IWbemObjectSink,
  1537. (void **)pStubSink);
  1538. pStubUnk->Release();
  1539. }
  1540. pUnsecApp->Release();
  1541. }
  1542. }
  1543. else
  1544. hr = E_UNEXPECTED;
  1545. return hr;
  1546. }