Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1165 lines
41 KiB

  1. /** FILE: ups.c ********* Module Header ********************************
  2. *
  3. * Control panel applet for UPS configuration.
  4. * This applet let the user to specify the ups capabilities, including:
  5. * - signalling on power failure
  6. * - ability to turn itself off
  7. * - signal low battery power
  8. * - battery life and recharge time
  9. * - signal voltage
  10. * It also allows the user to specify the time of first notification
  11. * and the notification interval thereafter
  12. *
  13. * History:
  14. * 1pm on Wed 08 Apr 1992 -by- Mark Cliggett [markcl]
  15. * Created
  16. * Saturday 05 Sept 1992 -by- Congpa You [congpay]
  17. * Rewrite the code for the new UPS dialog.
  18. *
  19. * Copyright (C) 1992 Microsoft Corporation
  20. *
  21. *************************************************************************/
  22. //==========================================================================
  23. // Include files
  24. //==========================================================================
  25. // C Runtime
  26. #define _CTYPE_DISABLE_MACROS
  27. #include <stddef.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. // Application specific
  32. #include "ups.h"
  33. //==========================================================================
  34. // Local Functions
  35. //==========================================================================
  36. /* EnableCONFIG enable/disable the UPS configuration groupbox
  37. * and the comport combobox if bVal = TRUE/FALSE.
  38. */
  39. void EnableCONFIG (HWND hDlg, BOOL bVal)
  40. {
  41. EnableWindow (GetDlgItem (hDlg, IDD_UPS_PORTCB), bVal);
  42. EnableWindow (GetDlgItem (hDlg, IDD_UPS_UPSGROUP), bVal);
  43. EnableWindow (GetDlgItem (hDlg, IDD_UPS_TEXT), bVal);
  44. EnableWindow (GetDlgItem (hDlg, IDD_UPS_PFSIGNAL), bVal);
  45. EnableWindow (GetDlgItem (hDlg, IDD_UPS_LOWBATTERY), bVal);
  46. EnableWindow (GetDlgItem (hDlg, IDD_UPS_TURNOFF), bVal);
  47. EnableWindow (GetDlgItem (hDlg, IDD_UPS_COMMANDFILE), bVal);
  48. EnableWindow (GetDlgItem (hDlg, IDD_UPS_SIGN), bVal);
  49. EnableWindow (GetDlgItem (hDlg, IDD_UPS_TURNOFFHIGH), bVal);
  50. EnableWindow (GetDlgItem (hDlg, IDD_UPS_TURNOFFLOW), bVal);
  51. }
  52. /* EnablePFSINGNAL deals with power failure signal checkbox and radiobutton. */
  53. void EnablePFSIGNAL (HWND hDlg, BOOL bVal)
  54. {
  55. EnableWindow (GetDlgItem (hDlg, IDD_UPS_PFSIGNALHIGH), bVal);
  56. EnableWindow (GetDlgItem (hDlg, IDD_UPS_PFSIGNALLOW), bVal);
  57. }
  58. /* EnableLOWBATTERY deals with low battery signal checkbox and radiobutton. */
  59. void EnableLOWBATTERY (HWND hDlg, BOOL bVal)
  60. {
  61. EnableWindow (GetDlgItem (hDlg, IDD_UPS_LOWBATTERYHIGH), bVal);
  62. EnableWindow (GetDlgItem (hDlg, IDD_UPS_LOWBATTERYLOW), bVal);
  63. }
  64. /* EnableFILENAME deals with the Filename text field. */
  65. void EnableFILENAME (HWND hDlg, BOOL bVal)
  66. {
  67. EnableWindow (GetDlgItem (hDlg, IDD_UPS_FILETEXT), bVal);
  68. EnableWindow (GetDlgItem (hDlg, IDD_UPS_FILENAME), bVal);
  69. }
  70. /* EnableCHARACTER deals with the UPS characteristics groupbox. */
  71. void EnableCHARACTER (HWND hDlg, BOOL bVal)
  72. {
  73. EnableWindow (GetDlgItem (hDlg, IDD_UPS_CHARACTER), bVal);
  74. EnableWindow (GetDlgItem (hDlg, IDD_UPS_BLTEXT1), bVal);
  75. EnableWindow (GetDlgItem (hDlg, IDD_UPS_BLEDIT), bVal);
  76. EnableWindow (GetDlgItem (hDlg, IDD_UPS_BATTERYLIFE), bVal);
  77. EnableWindow (GetDlgItem (hDlg, IDD_UPS_BLTEXT2), bVal);
  78. EnableWindow (GetDlgItem (hDlg, IDD_UPS_RPMTEXT1), bVal);
  79. EnableWindow (GetDlgItem (hDlg, IDD_UPS_RPMEDIT), bVal);
  80. EnableWindow (GetDlgItem (hDlg, IDD_UPS_RECHARGEPERMINUTE), bVal);
  81. EnableWindow (GetDlgItem (hDlg, IDD_UPS_RPMTEXT2), bVal);
  82. }
  83. /* EnableSERVICE deals with the UPS service groupbox. */
  84. void EnableSERVICE (HWND hDlg, BOOL bVal)
  85. {
  86. EnableWindow (GetDlgItem (hDlg, IDD_UPS_SERVICE), bVal);
  87. EnableWindow (GetDlgItem (hDlg, IDD_UPS_FWTEXT1), bVal);
  88. EnableWindow (GetDlgItem (hDlg, IDD_UPS_FWEDIT), bVal);
  89. EnableWindow (GetDlgItem (hDlg, IDD_UPS_FIRSTWARNING), bVal);
  90. EnableWindow (GetDlgItem (hDlg, IDD_UPS_FWTEXT2), bVal);
  91. EnableWindow (GetDlgItem (hDlg, IDD_UPS_WITEXT1), bVal);
  92. EnableWindow (GetDlgItem (hDlg, IDD_UPS_WIEDIT), bVal);
  93. EnableWindow (GetDlgItem (hDlg, IDD_UPS_WARNINGINTERVAL), bVal);
  94. EnableWindow (GetDlgItem (hDlg, IDD_UPS_WITEXT2), bVal);
  95. }
  96. /* Show draws the UPS dialog according to the UPS configuation.
  97. * There are only 8 cases because there are three check box in
  98. * UPS configuration groupbox.
  99. */
  100. void Show (HWND hDlg, ULONG ulOptions)
  101. {
  102. switch (ulOptions) {
  103. case UPS_POWERFAILSIGNAL:
  104. CheckDlgButton (hDlg, IDD_UPS_PFSIGNAL, TRUE);
  105. EnablePFSIGNAL (hDlg, TRUE);
  106. EnableLOWBATTERY (hDlg, FALSE);
  107. EnableCHARACTER (hDlg, TRUE);
  108. EnableSERVICE (hDlg, TRUE);
  109. break;
  110. case UPS_LOWBATTERYSIGNAL:
  111. CheckDlgButton (hDlg, IDD_UPS_LOWBATTERY, TRUE);
  112. EnablePFSIGNAL (hDlg, FALSE);
  113. EnableLOWBATTERY (hDlg, TRUE);
  114. EnableCHARACTER (hDlg, FALSE);
  115. EnableSERVICE (hDlg, FALSE);
  116. break;
  117. case UPS_CANTURNOFF:
  118. CheckDlgButton (hDlg, IDD_UPS_TURNOFF, TRUE);
  119. EnablePFSIGNAL (hDlg, FALSE);
  120. EnableLOWBATTERY (hDlg, FALSE);
  121. EnableCHARACTER (hDlg, FALSE);
  122. EnableSERVICE (hDlg, FALSE);
  123. break;
  124. case UPS_POWERFAILSIGNAL | UPS_LOWBATTERYSIGNAL:
  125. CheckDlgButton (hDlg, IDD_UPS_PFSIGNAL, TRUE);
  126. CheckDlgButton (hDlg, IDD_UPS_LOWBATTERY, TRUE);
  127. EnablePFSIGNAL (hDlg, TRUE);
  128. EnableLOWBATTERY (hDlg, TRUE);
  129. EnableCHARACTER (hDlg, FALSE);
  130. EnableSERVICE (hDlg, TRUE);
  131. break;
  132. case UPS_POWERFAILSIGNAL | UPS_CANTURNOFF:
  133. CheckDlgButton (hDlg, IDD_UPS_PFSIGNAL, TRUE);
  134. CheckDlgButton (hDlg, IDD_UPS_TURNOFF, TRUE);
  135. EnablePFSIGNAL (hDlg, TRUE);
  136. EnableLOWBATTERY (hDlg, FALSE);
  137. EnableCHARACTER (hDlg, TRUE);
  138. EnableSERVICE (hDlg, TRUE);
  139. break;
  140. case UPS_LOWBATTERYSIGNAL | UPS_CANTURNOFF:
  141. CheckDlgButton (hDlg, IDD_UPS_LOWBATTERY, TRUE);
  142. CheckDlgButton (hDlg, IDD_UPS_TURNOFF, TRUE);
  143. EnablePFSIGNAL (hDlg, FALSE);
  144. EnableLOWBATTERY (hDlg, TRUE);
  145. EnableCHARACTER (hDlg, FALSE);
  146. EnableSERVICE (hDlg, FALSE);
  147. break;
  148. case UPS_POWERFAILSIGNAL | UPS_LOWBATTERYSIGNAL | UPS_CANTURNOFF:
  149. CheckDlgButton (hDlg, IDD_UPS_PFSIGNAL, TRUE);
  150. CheckDlgButton (hDlg, IDD_UPS_LOWBATTERY, TRUE);
  151. CheckDlgButton (hDlg, IDD_UPS_TURNOFF, TRUE);
  152. EnablePFSIGNAL (hDlg, TRUE);
  153. EnableLOWBATTERY (hDlg, TRUE);
  154. EnableCHARACTER (hDlg, FALSE);
  155. EnableSERVICE (hDlg, TRUE);
  156. break;
  157. default: // All checkboxes in configuration groupbox are not checked.
  158. EnablePFSIGNAL (hDlg, FALSE);
  159. EnableLOWBATTERY (hDlg, FALSE);
  160. EnableCHARACTER (hDlg, FALSE);
  161. EnableSERVICE (hDlg, FALSE);
  162. }
  163. }
  164. // Local function. Used by UPSDlg. and ErrorOut.
  165. void ShowError (HWND hDlg,
  166. DWORD dwError)
  167. {
  168. TCHAR szErrorMessage[LONGBZ];
  169. if (!FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
  170. NULL,
  171. dwError,
  172. 0,
  173. szErrorMessage,
  174. LONGBZ,
  175. NULL))
  176. {
  177. MessageBox (hDlg, szErrMem, szCtlPanel, MB_OK|MB_ICONSTOP); //szErrMem is loaded in cpl.c.
  178. }
  179. else
  180. {
  181. MessageBox (hDlg, szErrorMessage, szCtlPanel, MB_OK|MB_ICONSTOP);
  182. }
  183. }
  184. // Local Function. Used by UPSDlg.
  185. BOOL ErrorOut (HWND hDlg,
  186. DWORD dwError,
  187. SC_HANDLE hups,
  188. SC_HANDLE hsc,
  189. HKEY vhKey)
  190. {
  191. ShowError(hDlg, dwError);
  192. CloseServiceHandle(hups);
  193. CloseServiceHandle(hsc);
  194. RegCloseKey(vhKey);
  195. EndDialog(hDlg, 0L);
  196. return(FALSE);
  197. }
  198. // Called after trying to start or stop service. Return TRUE if it gets to
  199. // the final state. Otherwise return FALSE
  200. BOOL FinishCheck (DWORD dwFinalState,
  201. SERVICE_STATUS * pss,
  202. SC_HANDLE hups)
  203. {
  204. int max_tries = MAXTRIES;
  205. int i = 0;
  206. DWORD sleep_time;
  207. DWORD old_checkpoint = 0;
  208. DWORD new_checkpoint = 0;
  209. DWORD dwStartState;
  210. DWORD dwPendingState;
  211. if (dwFinalState == SERVICE_STOPPED)
  212. {
  213. dwStartState = SERVICE_RUNNING;
  214. dwPendingState = SERVICE_STOP_PENDING;
  215. }
  216. else if (dwFinalState == SERVICE_RUNNING)
  217. {
  218. dwStartState = SERVICE_STOPPED;
  219. dwPendingState = SERVICE_START_PENDING;
  220. }
  221. else
  222. return(FALSE);
  223. while ((pss->dwCurrentState != dwFinalState) &&
  224. (i++ < max_tries))
  225. {
  226. if (!QueryServiceStatus (hups, pss))
  227. return(FALSE);
  228. if (pss->dwCurrentState != dwPendingState)
  229. break;
  230. new_checkpoint = pss->dwCheckPoint;
  231. if (old_checkpoint != new_checkpoint)
  232. {
  233. sleep_time = pss->dwWaitHint;
  234. if (sleep_time > SLEEP_TIME)
  235. {
  236. max_tries = ((3 * sleep_time)/SLEEP_TIME);
  237. sleep_time = SLEEP_TIME;
  238. i = 0;
  239. }
  240. }
  241. else
  242. sleep_time = SLEEP_TIME;
  243. old_checkpoint = new_checkpoint;
  244. Sleep (sleep_time);
  245. }
  246. if (pss->dwCurrentState != dwFinalState)
  247. {
  248. return(FALSE);
  249. }
  250. return(TRUE);
  251. }
  252. BOOL IsFilename (TCHAR * szFilename)
  253. {
  254. if (strchr (szFilename, '\\') == NULL)
  255. return(TRUE);
  256. else
  257. return(FALSE);
  258. }
  259. BOOL IsExecutable (TCHAR * szFilename)
  260. {
  261. szFilename += (strlen(szFilename) -4);
  262. if ((_strnicmp (szFilename, ".cmd", 5) == 0) ||
  263. (_strnicmp (szFilename, ".com", 5) == 0) ||
  264. (_strnicmp (szFilename, ".exe", 5) == 0) ||
  265. (_strnicmp (szFilename, ".bat", 5) == 0))
  266. {
  267. return(TRUE);
  268. }
  269. else
  270. return(FALSE);
  271. }
  272. //==========================================================================
  273. // Local Data Declarations
  274. //==========================================================================
  275. TCHAR szUpsReg[] = "System\\CurrentControlSet\\Services\\UPS";
  276. TCHAR szPort[] = "Port";
  277. TCHAR szPortDefault[]= "COM1:";
  278. TCHAR szOptions[] = "Options";
  279. TCHAR szBatteryLife[] = "BatteryLife";
  280. TCHAR szRechargePerMinute[] = "RechargeRate";
  281. TCHAR szFirstWarning[] = "FirstMessageDelay";
  282. TCHAR szWarningInterval[] = "MessageInterval";
  283. TCHAR szCommandFile[] = "CommandFile";
  284. TCHAR szCOM[] = "COM";
  285. TCHAR szCOLON[] = ":";
  286. HKEY vhKey; // handle to registry node
  287. WNDPROC lpDefWndProc;
  288. ARROWVSCROLL avs[4] = { { 1, -1, 5, -5, 720, 2, 12, 12 },
  289. { 1, -1, 5, -5, 250, 1, 30, 30 },
  290. { 1, -1, 5, -5, 120, 0, 30, 30 },
  291. { 1, -1, 4, -4, 300, 5, 0, 0 } };
  292. LRESULT NewEditWndProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  293. {
  294. switch (message)
  295. {
  296. case WM_CHAR:
  297. if ((wParam < L'0' || wParam > L'9') &&
  298. (wParam != VK_BACK) &&
  299. (wParam != VK_DELETE) &&
  300. (wParam != VK_END) &&
  301. (wParam != VK_HOME))
  302. {
  303. MessageBeep (0);
  304. return TRUE;
  305. }
  306. default:
  307. break;
  308. }
  309. return(CallWindowProc(lpDefWndProc, hDlg, message, wParam, lParam));
  310. }
  311. //==========================================================================
  312. // UPSDlg is the dlgproc for ups dialog.
  313. //==========================================================================
  314. INT_PTR UPSDlg (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  315. {
  316. int n;
  317. int selection;
  318. int num;
  319. int focusid;
  320. int id;
  321. int i;
  322. int RetVal;
  323. int rc;
  324. DWORD ulOptions;
  325. DWORD ulBatteryLife;
  326. DWORD ulRechargePerMinute;
  327. DWORD ulFirstWarning;
  328. DWORD ulWarningInterval;
  329. DWORD cb;
  330. DWORD dwStart;
  331. DWORD dwError;
  332. DWORD type;
  333. TCHAR * pMemory;
  334. TCHAR * pszKeyName;
  335. TCHAR * pszAug;
  336. TCHAR szTemport[PORTLEN];
  337. TCHAR szTemp[LONGBZ];
  338. TCHAR szFilename[PATHMAX];
  339. TCHAR szErrorMessage[10*LONGBZ];
  340. TCHAR szSysDir[PATHMAX];
  341. TCHAR szNum[SHORTBZ];
  342. TCHAR szStatus[MIDBZ];
  343. BOOL bOK;
  344. BOOL fUpsSelected = FALSE;
  345. static BOOL bPFSIGNAL = FALSE;
  346. static BOOL bLOWBATTERY = FALSE;
  347. static BOOL bTURNOFF = FALSE;
  348. static BOOL bFILENAME = FALSE;
  349. SC_HANDLE hsc;
  350. SC_HANDLE hups;
  351. SERVICE_STATUS ss;
  352. WIN32_FIND_DATA ffd;
  353. HANDLE hFile;
  354. LONG lpfnWndProc;
  355. switch (message)
  356. {
  357. case WM_INITDIALOG:
  358. HourGlass (TRUE);
  359. // Read UPS info from registry
  360. //rc = RegOpenKey(HKEY_LOCAL_MACHINE, szUpsReg, &vhKey);
  361. rc = RegOpenKey(HKEY_LOCAL_MACHINE, szUpsReg, &vhKey);
  362. if (rc == ERROR_ACCESS_DENIED)
  363. {
  364. MyMessageBox (hDlg, UPS_ACCESS_ERROR, CPCAPTION, MB_OK|MB_ICONSTOP);
  365. RegCloseKey (vhKey);
  366. EndDialog (hDlg, 0L);
  367. return(FALSE);
  368. }
  369. else if (rc)
  370. {
  371. HourGlass (FALSE);
  372. MyMessageBox (hDlg, UPS_REGISTRY_ERROR, CPCAPTION, MB_OK|MB_ICONSTOP);
  373. RegCloseKey (vhKey);
  374. EndDialog (hDlg, 0L);
  375. return(FALSE);
  376. }
  377. // get UPS values
  378. cb = sizeof(ULONG);
  379. if (RegQueryValueEx(vhKey, szOptions, NULL, &type,
  380. (LPTSTR)&ulOptions, &cb))
  381. // if no data exists, UPS is not installed
  382. ulOptions = 0;
  383. cb = sizeof(ulOptions);
  384. if (RegQueryValueEx(vhKey, szBatteryLife, NULL, &type,
  385. (LPTSTR)&ulBatteryLife, &cb))
  386. ulBatteryLife = DEFAULTBATTERYLIFE;
  387. cb = sizeof(ulOptions);
  388. if (RegQueryValueEx(vhKey, szRechargePerMinute, NULL, &type,
  389. (LPTSTR)&ulRechargePerMinute, &cb))
  390. ulRechargePerMinute = DEFAULTRECHARGEPERMINUTE;
  391. cb = sizeof(ulOptions);
  392. if (RegQueryValueEx(vhKey, szFirstWarning, NULL, &type,
  393. (LPTSTR)&ulFirstWarning, &cb))
  394. ulFirstWarning = DEFAULTFIRSTWARNING;
  395. cb = sizeof(ulOptions);
  396. if (RegQueryValueEx(vhKey, szWarningInterval, NULL, &type,
  397. (LPTSTR)&ulWarningInterval, &cb))
  398. ulWarningInterval = DEFAULTWARNINGINTERVAL;
  399. cb = sizeof (szTemport) / sizeof (szTemport[0]);
  400. if (RegQueryValueEx(vhKey, szPort, NULL, &type, szTemport, &cb))
  401. szTemport[0] = 0;
  402. cb = sizeof (szFilename) / sizeof (szFilename[0]);
  403. if (RegQueryValueEx(vhKey, szCommandFile, NULL, &type, szFilename, &cb))
  404. szFilename[0] = 0;
  405. // check to see if the current user can change settings
  406. if (RegSetValueEx(vhKey, szOptions, 0, REG_DWORD,
  407. (LPSTR)&ulOptions, sizeof(ulOptions)) ==
  408. ERROR_ACCESS_DENIED)
  409. {
  410. MyMessageBox(hDlg, UPS_ACCESS_ERROR, CPCAPTION, MB_OK|MB_ICONSTOP);
  411. RegCloseKey (vhKey);
  412. EndDialog (hDlg, 0L);
  413. return(FALSE);
  414. }
  415. SetDlgItemText (hDlg, IDD_UPS_FILENAME, szFilename);
  416. // Show recorded time values in registry.
  417. _itoa (ulBatteryLife, szNum, 10);
  418. SetDlgItemText (hDlg, IDD_UPS_BLEDIT, (LPSTR)szNum);
  419. _itoa (ulRechargePerMinute, szNum, 10);
  420. SetDlgItemText (hDlg, IDD_UPS_RPMEDIT, (LPSTR)szNum);
  421. _itoa (ulFirstWarning, szNum, 10);
  422. SetDlgItemText (hDlg, IDD_UPS_FWEDIT, (LPSTR)szNum);
  423. _itoa (ulWarningInterval, szNum, 10);
  424. SetDlgItemText (hDlg, IDD_UPS_WIEDIT, (LPSTR)szNum);
  425. // Show recorded signal voltage.
  426. CheckRadioButton(hDlg, IDD_UPS_PFSIGNALHIGH, IDD_UPS_PFSIGNALLOW,
  427. ulOptions & UPS_POWERFAIL_LOW ?
  428. IDD_UPS_PFSIGNALLOW : IDD_UPS_PFSIGNALHIGH);
  429. CheckRadioButton(hDlg, IDD_UPS_LOWBATTERYHIGH, IDD_UPS_LOWBATTERYLOW,
  430. ulOptions & UPS_LOWBATTERY_LOW ?
  431. IDD_UPS_LOWBATTERYLOW : IDD_UPS_LOWBATTERYHIGH);
  432. CheckRadioButton(hDlg, IDD_UPS_TURNOFFHIGH, IDD_UPS_TURNOFFLOW,
  433. ulOptions & UPS_TURNOFF_LOW ?
  434. IDD_UPS_TURNOFFLOW : IDD_UPS_TURNOFFHIGH);
  435. // Show port names from WIN.INI [ports] section
  436. SendDlgItemMessage (hDlg, IDD_UPS_PORTCB, CB_RESETCONTENT, 0, 0L);
  437. if (!(pszKeyName = pMemory = AllocMem(KEYBZ)))
  438. {
  439. MessageBox (hDlg, szErrMem, szCtlPanel, MB_OK|MB_ICONSTOP);
  440. RegCloseKey (vhKey);
  441. EndDialog (hDlg, 0L);
  442. return(FALSE);
  443. }
  444. GetProfileString ("ports", NULL, (LPSTR) szPortDefault, (LPSTR) pszKeyName, KEYBZ);
  445. n = selection = 0;
  446. while (*pszKeyName)
  447. {
  448. // Check if the string is COM?:
  449. if ((_strnicmp (pszKeyName, szCOM, 3) == 0) &&
  450. (isdigit (*(pszKeyName + 3))) &&
  451. (_strnicmp (pszKeyName + 4, szCOLON, 1) == 0))
  452. {
  453. SendDlgItemMessage(hDlg, IDD_UPS_PORTCB, CB_INSERTSTRING,
  454. (WPARAM)-1, (LPARAM)pszKeyName);
  455. if (!strcmp (pszKeyName, szTemport))
  456. selection = n;
  457. n++;
  458. }
  459. SendMessage (GetDlgItem(hDlg, IDD_UPS_PORTCB),
  460. CB_SETCURSEL, selection, 0L);
  461. /* Point to next string in buffer */
  462. pszKeyName += strlen (pszKeyName) + sizeof(TCHAR);
  463. }
  464. FreeMem(pMemory, KEYBZ);
  465. // Display other fields properly.
  466. if (ulOptions & UPS_INSTALLED)
  467. {
  468. CheckDlgButton (hDlg, IDD_UPS_EXISTS, TRUE);
  469. EnableCONFIG (hDlg, TRUE);
  470. // MASK gets the choice in configuration groupbox.
  471. Show (hDlg, ulOptions & MASK);
  472. // Enable command file selection?
  473. if (ulOptions & UPS_COMMANDFILE)
  474. {
  475. CheckDlgButton (hDlg, IDD_UPS_COMMANDFILE, TRUE);
  476. EnableFILENAME (hDlg, TRUE);
  477. }
  478. else
  479. EnableFILENAME (hDlg, FALSE);
  480. }
  481. // Set the ranges of the edit fields
  482. SendMessage(GetDlgItem(hDlg, IDD_UPS_FILENAME),
  483. EM_LIMITTEXT,
  484. PATHMAX-1,
  485. 0L);
  486. SendMessage(GetDlgItem(hDlg, IDD_UPS_BLEDIT),
  487. EM_LIMITTEXT,
  488. 3,
  489. 0L);
  490. SendMessage(GetDlgItem(hDlg, IDD_UPS_RPMEDIT),
  491. EM_LIMITTEXT,
  492. 3,
  493. 0L);
  494. SendMessage(GetDlgItem(hDlg, IDD_UPS_FWEDIT),
  495. EM_LIMITTEXT,
  496. 3,
  497. 0L);
  498. SendMessage(GetDlgItem(hDlg, IDD_UPS_WIEDIT),
  499. EM_LIMITTEXT,
  500. 3,
  501. 0L);
  502. // Change the window proc of the edit fields.
  503. lpDefWndProc = (WNDPROC)GetWindowLongPtr(GetDlgItem(hDlg, IDD_UPS_BLEDIT),
  504. GWLP_WNDPROC);
  505. SetWindowLongPtr (GetDlgItem(hDlg, IDD_UPS_BLEDIT),
  506. GWLP_WNDPROC,
  507. (LONG_PTR)NewEditWndProc);
  508. SetWindowLongPtr (GetDlgItem(hDlg, IDD_UPS_RPMEDIT),
  509. GWLP_WNDPROC,
  510. (LONG_PTR)NewEditWndProc);
  511. SetWindowLongPtr (GetDlgItem(hDlg, IDD_UPS_FWEDIT),
  512. GWLP_WNDPROC,
  513. (LONG_PTR)NewEditWndProc);
  514. SetWindowLongPtr (GetDlgItem(hDlg, IDD_UPS_WIEDIT),
  515. GWLP_WNDPROC,
  516. (LONG_PTR)NewEditWndProc);
  517. if (!(ulOptions & UPS_INSTALLED))
  518. {
  519. EnableCONFIG (hDlg, FALSE);
  520. EnablePFSIGNAL (hDlg, FALSE);
  521. EnableLOWBATTERY (hDlg, FALSE);
  522. EnableCHARACTER (hDlg, FALSE);
  523. EnableSERVICE (hDlg, FALSE);
  524. EnableFILENAME (hDlg, FALSE);
  525. EnableWindow (GetDlgItem (hDlg, IDD_UPS_SIGN), FALSE);
  526. }
  527. HourGlass (FALSE);
  528. break;
  529. case WM_VSCROLL:
  530. focusid = GetWindowLong(GetFocus(), GWL_ID);
  531. switch (HIWORD(wParam))
  532. {
  533. case IDD_UPS_BATTERYLIFE:
  534. id = IDD_UPS_BLEDIT;
  535. i = 0;
  536. break;
  537. case IDD_UPS_RECHARGEPERMINUTE:
  538. id = IDD_UPS_RPMEDIT;
  539. i = 1;
  540. break;
  541. case IDD_UPS_FIRSTWARNING:
  542. id = IDD_UPS_FWEDIT;
  543. i = 2;
  544. break;
  545. case IDD_UPS_WARNINGINTERVAL:
  546. id = IDD_UPS_WIEDIT;
  547. i = 3;
  548. break;
  549. default:
  550. return (FALSE);
  551. }
  552. if (focusid != id)
  553. SetFocus(GetDlgItem(hDlg, id));
  554. switch(LOWORD(wParam))
  555. {
  556. case SB_THUMBTRACK:
  557. case SB_ENDSCROLL:
  558. return (TRUE);
  559. break;
  560. default:
  561. num = GetDlgItemInt (hDlg, id, &bOK, FALSE);
  562. num = ArrowVScrollProc (LOWORD(wParam), (short)num,
  563. (LPARROWVSCROLL) (avs + i));
  564. _itoa (num, szNum, 10);
  565. SetDlgItemText (hDlg, id, (LPSTR)szNum);
  566. SendDlgItemMessage (hDlg, id, EM_SETSEL, 0, -1L);
  567. break;
  568. }
  569. break;
  570. case WM_COMMAND:
  571. // Initialize ulOptions to 0 in order to avoid side effect.
  572. ulOptions = 0;
  573. switch (LOWORD(wParam))
  574. {
  575. case IDD_UPS_EXISTS:
  576. // Enable/Disable UPS options controls.
  577. if (!IsDlgButtonChecked (hDlg, IDD_UPS_EXISTS))
  578. {
  579. if (IsDlgButtonChecked (hDlg, IDD_UPS_PFSIGNAL))
  580. {
  581. bPFSIGNAL = TRUE;
  582. }
  583. if (IsDlgButtonChecked (hDlg, IDD_UPS_LOWBATTERY))
  584. {
  585. bLOWBATTERY = TRUE;
  586. }
  587. if (IsDlgButtonChecked (hDlg, IDD_UPS_TURNOFF))
  588. {
  589. bTURNOFF = TRUE;
  590. }
  591. if (IsDlgButtonChecked (hDlg, IDD_UPS_COMMANDFILE))
  592. {
  593. bFILENAME = TRUE;
  594. }
  595. SendMessage (GetDlgItem (hDlg, IDD_UPS_PFSIGNAL), BM_SETCHECK, FALSE, 0L);
  596. SendMessage (GetDlgItem (hDlg, IDD_UPS_LOWBATTERY), BM_SETCHECK, FALSE, 0L);
  597. SendMessage (GetDlgItem (hDlg, IDD_UPS_TURNOFF), BM_SETCHECK, FALSE, 0L);
  598. SendMessage (GetDlgItem (hDlg, IDD_UPS_COMMANDFILE), BM_SETCHECK, FALSE, 0L);
  599. EnableCONFIG (hDlg, FALSE);
  600. EnablePFSIGNAL (hDlg, FALSE);
  601. EnableLOWBATTERY (hDlg, FALSE);
  602. EnableCHARACTER (hDlg, FALSE);
  603. EnableSERVICE (hDlg, FALSE);
  604. EnableFILENAME (hDlg, FALSE);
  605. }
  606. else
  607. {
  608. EnableCONFIG (hDlg, TRUE);
  609. if (bPFSIGNAL)
  610. {
  611. ulOptions |= UPS_POWERFAILSIGNAL;
  612. }
  613. if (bLOWBATTERY)
  614. {
  615. ulOptions |= UPS_LOWBATTERYSIGNAL;
  616. }
  617. if (bTURNOFF)
  618. {
  619. ulOptions |= UPS_CANTURNOFF;
  620. }
  621. Show (hDlg, (ulOptions & MASK));
  622. CheckDlgButton (hDlg, IDD_UPS_COMMANDFILE, bFILENAME);
  623. EnableFILENAME (hDlg, bFILENAME);
  624. }
  625. break;
  626. case IDD_UPS_PFSIGNAL:
  627. case IDD_UPS_LOWBATTERY:
  628. case IDD_UPS_TURNOFF:
  629. // Show the config properly.
  630. if (IsDlgButtonChecked (hDlg, IDD_UPS_PFSIGNAL))
  631. {
  632. bPFSIGNAL = TRUE;
  633. ulOptions |= UPS_POWERFAILSIGNAL;
  634. }
  635. else
  636. bPFSIGNAL = FALSE;
  637. if (IsDlgButtonChecked (hDlg, IDD_UPS_LOWBATTERY))
  638. {
  639. bLOWBATTERY = TRUE;
  640. ulOptions |= UPS_LOWBATTERYSIGNAL;
  641. }
  642. else
  643. bLOWBATTERY = FALSE;
  644. if (IsDlgButtonChecked (hDlg, IDD_UPS_TURNOFF))
  645. {
  646. bTURNOFF = TRUE;
  647. ulOptions |= UPS_CANTURNOFF;
  648. }
  649. else
  650. bTURNOFF = FALSE;
  651. Show (hDlg, (ulOptions & MASK));
  652. break;
  653. case IDD_UPS_COMMANDFILE:
  654. if (IsDlgButtonChecked (hDlg, IDD_UPS_COMMANDFILE))
  655. {
  656. bFILENAME = TRUE;
  657. ulOptions |= UPS_COMMANDFILE;
  658. EnableFILENAME (hDlg, TRUE);
  659. }
  660. else
  661. {
  662. bFILENAME = FALSE;
  663. EnableFILENAME (hDlg, FALSE);
  664. }
  665. break;
  666. case IDD_UPS_PFSIGNALHIGH:
  667. case IDD_UPS_PFSIGNALLOW:
  668. case IDD_UPS_LOWBATTERYHIGH:
  669. case IDD_UPS_LOWBATTERYLOW:
  670. case IDD_UPS_TURNOFFHIGH:
  671. case IDD_UPS_TURNOFFLOW:
  672. case IDD_UPS_BATTERYLIFE:
  673. case IDD_UPS_RECHARGEPERMINUTE:
  674. case IDD_UPS_FIRSTWARNING:
  675. case IDD_UPS_WARNINGINTERVAL:
  676. break;
  677. case IDOK:
  678. ulOptions = 0;
  679. HourGlass (TRUE);
  680. // Get the current status of UPS service.
  681. if (!(hsc = OpenSCManager(NULL, NULL, GENERIC_ALL)) ||
  682. !(hups = OpenService(hsc, "UPS", GENERIC_ALL)) ||
  683. !(QueryServiceStatus(hups, &ss)) )
  684. {
  685. dwError = GetLastError();
  686. return( ErrorOut (hDlg, dwError, hups, hsc, vhKey));
  687. }
  688. // Check whether the parameters are set correctly.
  689. if (IsDlgButtonChecked (hDlg, IDD_UPS_EXISTS)) // If UPS is selected.
  690. {
  691. // If neither Power failure signal nor Low battery signal is select,
  692. // popup a message box to tell the use to select one.
  693. if (!(IsDlgButtonChecked (hDlg, IDD_UPS_PFSIGNAL)) &&
  694. !(IsDlgButtonChecked (hDlg, IDD_UPS_LOWBATTERY)))
  695. {
  696. MyMessageBox(hDlg, UPS_OPTIONS_ERROR, CPCAPTION, MB_OK|MB_ICONSTOP);
  697. break;
  698. }
  699. }
  700. else // If UPS is not selected set static variable bPFSIGNAL etc to 0.
  701. {
  702. bPFSIGNAL = FALSE;
  703. bLOWBATTERY = FALSE;
  704. bTURNOFF = FALSE;
  705. bFILENAME = FALSE;
  706. }
  707. //Get ulOptions
  708. if (IsDlgButtonChecked (hDlg, IDD_UPS_PFSIGNAL))
  709. ulOptions |= UPS_POWERFAILSIGNAL;
  710. if (IsDlgButtonChecked (hDlg, IDD_UPS_LOWBATTERY))
  711. ulOptions |= UPS_LOWBATTERYSIGNAL;
  712. if (IsDlgButtonChecked (hDlg, IDD_UPS_TURNOFF))
  713. ulOptions |= UPS_CANTURNOFF;
  714. if (IsDlgButtonChecked (hDlg, IDD_UPS_PFSIGNALLOW))
  715. ulOptions |= UPS_POWERFAIL_LOW;
  716. if (IsDlgButtonChecked (hDlg, IDD_UPS_LOWBATTERYLOW))
  717. ulOptions |= UPS_LOWBATTERY_LOW;
  718. if (IsDlgButtonChecked (hDlg, IDD_UPS_TURNOFFLOW))
  719. ulOptions |= UPS_TURNOFF_LOW;
  720. if (IsDlgButtonChecked (hDlg, IDD_UPS_COMMANDFILE))
  721. ulOptions |= UPS_COMMANDFILE;
  722. fUpsSelected = IsDlgButtonChecked (hDlg, IDD_UPS_EXISTS);
  723. if (fUpsSelected)
  724. {
  725. ulOptions |= UPS_INSTALLED;
  726. }
  727. //Get the UPS parameters
  728. GetDlgItemText(hDlg, IDD_UPS_PORTCB, szTemport, PORTLEN);
  729. GetDlgItemText(hDlg, IDD_UPS_FWEDIT, szNum, 10);
  730. ulFirstWarning = atoi(szNum);
  731. GetDlgItemText(hDlg, IDD_UPS_WIEDIT, szNum, 10);
  732. ulWarningInterval = atoi(szNum);
  733. GetDlgItemText(hDlg, IDD_UPS_BLEDIT, szNum, 10);
  734. ulBatteryLife = atoi(szNum);
  735. GetDlgItemText(hDlg, IDD_UPS_RPMEDIT, szNum, 10);
  736. ulRechargePerMinute = atoi(szNum);
  737. if (fUpsSelected && (ulOptions & UPS_POWERFAILSIGNAL))
  738. {
  739. // Give warning if ulFirstWarning is not in the valid range.
  740. if ((ulFirstWarning < (ULONG) avs[2].bottom) | (ulFirstWarning > (ULONG) avs[2].top))
  741. {
  742. MyMessageBox (hDlg, UPS_FWRange, CPCAPTION, MB_OK|MB_ICONSTOP);
  743. SetFocus (GetDlgItem (hDlg, IDD_UPS_FWEDIT));
  744. SendMessage(GetDlgItem (hDlg, IDD_UPS_FWEDIT),
  745. EM_SETSEL,
  746. 0,
  747. -1L);
  748. break;
  749. }
  750. // Give warning if ulWarningInterval is not in the valid range.
  751. if ((ulWarningInterval < (ULONG) avs[3].bottom) | (ulWarningInterval > (ULONG) avs[3].top))
  752. {
  753. MyMessageBox (hDlg, UPS_WIRange, CPCAPTION, MB_OK|MB_ICONSTOP);
  754. SetFocus (GetDlgItem (hDlg, IDD_UPS_WIEDIT));
  755. SendMessage(GetDlgItem (hDlg, IDD_UPS_WIEDIT),
  756. EM_SETSEL,
  757. 0,
  758. -1L);
  759. break;
  760. }
  761. }
  762. if (fUpsSelected &&
  763. (ulOptions & UPS_POWERFAILSIGNAL) &&
  764. !(ulOptions & UPS_LOWBATTERYSIGNAL))
  765. {
  766. // Give warning if ulBatteryLife is not in the valid range.
  767. if ((ulBatteryLife < (ULONG) avs[0].bottom) | (ulBatteryLife > (ULONG) avs[0].top))
  768. {
  769. MyMessageBox (hDlg, UPS_BLRange, CPCAPTION, MB_OK|MB_ICONSTOP);
  770. SetFocus (GetDlgItem (hDlg, IDD_UPS_BLEDIT));
  771. SendMessage(GetDlgItem (hDlg, IDD_UPS_BLEDIT),
  772. EM_SETSEL,
  773. 0,
  774. -1L);
  775. break;
  776. }
  777. // Give warning if ulRechargePerMinute is not in the valid range.
  778. if ((ulRechargePerMinute < (ULONG) avs[1].bottom) | (ulRechargePerMinute > (ULONG) avs[1].top))
  779. {
  780. MyMessageBox (hDlg, UPS_RPMRange, CPCAPTION, MB_OK|MB_ICONSTOP);
  781. SetFocus (GetDlgItem (hDlg, IDD_UPS_RPMEDIT));
  782. SendMessage(GetDlgItem (hDlg, IDD_UPS_RPMEDIT),
  783. EM_SETSEL,
  784. 0,
  785. -1L);
  786. break;
  787. }
  788. // Give warning if set first warning comes later then battery life.
  789. if (ulFirstWarning > ulBatteryLife * 60)
  790. {
  791. MyMessageBox(hDlg, UPS_FW_WARNING, CPCAPTION, MB_OK|MB_ICONSTOP);
  792. SetFocus (GetDlgItem (hDlg, IDD_UPS_FWEDIT));
  793. SendMessage(GetDlgItem (hDlg, IDD_UPS_FWEDIT),
  794. EM_SETSEL,
  795. 0,
  796. -1L);
  797. break;
  798. }
  799. // Give warning if set warning interval time longer than battery life.
  800. if (ulWarningInterval > ulBatteryLife * 60)
  801. {
  802. MyMessageBox(hDlg, UPS_DELAY_WARNING, CPCAPTION, MB_OK|MB_ICONSTOP);
  803. SetFocus (GetDlgItem (hDlg, IDD_UPS_WIEDIT));
  804. SendMessage(GetDlgItem (hDlg, IDD_UPS_WIEDIT),
  805. EM_SETSEL,
  806. 0,
  807. -1L);
  808. break;
  809. }
  810. }
  811. if (fUpsSelected && (ulOptions & UPS_COMMANDFILE))
  812. {
  813. GetDlgItemText(hDlg, IDD_UPS_FILENAME, szFilename, PATHMAX);
  814. if (!IsFilename (szFilename))
  815. {
  816. MyMessageBox(hDlg, UPS_INVALID_PATH, CPCAPTION, MB_OK|MB_ICONSTOP);
  817. SetFocus (GetDlgItem (hDlg, IDD_UPS_FILENAME));
  818. SendMessage(GetDlgItem (hDlg, IDD_UPS_FILENAME),
  819. EM_SETSEL,
  820. 0,
  821. -1L);
  822. break;
  823. }
  824. if (!IsExecutable (szFilename))
  825. {
  826. MyMessageBox(hDlg, UPS_INVALID_FILENAME, CPCAPTION, MB_OK|MB_ICONSTOP);
  827. SetFocus (GetDlgItem (hDlg, IDD_UPS_FILENAME));
  828. SendMessage(GetDlgItem (hDlg, IDD_UPS_FILENAME),
  829. EM_SETSEL,
  830. 0,
  831. -1L);
  832. break;
  833. }
  834. // Check if the file is in the system directory.
  835. n = GetSystemDirectory(szSysDir, PATHMAX);
  836. if ((n == 0) || (n > PATHMAX))
  837. {
  838. MyMessageBox(hDlg, UPS_CANT_FIND_SYSDIR, CPCAPTION, MB_OK|MB_ICONSTOP);
  839. break;
  840. }
  841. else
  842. {
  843. strcpy (szTemp, szSysDir);
  844. strcat (szTemp, "\\");
  845. strcat (szTemp, szFilename);
  846. hFile = FindFirstFile (szTemp, &ffd);
  847. if (hFile == INVALID_HANDLE_VALUE)
  848. {
  849. if (!LoadString (hModule, UPS_FILE_NOT_EXIST, szTemp, sizeof(szTemp)))
  850. {
  851. ErrLoadString (hDlg);
  852. break;
  853. }
  854. pszAug = szSysDir;
  855. if (!FormatMessage (FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
  856. szTemp,
  857. 0,
  858. 0,
  859. szErrorMessage,
  860. LONGBZ,
  861. (va_list *)&(pszAug)))
  862. {
  863. ShowError (hDlg, GetLastError());
  864. }
  865. else
  866. {
  867. MessageBox (hDlg, szErrorMessage, szCtlPanel, MB_OK|MB_ICONSTOP);
  868. }
  869. SetFocus (GetDlgItem (hDlg, IDD_UPS_FILENAME));
  870. SendMessage(GetDlgItem (hDlg, IDD_UPS_FILENAME),
  871. EM_SETSEL,
  872. 0,
  873. -1L);
  874. break;
  875. }
  876. else
  877. {
  878. FindClose (hFile);
  879. }
  880. }
  881. RegSetValueEx(vhKey, szCommandFile, 0, REG_SZ, szFilename, (strlen(szFilename)+1));
  882. }
  883. // Write the configuration to registry.
  884. RegSetValueEx(vhKey, szPort, 0, REG_SZ, szTemport, (strlen(szTemport)+1));
  885. RegSetValueEx(vhKey, szOptions, 0, REG_DWORD,
  886. (LPSTR)&ulOptions, sizeof(ulOptions));
  887. RegSetValueEx(vhKey, szBatteryLife, 0, REG_DWORD,
  888. (LPSTR)&ulBatteryLife, sizeof(ulBatteryLife));
  889. RegSetValueEx(vhKey, szRechargePerMinute, 0, REG_DWORD,
  890. (LPSTR)&ulRechargePerMinute, sizeof(ulRechargePerMinute));
  891. RegSetValueEx(vhKey, szFirstWarning, 0, REG_DWORD,
  892. (LPSTR)&ulFirstWarning, sizeof(ulFirstWarning));
  893. RegSetValueEx(vhKey, szWarningInterval, 0, REG_DWORD,
  894. (LPSTR)&ulWarningInterval, sizeof(ulWarningInterval));
  895. // Start or stop the service according to user's choice.
  896. // If user select UPS, and the service is not running, start it.
  897. if (ss.dwCurrentState == SERVICE_STOPPED && fUpsSelected)
  898. {
  899. RetVal = MyMessageBox (hDlg, UPS_START_MSG, CPCAPTION, MB_YESNOCANCEL|MB_ICONINFORMATION);
  900. if (RetVal == IDCANCEL)
  901. {
  902. break;
  903. }
  904. if (RetVal == IDYES)
  905. {
  906. if (!StartService (hups, 0, NULL))
  907. {
  908. dwError = GetLastError();
  909. return (ErrorOut (hDlg, dwError, hups, hsc, vhKey));
  910. }
  911. if (!FinishCheck (SERVICE_RUNNING, &ss, hups))
  912. {
  913. if (MyMessageBox (hDlg, UPS_STARTFAIL_MSG, CPCAPTION, MB_OKCANCEL|MB_ICONSTOP)
  914. == IDOK)
  915. {
  916. break;
  917. }
  918. }
  919. }
  920. }
  921. // If the service is running, stop it first.
  922. else if ((ss.dwCurrentState == SERVICE_RUNNING) ||
  923. (ss.dwCurrentState == SERVICE_PAUSED))
  924. {
  925. RetVal = MyMessageBox (hDlg,
  926. fUpsSelected? UPS_RESTART_MSG: UPS_STOP_MSG,
  927. CPCAPTION,
  928. MB_YESNOCANCEL|MB_ICONINFORMATION);
  929. if (RetVal == IDCANCEL)
  930. {
  931. break;
  932. }
  933. if (RetVal == IDYES)
  934. {
  935. // Stop the service first.
  936. if (!ControlService (hups, SERVICE_CONTROL_STOP, &ss))
  937. {
  938. dwError = GetLastError();
  939. return( ErrorOut (hDlg, dwError, hups, hsc, vhKey));
  940. }
  941. if (!FinishCheck (SERVICE_STOPPED, &ss, hups))
  942. {
  943. if (MyMessageBox (hDlg, UPS_STOPFAIL_MSG, CPCAPTION, MB_OKCANCEL|MB_ICONSTOP)
  944. == IDCANCEL)
  945. {
  946. break;
  947. }
  948. else
  949. fUpsSelected = FALSE; // For error break out.
  950. }
  951. // If user selected UPS, start it again. Otherwise exit.
  952. if (fUpsSelected)
  953. {
  954. if (!StartService (hups, 0, NULL))
  955. {
  956. dwError = GetLastError();
  957. return (ErrorOut (hDlg, dwError, hups, hsc, vhKey));
  958. }
  959. if (!FinishCheck (SERVICE_RUNNING, &ss, hups))
  960. {
  961. if (MyMessageBox (hDlg, UPS_STARTFAIL_MSG, CPCAPTION, MB_OKCANCEL|MB_ICONSTOP)
  962. == IDOK)
  963. {
  964. break;
  965. }
  966. }
  967. }
  968. }
  969. }
  970. // If the service is in pending state. ask user to use Services applet.
  971. else if ((ss.dwCurrentState == SERVICE_START_PENDING) ||
  972. (ss.dwCurrentState == SERVICE_STOP_PENDING) ||
  973. (ss.dwCurrentState == SERVICE_CONTINUE_PENDING) ||
  974. (ss.dwCurrentState == SERVICE_PAUSE_PENDING))
  975. {
  976. if (MyMessageBox (hDlg, UPS_PENDING_MSG, CPCAPTION, MB_OKCANCEL|MB_ICONSTOP)
  977. == IDCANCEL)
  978. break;
  979. }
  980. // If the service in Unknow state, report error.
  981. else
  982. MyMessageBox (hDlg, UPS_UNKNOWNSTATE_MSG, CPCAPTION, MB_OK|MB_ICONSTOP);
  983. // Set UPS service to be auto start if the UPS installed box is checked.
  984. // Set UPS service to be manual start if the installed box is not checked.
  985. dwStart = fUpsSelected? SERVICE_AUTO_START : SERVICE_DEMAND_START;
  986. if (!ChangeServiceConfig (hups,
  987. SERVICE_NO_CHANGE,
  988. dwStart,
  989. SERVICE_ERROR_NORMAL,
  990. NULL,
  991. NULL,
  992. NULL,
  993. NULL,
  994. NULL,
  995. NULL,
  996. NULL))
  997. {
  998. dwError = GetLastError();
  999. return (ErrorOut (hDlg, dwError, hups, hsc, vhKey));
  1000. }
  1001. // Close opened services handles.
  1002. CloseServiceHandle(hups);
  1003. CloseServiceHandle(hsc);
  1004. // Close registry handle and exit dialog.
  1005. RegCloseKey(vhKey);
  1006. EndDialog (hDlg, 0L);
  1007. break;
  1008. case IDCANCEL:
  1009. RegCloseKey(vhKey);
  1010. EndDialog (hDlg, 0L);
  1011. break;
  1012. case IDD_HELP:
  1013. CPHelp(hDlg);
  1014. break;
  1015. default:
  1016. break;
  1017. }
  1018. break;
  1019. default:
  1020. if (message == wHelpMessage)
  1021. {
  1022. CPHelp(hDlg);
  1023. }
  1024. else
  1025. return FALSE;
  1026. break;
  1027. }
  1028. return(TRUE);
  1029. }
  1030. // Turn hourglass on or off
  1031. void HourGlass (BOOL bOn)
  1032. {
  1033. if (!GetSystemMetrics(SM_MOUSEPRESENT))
  1034. ShowCursor(bOn);
  1035. SetCursor(LoadCursor(NULL, bOn ? IDC_WAIT : IDC_ARROW));
  1036. }