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.

1528 lines
38 KiB

  1. // Copyright (c) 1997-1999 Microsoft Corporation
  2. #include "precomp.h"
  3. #ifdef EXT_DEBUG
  4. #undef THIS_FILE
  5. static char THIS_FILE[] = __FILE__;
  6. #endif
  7. #include "EnvPage.h"
  8. // avoid some warnings.
  9. #undef HDS_HORZ
  10. #undef HDS_BUTTONS
  11. #undef HDS_HIDDEN
  12. #include "resource.h"
  13. #include <stdlib.h>
  14. #include <TCHAR.h>
  15. #include "..\Common\util.h"
  16. #include "..\common\SshWbemHelpers.h"
  17. #include <windowsx.h>
  18. #include <commctrl.h>
  19. #include "edtenvar.h"
  20. #include "helpid.h"
  21. #include "common.h"
  22. // plagiarized without remorse from other WMI headers.
  23. template<class T>
  24. class CDeleteMe
  25. {
  26. protected:
  27. T* m_p;
  28. public:
  29. CDeleteMe(T* p) : m_p(p){}
  30. ~CDeleteMe() {delete m_p;}
  31. };
  32. // shows dialog box with exclamation icon
  33. // expects strID string to be of the form:
  34. // "Some thing bad happened for user %s: %s"
  35. // where the second %s will be filled by format message applied to the hr
  36. void EnvVarErrorDialog(HWND hParent, UINT strID, BSTR user, HRESULT hr)
  37. {
  38. CHString errorDescription;
  39. CHString errorMessage;
  40. TCHAR formatString[1024];
  41. ::LoadString(HINST_THISDLL, strID,
  42. formatString, 1024);
  43. TCHAR errorHeading[40];
  44. ::LoadString(HINST_THISDLL,IDS_ERR_HEADING,
  45. errorHeading, 40);
  46. ErrorLookup(hr, errorDescription);
  47. errorMessage.Format(formatString, user, errorDescription);
  48. ::MessageBox(hParent,errorMessage,errorHeading, MB_OK | MB_ICONEXCLAMATION);
  49. }
  50. DWORD aEnvVarsHelpIds[] = {
  51. IDC_ENVVAR_SYS_USERGROUP, IDH_NO_HELP,
  52. IDC_ENVVAR_SYS_LB_SYSVARS, (IDH_ENV + 0),
  53. IDC_ENVVAR_SYS_SYSVARS, (IDH_ENV + 0),
  54. IDC_ENVVAR_SYS_USERENV, (IDH_ENV + 2),
  55. IDC_ENVVAR_SYS_LB_USERVARS, (IDH_ENV + 2),
  56. IDC_ENVVAR_SYS_NEWUV, (IDH_ENV + 7),
  57. IDC_ENVVAR_SYS_EDITUV, (IDH_ENV + 8),
  58. IDC_ENVVAR_SYS_NDELUV, (IDH_ENV + 9),
  59. IDC_ENVVAR_SYS_NEWSV, (IDH_ENV + 10),
  60. IDC_ENVVAR_SYS_EDITSV, (IDH_ENV + 11),
  61. IDC_ENVVAR_SYS_DELSV, (IDH_ENV + 12),
  62. IDC_USERLIST, IDH_WBEM_ADVANCED_ENVARIABLE_USERVAR_LISTBOX,
  63. 0, 0
  64. };
  65. //----------------------------------------------------------------------
  66. INT_PTR CALLBACK StaticEnvDlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
  67. {
  68. // if this is the initDlg msg...
  69. if(message == WM_INITDIALOG)
  70. {
  71. // transfer the 'this' ptr to the extraBytes.
  72. SetWindowLongPtr(hwndDlg, DWLP_USER, lParam);
  73. }
  74. // DWL_USER is the 'this' ptr.
  75. EnvPage *me = (EnvPage *)GetWindowLongPtr(hwndDlg, DWLP_USER);
  76. if(me != NULL)
  77. {
  78. // call into the DlgProc() that has some context.
  79. return me->DlgProc(hwndDlg, message, wParam, lParam);
  80. }
  81. else
  82. {
  83. return FALSE;
  84. }
  85. }
  86. //--------------------------------------------------------------
  87. EnvPage::EnvPage(WbemServiceThread *serviceThread)
  88. : WBEMPageHelper(serviceThread)
  89. {
  90. m_bEditSystemVars = FALSE;
  91. m_bUserVars = FALSE;
  92. m_currUserModified = false;
  93. m_SysModified = false;
  94. }
  95. //--------------------------------------------------------------
  96. INT_PTR EnvPage::DoModal(HWND hDlg)
  97. {
  98. return DialogBoxParam(HINST_THISDLL,
  99. (LPTSTR) MAKEINTRESOURCE(IDD_ENVVARS),
  100. hDlg, StaticEnvDlgProc, (LPARAM)this);
  101. }
  102. //--------------------------------------------------------------
  103. EnvPage::~EnvPage()
  104. {
  105. }
  106. //--------------------------------------------------------------
  107. INT_PTR CALLBACK EnvPage::DlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
  108. {
  109. int i;
  110. m_hDlg = hwndDlg;
  111. switch (message)
  112. {
  113. case WM_INITDIALOG:
  114. Init(m_hDlg);
  115. return TRUE;
  116. break;
  117. case WM_NOTIFY:
  118. switch(((NMHDR FAR*)lParam)->code)
  119. {
  120. case LVN_KEYDOWN:
  121. switch(((NMHDR FAR*)lParam)->idFrom)
  122. {
  123. case IDC_ENVVAR_SYS_LB_USERVARS:
  124. i = IDC_ENVVAR_SYS_NDELUV;
  125. break;
  126. case IDC_ENVVAR_SYS_LB_SYSVARS:
  127. i = IDC_ENVVAR_SYS_DELSV;
  128. break;
  129. default:
  130. return(FALSE);
  131. break;
  132. } // switch
  133. if(VK_DELETE == ((LV_KEYDOWN FAR *) lParam)->wVKey)
  134. {
  135. HWND hwnd = GetDlgItem(m_hDlg, i);
  136. if(IsWindowEnabled(hwnd))
  137. {
  138. SendMessage(m_hDlg, WM_COMMAND,
  139. MAKEWPARAM(i, BN_CLICKED),
  140. (LPARAM)hwnd );
  141. }
  142. else
  143. {
  144. MessageBeep(MB_ICONASTERISK);
  145. }
  146. } // if (VK_DELETE...
  147. break;
  148. case NM_SETFOCUS:
  149. if(wParam == IDC_ENVVAR_SYS_LB_USERVARS)
  150. {
  151. m_bUserVars = TRUE;
  152. }
  153. else
  154. {
  155. m_bUserVars = FALSE;
  156. }
  157. break;
  158. case NM_DBLCLK:
  159. { //BEGIN
  160. HWND hWndTemp;
  161. switch(((NMHDR FAR*)lParam)->idFrom)
  162. {
  163. case IDC_ENVVAR_SYS_LB_USERVARS:
  164. i = IDC_ENVVAR_SYS_EDITUV;
  165. break;
  166. case IDC_ENVVAR_SYS_LB_SYSVARS:
  167. i = IDC_ENVVAR_SYS_EDITSV;
  168. break;
  169. default:
  170. return(FALSE);
  171. break;
  172. } // switch
  173. hWndTemp = GetDlgItem(m_hDlg, i);
  174. if(IsWindowEnabled(hWndTemp))
  175. {
  176. SendMessage(m_hDlg, WM_COMMAND,
  177. MAKEWPARAM(i, BN_CLICKED),
  178. (LPARAM)hWndTemp);
  179. }
  180. else
  181. {
  182. MessageBeep(MB_ICONASTERISK);
  183. }
  184. }//END
  185. break;
  186. default:
  187. return FALSE;
  188. } //endswitch(((NMHDR FAR*)lParam)->code)
  189. break;
  190. case WM_COMMAND:
  191. DoCommand(m_hDlg, (HWND)lParam, LOWORD(wParam), HIWORD(wParam));
  192. break;
  193. case WM_DESTROY:
  194. CleanUp(m_hDlg);
  195. break;
  196. case WM_HELP: // F1
  197. ::WinHelp((HWND)((LPHELPINFO)lParam)->hItemHandle,
  198. L"sysdm.hlp",
  199. HELP_WM_HELP,
  200. (ULONG_PTR)(LPSTR)aEnvVarsHelpIds);
  201. break;
  202. case WM_CONTEXTMENU: // right mouse click
  203. WinHelp((HWND) wParam,
  204. HELP_FILE, HELP_CONTEXTMENU,
  205. (ULONG_PTR)(LPSTR)aEnvVarsHelpIds);
  206. break;
  207. default:
  208. return FALSE;
  209. }
  210. return FALSE;
  211. }
  212. //------------------------------------------------------------
  213. int EnvPage::AddUniqueUser(HWND hwnd, LPCTSTR str)
  214. {
  215. // if it doesn't already exist...
  216. if(ComboBox_FindStringExact(hwnd, -1, str) == CB_ERR)
  217. {
  218. // add it.
  219. return ComboBox_AddString(hwnd, str);
  220. }
  221. return -1;
  222. }
  223. //------------------------------------------------------------
  224. #define MAX_USER_NAME 100
  225. #define BUFZ 4096
  226. #define MAX_VALUE_LEN 1024
  227. TCHAR szSysEnv[] = TEXT( "System\\CurrentControlSet\\Control\\Session Manager\\Environment" );
  228. BOOL EnvPage::Init(HWND hDlg)
  229. {
  230. TCHAR szBuffer1[200] = {0};
  231. HWND hwndSys, hwndUser, hwndUserList;
  232. HRESULT hr = 0;
  233. LV_COLUMN col;
  234. LV_ITEM item;
  235. RECT rect;
  236. int cxFirstCol;
  237. unsigned int MaxScrollExtent = 0;
  238. IWbemClassObject *envInst = NULL;
  239. IEnumWbemClassObject *envEnum = NULL;
  240. bool bSysVar = false;
  241. DWORD uReturned = 0;
  242. bstr_t sSysUser("<SYSTEM>"); // magic string returned by provider
  243. bstr_t sUserName("UserName");
  244. bstr_t userName, firstUser;
  245. variant_t pVal, pVal1;
  246. // Create the first column
  247. LoadString(HINST_THISDLL, SYSTEM + 50, szBuffer1, 200);
  248. if (!GetClientRect(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS), &rect))
  249. {
  250. rect.right = 300;
  251. }
  252. cxFirstCol = (int)(rect.right * .3);
  253. col.mask = LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
  254. col.fmt = LVCFMT_LEFT;
  255. col.cx = cxFirstCol;
  256. col.pszText = szBuffer1;
  257. col.iSubItem = 0;
  258. SendDlgItemMessage(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS, LVM_INSERTCOLUMN,
  259. 0, (LPARAM) &col);
  260. SendDlgItemMessage(hDlg, IDC_ENVVAR_SYS_LB_USERVARS, LVM_INSERTCOLUMN,
  261. 0, (LPARAM) &col);
  262. // Create the second column
  263. LoadString(HINST_THISDLL, SYSTEM + 51, szBuffer1, 200);
  264. col.cx = rect.right - cxFirstCol - GetSystemMetrics(SM_CYHSCROLL);
  265. col.iSubItem = 1;
  266. SendDlgItemMessage(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS, LVM_INSERTCOLUMN,
  267. 1, (LPARAM) &col);
  268. SendDlgItemMessage(hDlg, IDC_ENVVAR_SYS_LB_USERVARS, LVM_INSERTCOLUMN,
  269. 1, (LPARAM) &col);
  270. ////////////////////////////////////////////////////////////////////
  271. // Display System Variables from wbem in listbox
  272. ////////////////////////////////////////////////////////////////////
  273. hwndSys = GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS);
  274. hwndUser = GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_USERVARS);
  275. hwndUserList = GetDlgItem(hDlg, IDC_USERLIST);
  276. // Try to open the System Environment variables area with
  277. // Read AND Write permission. If successful, then we allow
  278. // the User to edit them the same as their own variables.
  279. m_bEditSystemVars = FALSE;
  280. RemoteRegWriteable(szSysEnv, m_bEditSystemVars);
  281. // Disable System Var Editing buttons if
  282. // user is not an administrator
  283. //
  284. EnableWindow(GetDlgItem(hDlg, IDC_ENVVAR_SYS_NEWSV),
  285. m_bEditSystemVars);
  286. EnableWindow(GetDlgItem(hDlg, IDC_ENVVAR_SYS_EDITSV),
  287. m_bEditSystemVars);
  288. EnableWindow(GetDlgItem(hDlg, IDC_ENVVAR_SYS_DELSV),
  289. m_bEditSystemVars);
  290. if(m_serviceThread->m_machineName.length() > 0)
  291. m_bLocal = false;
  292. else
  293. m_bLocal = true;
  294. if((hr = m_WbemServices.CreateInstanceEnum(bstr_t("Win32_Environment"),
  295. WBEM_FLAG_SHALLOW,
  296. &envEnum)) == S_OK)
  297. {
  298. // get the first and only instance.
  299. while(SUCCEEDED(envEnum->Next(-1, 1, &envInst, &uReturned)) &&
  300. (uReturned != 0))
  301. {
  302. //Get whether the
  303. // who's variable.
  304. if(envInst->Get(sUserName, 0L, &pVal, NULL, NULL) == S_OK)
  305. {
  306. userName = V_BSTR(&pVal);
  307. if( userName.length() > MaxScrollExtent ){
  308. MaxScrollExtent = userName.length();
  309. }
  310. // setup for which list box gets this instance.
  311. if(userName == sSysUser)
  312. {
  313. LoadUser(envInst, userName, hwndSys);
  314. }
  315. else
  316. {
  317. if(m_bLocal == false)
  318. {
  319. if((firstUser.length() == 0) || // if the first user seen.
  320. (firstUser == userName)) // if seeing the firstUser again.
  321. {
  322. // save the first user.
  323. if(firstUser.length() == 0)
  324. {
  325. firstUser = userName;
  326. }
  327. AddUniqueUser(hwndUserList, userName);
  328. LoadUser(envInst, userName, hwndUser);
  329. }
  330. else
  331. {
  332. AddUniqueUser(hwndUserList, userName);
  333. }
  334. }
  335. else
  336. {
  337. if(IsLoggedInUser(userName))
  338. {
  339. if(firstUser.length() == 0)
  340. {
  341. firstUser = userName;
  342. }
  343. AddUniqueUser(hwndUserList, userName);
  344. LoadUser(envInst, userName, hwndUser);
  345. }
  346. else
  347. {
  348. AddUniqueUser(hwndUserList, userName);
  349. }
  350. }
  351. }
  352. } //endif who's variable.
  353. envInst->Release();
  354. } // endwhile envEnum
  355. envEnum->Release();
  356. SendMessage(
  357. hwndUserList, // handle to destination window
  358. CB_SETHORIZONTALEXTENT, // message to send
  359. (WPARAM) (MaxScrollExtent * 8 ),// scrollable width
  360. 0 // not used; must be zero
  361. );
  362. if(m_bLocal == false)
  363. {
  364. ComboBox_SetCurSel(hwndUserList, 0);
  365. }
  366. else
  367. {
  368. _bstr_t strLoggedinUser;
  369. GetLoggedinUser(&strLoggedinUser);
  370. SendMessage(hwndUserList,CB_SELECTSTRING,-1L,(LPARAM)(LPCTSTR)strLoggedinUser);
  371. }
  372. } //endif CreateInstanceEnum()
  373. // Select the first items in the listviews
  374. // It is important to set the User listview first, and
  375. // then the system. When the system listview is set,
  376. // we will receive a LVN_ITEMCHANGED notification and
  377. // clear the focus in the User listview. But when someone
  378. // tabs to the control the arrow keys will work correctly.
  379. item.mask = LVIF_STATE;
  380. item.iItem = 0;
  381. item.iSubItem = 0;
  382. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  383. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  384. SendDlgItemMessage(hDlg, IDC_ENVVAR_SYS_LB_USERVARS,
  385. LVM_SETITEMSTATE, 0, (LPARAM) &item);
  386. SendDlgItemMessage (hDlg, IDC_ENVVAR_SYS_LB_SYSVARS,
  387. LVM_SETITEMSTATE, 0, (LPARAM) &item);
  388. EnableWindow(GetDlgItem(hDlg, IDC_ENVVAR_SYS_SETUV), FALSE);
  389. EnableWindow(GetDlgItem(hDlg, IDC_ENVVAR_SYS_DELUV), FALSE);
  390. // Set extended LV style for whole line selection
  391. SendDlgItemMessage(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
  392. SendDlgItemMessage(hDlg, IDC_ENVVAR_SYS_LB_USERVARS, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT);
  393. return TRUE;
  394. }
  395. ////////////////////////////////////////////////////////////////////////////
  396. // FindVar
  397. //
  398. // Find the USER Environment variable that matches passed string
  399. // and return its listview index or -1
  400. //
  401. ////////////////////////////////////////////////////////////////////////////
  402. int EnvPage::FindVar(HWND hwndLV, LPTSTR szVar)
  403. {
  404. LV_FINDINFO FindInfo;
  405. FindInfo.flags = LVFI_STRING;
  406. FindInfo.psz = szVar;
  407. int n = ((int)SendMessage (hwndLV, LVM_FINDITEM, (WPARAM) -1, (LPARAM) &FindInfo));
  408. // list view seems to have trouble with long names, let's try it "by hand"
  409. if ((n == -1) && (_tcslen(szVar) > 200))
  410. {
  411. TCHAR* pBuf;
  412. int iCount = ListView_GetItemCount(hwndLV);
  413. LVITEM item;
  414. item.mask = LVIF_TEXT;
  415. item.iSubItem = 0;
  416. item.stateMask = 0;
  417. item.pszText = pBuf = new TCHAR[BUFZ +1];
  418. item.cchTextMax = BUFZ;
  419. if (pBuf)
  420. {
  421. for (int i = 0; i < iCount; i++)
  422. {
  423. // 'cuz the list view might change it??! (so say the docs)
  424. item.pszText = pBuf;
  425. item.iItem = i;
  426. if (ListView_GetItem(hwndLV, &item) && (0 == _tcsicmp(item.pszText, szVar)))
  427. {
  428. n = i;
  429. break;
  430. }
  431. }
  432. delete[] pBuf;
  433. }
  434. }
  435. return n;
  436. }
  437. ////////////////////////////////////////////////////////////////////////////
  438. // Saves the environment variables
  439. // pops up error dialog on error.
  440. ////////////////////////////////////////////////////////////////////////////
  441. void EnvPage::Save(HWND hDlg, int ID)
  442. {
  443. int i, n;
  444. HWND hwndTemp;
  445. ENVARS *penvar;
  446. CWbemClassObject inst;
  447. bstr_t sSysUser("<SYSTEM>"); // magic string returned by provider
  448. bstr_t sUserName("UserName");
  449. bstr_t sVarName("Name");
  450. bstr_t sVarVal("VariableValue");
  451. bstr_t sSysVar("SystemVariable");
  452. HRESULT hr = 0;
  453. // track separately, since the hr could get reset in the loop.
  454. HRESULT hrFailure = 0;
  455. int iFailure;
  456. LV_ITEM item;
  457. item.mask = LVIF_PARAM;
  458. item.iSubItem = 0;
  459. // purge the kill list.
  460. KillThemAllNow();
  461. hwndTemp = GetDlgItem (hDlg, ID);
  462. n = (int)SendMessage (hwndTemp, LVM_GETITEMCOUNT, 0, 0L);
  463. for(i = 0; i < n; i++)
  464. {
  465. item.iItem = i;
  466. if(SendMessage (hwndTemp, LVM_GETITEM, 0, (LPARAM) &item))
  467. {
  468. penvar = (ENVARS *) item.lParam;
  469. // if something changed.
  470. if(penvar->changed)
  471. {
  472. // if we need a new class object...
  473. if(penvar->objPath == NULL)
  474. {
  475. // must be a new one.
  476. CWbemClassObject cl = m_WbemServices.GetObject("Win32_Environment");
  477. inst = cl.SpawnInstance();
  478. }
  479. else
  480. {
  481. // get the old one.
  482. inst = m_WbemServices.GetObject(penvar->objPath);
  483. }
  484. if(!inst.IsNull())
  485. {
  486. if(ID == IDC_ENVVAR_SYS_LB_SYSVARS)
  487. {
  488. hr = inst.Put(sUserName, sSysUser);
  489. hr = inst.Put(sSysVar, true);
  490. }
  491. else if(ID == IDC_ENVVAR_SYS_LB_USERVARS)
  492. {
  493. hr = inst.Put(sUserName, m_currentUser);
  494. hr = inst.Put(sSysVar, false);
  495. }
  496. else
  497. {
  498. continue;
  499. }
  500. hr = inst.Put(sVarName, bstr_t(penvar->szValueName));
  501. hr = inst.Put(sVarVal, bstr_t(penvar->szExpValue));
  502. hr = m_WbemServices.PutInstance(inst);
  503. if (FAILED(hr))
  504. {
  505. hrFailure = hr;
  506. iFailure = i;
  507. }
  508. }
  509. } //endif changed
  510. }
  511. } //endfor
  512. if (FAILED(hrFailure))
  513. EnvVarErrorDialog(hDlg, IDS_ERR_ENVVAR_SAVE,
  514. IDC_ENVVAR_SYS_LB_SYSVARS ? (BSTR)sSysUser : (BSTR)m_currentUser,
  515. hrFailure);
  516. }
  517. ////////////////////////////////////////////////////////////////////////////
  518. // EmptyListView
  519. //
  520. // Frees memory allocated for environment variables
  521. //
  522. // History:
  523. // 19-Jan-1996 EricFlo Wrote it
  524. ////////////////////////////////////////////////////////////////////////////
  525. void EnvPage::EmptyListView(HWND hDlg, int ID)
  526. {
  527. int i, n;
  528. HWND hwndTemp;
  529. ENVARS *penvar;
  530. LV_ITEM item;
  531. // Free alloc'd strings and memory for list box items.
  532. hwndTemp = GetDlgItem (hDlg, ID);
  533. n = (int) SendMessage (hwndTemp, LVM_GETITEMCOUNT, 0, 0L);
  534. item.mask = LVIF_PARAM;
  535. item.iSubItem = 0;
  536. for(i = 0; i < n; i++)
  537. {
  538. item.iItem = i;
  539. if(SendMessage (hwndTemp, LVM_GETITEM, 0, (LPARAM) &item))
  540. {
  541. penvar = (ENVARS *) item.lParam;
  542. }
  543. else
  544. {
  545. penvar = NULL;
  546. }
  547. delete penvar;
  548. }
  549. ListView_DeleteAllItems(hwndTemp);
  550. }
  551. //------------------------------------------------------------
  552. void EnvPage::CleanUp (HWND hDlg)
  553. {
  554. EmptyListView(hDlg, IDC_ENVVAR_SYS_LB_USERVARS);
  555. EmptyListView(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS);
  556. }
  557. //----------------------------------------------------------------------
  558. void EnvPage::DeleteVar(HWND hDlg,
  559. UINT VarType,
  560. LPCTSTR szVarName)
  561. /*++
  562. Routine Description:
  563. Deletes an environment variable of a given name and type
  564. Arguments:
  565. hDlg -
  566. Supplies window handle
  567. VarType -
  568. Supplies variable type (user or system)
  569. szVarName -
  570. Supplies variable name
  571. Return Value:
  572. None, although it really should have one someday.
  573. --*/
  574. {
  575. // TCHAR szTemp2[MAX_PATH] = {0};
  576. TCHAR* pszName;
  577. int i, n;
  578. HWND hwndTemp;
  579. ENVARS *penvar;
  580. LV_ITEM item;
  581. pszName = new TCHAR[_tcslen(szVarName) +2];
  582. if (!pszName)
  583. return; // see comment about return value...
  584. CDeleteMe<TCHAR> delName(pszName);
  585. // Delete listbox entry that matches value in szVarName
  586. // If found, delete entry else ignore
  587. wsprintf(pszName, TEXT("%s"), szVarName);
  588. if(pszName[0] == TEXT('\0'))
  589. return;
  590. // Determine which Listbox to use (SYSTEM or USER vars)
  591. switch(VarType)
  592. {
  593. case SYSTEM_VAR:
  594. i = IDC_ENVVAR_SYS_LB_SYSVARS;
  595. break;
  596. case USER_VAR:
  597. default:
  598. i = IDC_ENVVAR_SYS_LB_USERVARS;
  599. break;
  600. } // switch (VarType)
  601. hwndTemp = GetDlgItem(hDlg, i);
  602. n = FindVar(hwndTemp, pszName);
  603. if(n != -1)
  604. {
  605. // Free existing strings (listbox and ours)
  606. item.mask = LVIF_PARAM;
  607. item.iItem = n;
  608. item.iSubItem = 0;
  609. if(SendMessage (hwndTemp, LVM_GETITEM, 0, (LPARAM) &item))
  610. {
  611. penvar = (ENVARS *) item.lParam;
  612. // if cimom knows about it...
  613. if(penvar->objPath != NULL)
  614. {
  615. // queue for later DeleteInstance().
  616. KillLater(penvar);
  617. if(m_bUserVars)
  618. m_currUserModified = true;
  619. else
  620. m_SysModified = true;
  621. }
  622. else // user must have added it and changed his mind..
  623. {
  624. // just forget about it.
  625. penvar = (ENVARS *) item.lParam;
  626. delete penvar;
  627. }
  628. }
  629. else
  630. {
  631. penvar = NULL;
  632. }
  633. SendMessage (hwndTemp, LVM_DELETEITEM, n, 0L);
  634. PropSheet_Changed(GetParent(hDlg), hDlg);
  635. // Fix selection state in listview
  636. if(n > 0)
  637. {
  638. n--;
  639. }
  640. item.mask = LVIF_STATE;
  641. item.iItem = n;
  642. item.iSubItem = 0;
  643. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  644. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  645. SendDlgItemMessage(hDlg, i,
  646. LVM_SETITEMSTATE, n, (LPARAM) &item);
  647. }
  648. }
  649. //----------------------------------------------------------------------
  650. void EnvPage::SetVar(HWND hDlg,
  651. UINT VarType,
  652. LPCTSTR szVarName,
  653. LPCTSTR szVarValue)
  654. /*++
  655. Routine Description:
  656. Given an environment variable's type (system or user), name, and value,
  657. creates a ENVVARS structure for that environment variable and inserts
  658. it into the proper list view control, deleteing any existing variable
  659. of the same name.
  660. Arguments:
  661. hDlg -
  662. Supplies window handle
  663. VarType -
  664. Supplies the type of the environment variable (system or user)
  665. szVarName -
  666. Supplies the name of the environment variable
  667. szVarValue -
  668. Supplies the value of the environment variable
  669. Return Value:
  670. None, although it really should have one someday.
  671. --*/
  672. {
  673. //TCHAR szTemp2[MAX_PATH] = {0};
  674. TCHAR* pszName;
  675. int i, n;
  676. TCHAR *bBuffer;
  677. TCHAR *pszTemp;
  678. LPTSTR pszString;
  679. HWND hwndTemp;
  680. int idTemp;
  681. ENVARS *penvar = NULL;
  682. LV_ITEM item;
  683. pszName = new TCHAR[_tcslen(szVarName) +2];
  684. if (!pszName)
  685. return; // see comment about return value...
  686. CDeleteMe<TCHAR> delName(pszName);
  687. wsprintf(pszName, TEXT("%s"), szVarName);
  688. // Strip trailing whitespace from end of Env Variable
  689. i = lstrlen(pszName) - 1;
  690. while(i >= 0)
  691. {
  692. if (_istspace(pszName[i]))
  693. pszName[i--] = TEXT('\0');
  694. else
  695. break;
  696. }
  697. // Make sure variable name does not contain the "=" sign.
  698. pszTemp = _tcspbrk (pszName, TEXT("="));
  699. if(pszTemp)
  700. *pszTemp = TEXT('\0');
  701. if(pszName[0] == TEXT('\0'))
  702. return;
  703. bBuffer = new TCHAR[BUFZ];
  704. pszString = (LPTSTR)new TCHAR[BUFZ];
  705. wsprintf(bBuffer, TEXT("%s"), szVarValue);
  706. // Determine which Listbox to use (SYSTEM or USER vars)
  707. switch (VarType)
  708. {
  709. case SYSTEM_VAR:
  710. idTemp = IDC_ENVVAR_SYS_LB_SYSVARS;
  711. break;
  712. case USER_VAR:
  713. default:
  714. idTemp = IDC_ENVVAR_SYS_LB_USERVARS;
  715. break;
  716. } // switch (VarType)
  717. hwndTemp = GetDlgItem(hDlg, idTemp);
  718. n = FindVar(hwndTemp, pszName);
  719. if (n != -1)
  720. {
  721. // Free existing strings (listview and ours)
  722. item.mask = LVIF_PARAM;
  723. item.iItem = n;
  724. item.iSubItem = 0;
  725. if(SendMessage(hwndTemp, LVM_GETITEM, 0, (LPARAM) &item))
  726. {
  727. // we're just changing an old one.
  728. penvar = (ENVARS *) item.lParam;
  729. delete penvar->szValueName;
  730. delete penvar->szValue;
  731. delete penvar->szExpValue;
  732. }
  733. else
  734. {
  735. penvar = NULL;
  736. }
  737. SendMessage (hwndTemp, LVM_DELETEITEM, n, 0L);
  738. }
  739. if(penvar == NULL)
  740. {
  741. // Get some storage for new EnVar.
  742. penvar = new ENVARS;
  743. if (penvar == NULL)
  744. return;
  745. penvar->userName = CloneString(m_currentUser);
  746. }
  747. if((m_bLocal == true) && ((VarType == SYSTEM_VAR) || (IsLoggedInUser(penvar->userName))))
  748. {
  749. ExpandEnvironmentStrings(bBuffer, pszString, BUFZ);
  750. }
  751. else
  752. {
  753. _tcscpy(pszString,bBuffer);
  754. }
  755. if (penvar == NULL)
  756. return;
  757. penvar->szValueName = CloneString(pszName);
  758. penvar->szValue = CloneString(bBuffer);
  759. penvar->szExpValue = CloneString(pszString);
  760. penvar->changed = true;
  761. item.mask = LVIF_TEXT | LVIF_PARAM;
  762. item.iItem = ListView_GetItemCount(hwndTemp);
  763. item.iSubItem = 0;
  764. item.pszText = penvar->szValueName;
  765. item.lParam = (LPARAM) penvar;
  766. n = (int) SendMessage(hwndTemp, LVM_INSERTITEM, 0, (LPARAM) &item);
  767. if (n != -1)
  768. {
  769. item.mask = LVIF_TEXT;
  770. item.iItem = n;
  771. item.iSubItem = 1;
  772. item.pszText = penvar->szExpValue;
  773. SendMessage(hwndTemp, LVM_SETITEMTEXT, n, (LPARAM) &item);
  774. item.mask = LVIF_STATE;
  775. item.iItem = n;
  776. item.iSubItem = 0;
  777. item.state = LVIS_SELECTED | LVIS_FOCUSED;
  778. item.stateMask = LVIS_SELECTED | LVIS_FOCUSED;
  779. SendDlgItemMessage(hDlg, idTemp,
  780. LVM_SETITEMSTATE, n, (LPARAM) &item);
  781. }
  782. delete bBuffer;
  783. delete pszString;
  784. if(m_bUserVars)
  785. m_currUserModified = true;
  786. else
  787. m_SysModified = true;
  788. }
  789. //----------------------------------------------------------------------
  790. void EnvPage::DoEdit(HWND hWnd,
  791. UINT VarType,
  792. UINT EditType,
  793. int iSelection)
  794. /*++
  795. Routine Description:
  796. Sets up for, executes, and cleans up after an Environment Variable
  797. New... or Edit... dialog. Called when user presses a New... or Edit...
  798. button.
  799. Arguments:
  800. hWnd -
  801. Supplies window handle
  802. VarType -
  803. Supplies the type of the variable: User (USER_VAR) or
  804. System (SYSTEM_VAR)
  805. EditType -
  806. Supplies the type of the edit: create New (NEW_VAR) or
  807. Edit existing (EDIT_VAR)
  808. iSelection -
  809. Supplies the currently selected variable of type VarType. This
  810. value is ignored if EditType is NEW_VAR.
  811. Return Value:
  812. None. May alter the contents of a list view control as a side effect.
  813. --*/
  814. {
  815. INT_PTR Result = 0;
  816. BOOL fVarChanged = FALSE;
  817. HWND hWndLB = NULL;
  818. ENVARS *penvar = NULL;
  819. g_VarType = VarType;
  820. g_EditType = EditType;
  821. penvar = GetVar(hWnd, VarType, iSelection);
  822. // init the edit dialog controls.
  823. switch(EditType)
  824. {
  825. case NEW_VAR:
  826. ZeroMemory((LPVOID) g_szVarName, (DWORD) BUFZ * sizeof(TCHAR));
  827. ZeroMemory((LPVOID) g_szVarValue, (DWORD) BUFZ * sizeof(TCHAR));
  828. break;
  829. case EDIT_VAR:
  830. if(penvar)
  831. {
  832. wsprintf(g_szVarName, TEXT("%s"), penvar->szValueName);
  833. wsprintf(g_szVarValue, TEXT("%s"), penvar->szValue);
  834. }
  835. else
  836. {
  837. MessageBeep(MB_ICONASTERISK);
  838. return;
  839. } // if
  840. break;
  841. case INVALID_EDIT_TYPE:
  842. default:
  843. return;
  844. } // switch
  845. // call the edit dialog.
  846. Result = DialogBox(HINST_THISDLL,
  847. (LPTSTR) MAKEINTRESOURCE(IDD_ENVVAREDIT),
  848. hWnd, EnvVarsEditDlg);
  849. // figure out what was changed.
  850. bool nameChanged = false;
  851. bool valueChanged = false;
  852. // Only update the list view control if the user
  853. // actually changed or created a variable
  854. switch (Result)
  855. {
  856. case EDIT_CHANGE:
  857. if(EDIT_VAR == EditType)
  858. {
  859. nameChanged = (lstrcmp(penvar->szValueName, g_szVarName) != 0);
  860. valueChanged = (lstrcmp(penvar->szValue, g_szVarValue) != 0);
  861. }
  862. else if(NEW_VAR == EditType)
  863. {
  864. nameChanged = (lstrlen(g_szVarName) != 0);
  865. valueChanged = (lstrlen(g_szVarValue) != 0);
  866. }
  867. // if the name changed, its a whole new wbem class object.
  868. if(nameChanged)
  869. {
  870. if(EDIT_VAR == EditType)
  871. {
  872. DeleteVar(hWnd, VarType, penvar->szValueName);
  873. }
  874. SetVar(hWnd, VarType, g_szVarName, g_szVarValue);
  875. }
  876. else if(valueChanged)
  877. {
  878. // keep the class object but change the value.
  879. SetVar(hWnd, VarType, g_szVarName, g_szVarValue);
  880. }
  881. // if anything changed...
  882. if(nameChanged || valueChanged)
  883. {
  884. // set the list's dirty flag.
  885. if(VarType == SYSTEM_VAR)
  886. {
  887. m_SysModified = true;
  888. }
  889. else if(VarType == USER_VAR)
  890. {
  891. m_currUserModified = true;
  892. }
  893. }
  894. break;
  895. default:
  896. break;
  897. } // endswitch (Result)
  898. g_VarType = INVALID_VAR_TYPE;
  899. g_EditType = INVALID_EDIT_TYPE;
  900. }
  901. //-------------------------------------------------
  902. EnvPage::ENVARS *EnvPage::GetVar(HWND hDlg,
  903. UINT VarType,
  904. int iSelection)
  905. /*++
  906. Routine Description:
  907. Returns a given System or User environment variable, as stored
  908. in the System or User environment variable listview control.
  909. Changing the structure returned by this routine is not
  910. recommended, because it will alter the values actually stored
  911. in the listview control.
  912. Arguments:
  913. hDlg -
  914. Supplies window handle
  915. VarType -
  916. Supplies variable type--System or User
  917. iSelection -
  918. Supplies the selection index into the listview control of
  919. the desired environment variable
  920. Return Value:
  921. Pointer to a valid ENVARS structure if successful.
  922. NULL if unsuccessful.
  923. --*/
  924. {
  925. HWND hWndLB = NULL;
  926. ENVARS *penvar = NULL;
  927. LV_ITEM item;
  928. switch(VarType)
  929. {
  930. case SYSTEM_VAR:
  931. hWndLB = GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS);
  932. break;
  933. case USER_VAR:
  934. hWndLB = GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_USERVARS);
  935. break;
  936. case INVALID_VAR_TYPE:
  937. default:
  938. return NULL;
  939. } // switch (VarType)
  940. item.mask = LVIF_PARAM;
  941. item.iItem = iSelection;
  942. item.iSubItem = 0;
  943. if (SendMessage (hWndLB, LVM_GETITEM, 0, (LPARAM) &item))
  944. {
  945. penvar = (ENVARS *) item.lParam;
  946. }
  947. else
  948. {
  949. penvar = NULL;
  950. }
  951. return(penvar);
  952. }
  953. //----------------------------------------------------------------------
  954. void EnvPage::DoCommand(HWND hDlg, HWND hwndCtl, int idCtl, int iNotify )
  955. {
  956. int i;
  957. ENVARS *penvar = NULL;
  958. switch (idCtl)
  959. {
  960. case IDOK:
  961. if(m_currUserModified)
  962. {
  963. Save(m_hDlg, IDC_ENVVAR_SYS_LB_USERVARS);
  964. m_currUserModified = false;
  965. }
  966. if(m_SysModified)
  967. {
  968. Save(m_hDlg, IDC_ENVVAR_SYS_LB_SYSVARS);
  969. m_SysModified = false;
  970. }
  971. EndDialog(hDlg, 0);
  972. break;
  973. case IDCANCEL:
  974. EndDialog(hDlg, 0);
  975. break;
  976. case IDC_ENVVAR_SYS_EDITSV:
  977. DoEdit(hDlg, SYSTEM_VAR, EDIT_VAR,
  978. GetSelectedItem(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS)));
  979. SetFocus(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS));
  980. break;
  981. case IDC_ENVVAR_SYS_EDITUV:
  982. DoEdit(hDlg, USER_VAR, EDIT_VAR,
  983. GetSelectedItem(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_USERVARS)));
  984. SetFocus(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_USERVARS));
  985. break;
  986. case IDC_ENVVAR_SYS_NEWSV:
  987. DoEdit(hDlg, SYSTEM_VAR, NEW_VAR, -1);
  988. SetFocus(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS));
  989. break;
  990. case IDC_ENVVAR_SYS_NEWUV:
  991. DoEdit(hDlg, USER_VAR, NEW_VAR, -1);
  992. SetFocus(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_USERVARS));
  993. break;
  994. case IDC_ENVVAR_SYS_DELSV:
  995. i = GetSelectedItem(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS));
  996. if(-1 != i)
  997. {
  998. penvar = GetVar(hDlg, SYSTEM_VAR, i);
  999. if (penvar)
  1000. DeleteVar(hDlg, SYSTEM_VAR, penvar->szValueName);
  1001. } // endif
  1002. SetFocus(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_SYSVARS));
  1003. break;
  1004. case IDC_ENVVAR_SYS_NDELUV:
  1005. i = GetSelectedItem(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_USERVARS));
  1006. if(-1 != i)
  1007. {
  1008. penvar = GetVar(hDlg, USER_VAR, i);
  1009. if (penvar)
  1010. DeleteVar(hDlg, USER_VAR, penvar->szValueName);
  1011. } // endif
  1012. SetFocus(GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_USERVARS));
  1013. break;
  1014. // the combobox of user names.
  1015. case IDC_USERLIST:
  1016. {//BEGIN
  1017. TCHAR userName[100] = {0};
  1018. bstr_t sNewUser, sThisName;
  1019. HRESULT hr = 0;
  1020. IWbemClassObject *envInst = NULL;
  1021. IEnumWbemClassObject *envEnum = NULL;
  1022. DWORD uReturned = 0;
  1023. bstr_t sUserName("UserName");
  1024. variant_t pVal;
  1025. int idx, changeMsg = IDNO;
  1026. if (iNotify == CBN_SELENDOK)
  1027. {
  1028. // see if the user want to save his changes.
  1029. if(m_currUserModified)
  1030. {
  1031. changeMsg = MsgBoxParam(m_hDlg,
  1032. IDS_CHANGINGUSER, IDS_TITLE,
  1033. MB_YESNOCANCEL | MB_ICONEXCLAMATION);
  1034. }
  1035. // well does he?
  1036. switch(changeMsg)
  1037. {
  1038. case IDCANCEL:
  1039. // stay put.
  1040. return;
  1041. case IDYES:
  1042. Save(m_hDlg, IDC_ENVVAR_SYS_LB_USERVARS);
  1043. // NOTE: after saving. allow to fall through to repopulate the list.
  1044. case IDNO:
  1045. // reset contents here.
  1046. EmptyListView(hDlg, IDC_ENVVAR_SYS_LB_USERVARS);
  1047. m_currUserModified = false;
  1048. // get new user's name.
  1049. idx = ComboBox_GetCurSel(hwndCtl);
  1050. if(ComboBox_GetLBText(hwndCtl, idx, userName))
  1051. {
  1052. sNewUser = userName;
  1053. HWND hwndUser = GetDlgItem(hDlg, IDC_ENVVAR_SYS_LB_USERVARS);
  1054. // load his variables.
  1055. if((hr = m_WbemServices.CreateInstanceEnum(bstr_t("Win32_Environment"),
  1056. WBEM_FLAG_SHALLOW,
  1057. &envEnum)) == S_OK)
  1058. {
  1059. // get the instance
  1060. while(SUCCEEDED(envEnum->Next(-1, 1, &envInst, &uReturned)) &&
  1061. (uReturned != 0))
  1062. {
  1063. // who's variable.
  1064. if (envInst->Get(sUserName, 0L, &pVal, NULL, NULL) == S_OK)
  1065. {
  1066. sThisName = V_BSTR(&pVal);
  1067. // setup for which list box gets this instance.
  1068. if(sThisName == sNewUser)
  1069. {
  1070. LoadUser(envInst, sThisName, hwndUser);
  1071. } //endif(sThisName == sNewUser)
  1072. } //endif (envInst->Get(sUserName,
  1073. envInst->Release();
  1074. } // endwhile envEnum
  1075. envEnum->Release();
  1076. } //endif CreateInstanceEnum()
  1077. } //endif(ComboBox_GetText
  1078. }//end switch(Messagebox())
  1079. } //endif (iNotify == CBN_SELCHANGE)
  1080. }//END
  1081. break;
  1082. default:
  1083. break;
  1084. }
  1085. }
  1086. //---------------------------------------------------------------------------
  1087. void EnvPage::LoadUser(IWbemClassObject *envInst,
  1088. bstr_t userName,
  1089. HWND hwndUser)
  1090. {
  1091. bstr_t sVarName("Name");
  1092. bstr_t sVarVal("VariableValue");
  1093. bstr_t sPath("__PATH");
  1094. bstr_t sSysUser("<SYSTEM>"); // magic string returned by provider
  1095. variant_t pVal, pVal1, pVal2;
  1096. ENVARS *penvar = NULL;
  1097. bstr_t pszValue;
  1098. bstr_t szTemp;
  1099. bstr_t objPath;
  1100. TCHAR pszString[MAX_VALUE_LEN] = {0};
  1101. int n;
  1102. LV_ITEM item;
  1103. DWORD dwIndex = 0;
  1104. m_currentUser = userName;
  1105. // get the variable.
  1106. if ((envInst->Get(sVarVal, 0L, &pVal, NULL, NULL) == S_OK) &&
  1107. (envInst->Get(sVarName, 0L, &pVal1, NULL, NULL) == S_OK) &&
  1108. (envInst->Get(sPath, 0L, &pVal2, NULL, NULL) == S_OK))
  1109. {
  1110. // extract.
  1111. pszValue = V_BSTR(&pVal);
  1112. szTemp = V_BSTR(&pVal1);
  1113. objPath = V_BSTR(&pVal2);
  1114. // store with list item.
  1115. penvar = new ENVARS;
  1116. if (penvar == NULL) //outofmemory
  1117. return;
  1118. penvar->objPath = CloneString(objPath);
  1119. penvar->userName = CloneString(userName);
  1120. penvar->szValueName = CloneString( szTemp );
  1121. penvar->szValue = CloneString( pszValue );
  1122. if((m_bLocal == true) && ((userName == sSysUser) || (IsLoggedInUser(userName))))
  1123. {
  1124. ExpandEnvironmentStrings(pszValue, pszString, MAX_VALUE_LEN);
  1125. }
  1126. else
  1127. {
  1128. _tcscpy(pszString,pszValue);
  1129. }
  1130. penvar->szExpValue = CloneString( pszString );
  1131. penvar->changed = false;
  1132. // put in first column value (name).
  1133. item.mask = LVIF_TEXT | LVIF_PARAM;
  1134. item.iItem = (dwIndex - 1);
  1135. item.iSubItem = 0;
  1136. item.pszText = penvar->szValueName;
  1137. item.lParam = (LPARAM) penvar;
  1138. n = (int)SendMessage(hwndUser, LVM_INSERTITEM, 0, (LPARAM) &item);
  1139. // did it go?
  1140. if (n != -1)
  1141. {
  1142. // do the second column value.
  1143. item.mask = LVIF_TEXT;
  1144. item.iItem = n;
  1145. item.iSubItem = 1;
  1146. item.pszText = penvar->szExpValue;
  1147. SendMessage(hwndUser, LVM_SETITEMTEXT, n, (LPARAM) &item);
  1148. }
  1149. }
  1150. }
  1151. //---------------------------------------------------------------------------
  1152. bool EnvPage::IsLoggedInUser(bstr_t userName)
  1153. {
  1154. TCHAR strUserName[1024];
  1155. TCHAR strDomain[1024];
  1156. _tcscpy(strDomain,_T(""));
  1157. DWORD dwSize = 1024;
  1158. DWORD dwDomSize = 1024;
  1159. DWORD dwSidSize = 0;
  1160. BYTE *buff;
  1161. SID *sid = NULL;
  1162. SID_NAME_USE sidName;
  1163. if (!userName)
  1164. return false;
  1165. GetUserName(strUserName,&dwSize);
  1166. LookupAccountName(NULL,strUserName,sid,&dwSidSize,strDomain,&dwDomSize,&sidName);
  1167. buff = new BYTE[dwSidSize];
  1168. if(buff == NULL)
  1169. return false;
  1170. sid = (SID *)buff;
  1171. BOOL bFlag = LookupAccountName(NULL,strUserName,sid,&dwSidSize,strDomain,&dwDomSize,&sidName);
  1172. delete []buff;
  1173. _tcscat(strDomain,_T("\\"));
  1174. _tcscat(strDomain,strUserName);
  1175. if(_tcsicmp(strDomain,userName) == 0)
  1176. return true;
  1177. else
  1178. return false;
  1179. }
  1180. //---------------------------------------------------------------------------
  1181. void EnvPage::GetLoggedinUser(bstr_t *userName)
  1182. {
  1183. TCHAR strUserName[1024];
  1184. TCHAR strDomain[1024];
  1185. _tcscpy(strDomain,_T(""));
  1186. DWORD dwSize = 1024;
  1187. DWORD dwDomSize = 1024;
  1188. DWORD dwSidSize = 0;
  1189. BYTE *buff;
  1190. SID *sid = NULL;
  1191. SID_NAME_USE sidName;
  1192. GetUserName(strUserName,&dwSize);
  1193. LookupAccountName(NULL,strUserName,sid,&dwSidSize,strDomain,&dwDomSize,&sidName);
  1194. buff = new BYTE[dwSidSize];
  1195. sid = (SID *)buff;
  1196. BOOL bFlag = LookupAccountName(NULL,strUserName,sid,&dwSidSize,strDomain,&dwDomSize,&sidName);
  1197. delete []buff;
  1198. _tcscat(strDomain,_T("\\"));
  1199. _tcscat(strDomain,strUserName);
  1200. *userName = strDomain;
  1201. }
  1202. //---------------------------------------------------------------------------
  1203. void EnvPage::KillLater(ENVARS *var)
  1204. {
  1205. // remember this guy.
  1206. m_killers.Add(var);
  1207. }
  1208. //---------------------------------------------------------------------------
  1209. void EnvPage::KillThemAllNow(void)
  1210. {
  1211. ENVARS *var = NULL;
  1212. if(m_killers.GetSize() > 0)
  1213. {
  1214. for(int it = 0; it < m_killers.GetSize(); it++)
  1215. {
  1216. var = m_killers[it];
  1217. if(var->objPath != NULL)
  1218. {
  1219. m_WbemServices.DeleteInstance(var->objPath);
  1220. }
  1221. delete var;
  1222. }
  1223. m_killers.RemoveAll();
  1224. }
  1225. }
  1226. //------------------------------------------------------------------------
  1227. int EnvPage::GetSelectedItem(HWND hCtrl)
  1228. {
  1229. int i, n;
  1230. n = (int)SendMessage(hCtrl, LVM_GETITEMCOUNT, 0, 0L);
  1231. if (n != LB_ERR)
  1232. {
  1233. for (i = 0; i < n; i++)
  1234. {
  1235. if (SendMessage(hCtrl, LVM_GETITEMSTATE,
  1236. i, (LPARAM) LVIS_SELECTED) == LVIS_SELECTED)
  1237. {
  1238. return i;
  1239. }
  1240. }
  1241. }
  1242. return -1;
  1243. }