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.

234 lines
5.1 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 "RebootPage.h"
  8. // avoid some warnings.
  9. #undef HDS_HORZ
  10. #undef HDS_BUTTONS
  11. #undef HDS_HIDDEN
  12. #include "resource.h"
  13. #include <windowsx.h>
  14. #include "..\common\util.h"
  15. #include "common.h"
  16. //----------------------------------------------------------------------------
  17. INT_PTR CALLBACK StaticRebootDlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
  18. {
  19. // if this is the initDlg msg...
  20. if(message == WM_INITDIALOG)
  21. {
  22. // transfer the 'this' ptr to the extraBytes.
  23. SetWindowLongPtr(hwndDlg, DWLP_USER, lParam);
  24. }
  25. // DWL_USER is the 'this' ptr.
  26. RebootPage *me = (RebootPage *)GetWindowLongPtr(hwndDlg, DWLP_USER);
  27. if(me != NULL)
  28. {
  29. // call into the DlgProc() that has some context.
  30. return me->DlgProc(hwndDlg, message, wParam, lParam);
  31. }
  32. else
  33. {
  34. return FALSE;
  35. }
  36. }
  37. //--------------------------------------------------------------
  38. RebootPage::RebootPage(WbemServiceThread *serviceThread)
  39. : WBEMPageHelper(serviceThread)
  40. {
  41. IWbemClassObject *pInst = NULL;
  42. pInst = FirstInstanceOf("Win32_OperatingSystem");
  43. if(pInst)
  44. {
  45. m_OS = pInst;
  46. }
  47. }
  48. //--------------------------------------------------------------
  49. INT_PTR RebootPage::DoModal(HWND hDlg)
  50. {
  51. return DialogBoxParam(HINST_THISDLL,
  52. (LPTSTR) MAKEINTRESOURCE(IDD_SHUTDOWN),
  53. hDlg, StaticRebootDlgProc, (LPARAM)this);
  54. }
  55. //--------------------------------------------------------------
  56. RebootPage::~RebootPage()
  57. {
  58. }
  59. //--------------------------------------------------------------
  60. INT_PTR CALLBACK RebootPage::DlgProc(HWND hwndDlg, UINT message, WPARAM wParam, LPARAM lParam)
  61. {
  62. m_hDlg = hwndDlg;
  63. switch (message)
  64. {
  65. case WM_INITDIALOG:
  66. Init(hwndDlg);
  67. break;
  68. case WM_COMMAND:
  69. switch (LOWORD(wParam))
  70. {
  71. case IDOK:
  72. if(HIWORD(wParam) == BN_CLICKED)
  73. {
  74. if(Doit(hwndDlg))
  75. {
  76. EndDialog(hwndDlg, IDOK);
  77. }
  78. }
  79. break;
  80. case IDCANCEL:
  81. EndDialog(hwndDlg, IDCANCEL);
  82. break;
  83. }
  84. break;
  85. case WM_HELP: // F1
  86. break;
  87. case WM_CONTEXTMENU: // right mouse click
  88. // WinHelp((HWND) wParam, HELP_FILE, HELP_CONTEXTMENU,
  89. // (DWORD)(LPSTR)aStartupHelpIds);
  90. break;
  91. default:
  92. return FALSE;
  93. }
  94. return TRUE;
  95. }
  96. //--------------------------------------------------------------
  97. void RebootPage::Init(HWND hDlg)
  98. {
  99. // set initial radio buttons.
  100. Button_SetCheck(GetDlgItem(hDlg, IDC_LOGOFF), BST_CHECKED);
  101. Button_SetCheck(GetDlgItem(hDlg, IDC_NEVER), BST_CHECKED);
  102. bstr_t version;
  103. if(m_OS == NULL)
  104. {
  105. return;
  106. }
  107. try
  108. {
  109. // if NT && greater >= 5.0....
  110. version = m_OS.GetString(_T("Version"));
  111. }
  112. catch(_com_error e)
  113. {
  114. return;
  115. }
  116. WCHAR major; int nMaj;
  117. if (!version)
  118. nMaj = 0;
  119. else
  120. {
  121. wcsncpy(&major, (wchar_t *)version, 1);
  122. nMaj = _wtoi(&major);
  123. }
  124. if(nMaj >= 5)
  125. {
  126. EnableWindow(GetDlgItem(hDlg, IDC_IFHUNG), TRUE);
  127. }
  128. }
  129. //-------------------------------------------------------------
  130. // NOTE: maps the flag bit to the radio button IDs.
  131. typedef struct
  132. {
  133. UINT bit;
  134. UINT ID;
  135. } FLAGMAP;
  136. FLAGMAP g_flagmap[] = {
  137. {EWX_LOGOFF, IDC_LOGOFF},
  138. {EWX_POWEROFF, IDC_POWERDOWN},
  139. {EWX_REBOOT, IDC_REBOOT},
  140. {EWX_SHUTDOWN, IDC_SHUTDOWN},
  141. {EWX_FORCE, IDC_ALWAYS},
  142. {/*EWX_FORCEIFHUNG*/ 0x10, IDC_IFHUNG}}; // needs NT5 hdr.
  143. bool RebootPage::Doit(HWND hDlg)
  144. {
  145. long flags = 0L;
  146. bstr_t path;
  147. HRESULT hr = 0;
  148. // find exactly ONE from the first 4...
  149. for(int i = 0; i <= 3; i++)
  150. {
  151. if(Button_GetCheck(GetDlgItem(hDlg, g_flagmap[i].ID)) & BST_CHECKED)
  152. {
  153. flags |= g_flagmap[i].bit;
  154. break; // found it; bail early.
  155. }
  156. }
  157. // and find ONE from the last 2.
  158. // NOTE: I dont check IDC_NEVER cuz that means 'no bit set'. Its just there
  159. // so the user can uncheck the last two.
  160. for(i = 4; i <= 5; i++)
  161. {
  162. if(Button_GetCheck(GetDlgItem(hDlg, g_flagmap[i].ID)) & BST_CHECKED)
  163. {
  164. flags |= g_flagmap[i].bit;
  165. break; // found it; bail early.
  166. }
  167. }
  168. // call the helper in the base class.
  169. long retval = 0;
  170. hr = Reboot(flags, &retval);
  171. if(FAILED(hr) || (retval != 0))
  172. {
  173. TCHAR format[100] = {0};
  174. TCHAR caption[100] = {0};
  175. ::LoadString(HINST_THISDLL,
  176. IDS_ERR_EXECMETHOD_CAPTION,
  177. caption, 100);
  178. ::LoadString(HINST_THISDLL,
  179. IDS_ERR_EXECMETHOD,
  180. format, 100);
  181. CHString errorDescription;
  182. CHString errorMessage;
  183. if(hr)
  184. {
  185. ErrorLookup(hr, errorDescription);
  186. errorMessage.Format(format, errorDescription);
  187. }
  188. else
  189. {
  190. ErrorLookup(retval, errorDescription);
  191. errorMessage.Format(format, errorDescription);
  192. // calling code gets confused if the 'retval' error isn't
  193. // reported back SOMEHOW.
  194. hr = E_FAIL;
  195. }
  196. ::MessageBox(hDlg, errorMessage, caption,
  197. MB_OK| MB_ICONEXCLAMATION);
  198. }
  199. return SUCCEEDED(hr);
  200. }