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.

193 lines
5.4 KiB

  1. /******************************************************************
  2. Copyright (C) 2001 Microsoft Corporation. All rights reserved.
  3. Author -- H.K. Sivasubramanian(siva.sub@wipro.com)
  4. TaskShutDown.CPP -- Shutdown and Restart of a system
  5. Description:
  6. Shuts down or restarts a system depending on the command line parameter
  7. ******************************************************************/
  8. #include <windows.h>
  9. #include <comdef.h>
  10. #include <stdio.h>
  11. #include <tchar.h>
  12. #include "appsrvcs.h"
  13. #include "taskctx.h"
  14. #include <iostream>
  15. #include <string>
  16. using namespace std;
  17. // Release the object once done with it
  18. #define SAFEIRELEASE(pIObj) \
  19. if (pIObj) \
  20. { \
  21. pIObj->Release(); \
  22. pIObj = NULL; \
  23. }
  24. // To handle all exceptions
  25. #define ONFAILTHROWERROR(hr) \
  26. { \
  27. if (FAILED(hr)) \
  28. throw _com_error(hr); \
  29. }
  30. // 84DA8800-CB46-11D2-BF23-00105A1F3461
  31. const CLSID CLSID_TaskContext = { 0x84DA8800, 0xCB46, 0x11D2, { 0xBF, 0x23, 0x00, 0x10, 0x5A, 0x1F, 0x34, 0x61 } };
  32. // ----
  33. const CLSID IID_ITaskContext = { 0x96C637B0, 0xB8DE, 0x11D2, { 0xA9, 0x1C, 0x00, 0xAA, 0x00, 0xA7, 0x1D, 0xCA } };
  34. // 1BF00631-CB9E-11D2-90C3-00AA00A71DCA
  35. const CLSID CLSID_ApplianceServices = { 0x1BF00631, 0xCB9E, 0x11D2, { 0x90, 0xC3, 0x00, 0xAA, 0x00, 0xA7, 0x1D, 0xCA } };
  36. // 408B0460-B8E5-11D2-A91C-00AA00A71DCA
  37. const CLSID IID_IApplianceServices = { 0x408B0460, 0xB8E5, 0x11D2, { 0xA9, 0x1C, 0x00, 0xAA, 0x00, 0xA7, 0x1D, 0xCA } };
  38. #define NULL_STRING L"\0"
  39. extern "C"
  40. int __cdecl wmain(int argc, wchar_t *argv[])
  41. {
  42. // Checking whether the necessary command line arguments are supplied
  43. if (argc == 2)
  44. {
  45. HRESULT hr = S_OK;
  46. // Interface pointer to the ITaskContext interface
  47. ITaskContext *pITContext = NULL;
  48. // Interface pointer to the IApplianceServices interface
  49. IApplianceServices *pIASvcs = NULL;
  50. BSTR bstrParam = NULL;
  51. try
  52. {
  53. VARIANT vPowerOff, vValue;
  54. VariantInit(&vPowerOff);
  55. VariantInit(&vValue);
  56. ///////////////////////////////////////////////////////////////////
  57. // Call CoInitialize to initialize the COM library and then
  58. // CoCreateInstance to get the ITaskContext object
  59. // CoCreateInstance to get the IApplianceServices object
  60. ///////////////////////////////////////////////////////////////////
  61. hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
  62. ONFAILTHROWERROR(hr);
  63. // Creates a single uninitialized object of ITaskContext.
  64. hr = CoCreateInstance(CLSID_TaskContext,
  65. NULL,
  66. CLSCTX_INPROC_SERVER,
  67. IID_ITaskContext,
  68. (void **) &pITContext);
  69. // Exception handling
  70. ONFAILTHROWERROR(hr);
  71. // Creates a single uninitialized object of IApplianceServices
  72. hr = CoCreateInstance(CLSID_ApplianceServices,
  73. NULL,
  74. CLSCTX_INPROC_SERVER,
  75. IID_IApplianceServices,
  76. (void **) &pIASvcs);
  77. // Exception handling
  78. ONFAILTHROWERROR(hr);
  79. ///////////////////////////////////////////////////////////////////
  80. // Call IApplianceServices::Initialize and then
  81. // IApplianceServices::ExecuteTaskAsync to execute Shutdown method
  82. ///////////////////////////////////////////////////////////////////
  83. vPowerOff.vt = VT_BSTR;
  84. wstring wsOption(argv[1]);
  85. wstring wsShutdown(L"SHUTDOWN");
  86. wstring wsRestart(L"RESTART");
  87. if( _wcsicmp(wsOption.c_str(), wsShutdown.c_str()) == 0 )
  88. {
  89. cout << "Invoking Shutdown Task" << endl;
  90. vPowerOff.bstrVal = ::SysAllocString(L"1");
  91. }
  92. else
  93. {
  94. if( _wcsicmp(wsOption.c_str(),wsRestart.c_str()) == 0 )
  95. {
  96. cout << "Invoking Restart Task" << endl;
  97. vPowerOff.bstrVal = ::SysAllocString(L"0");;
  98. }
  99. else
  100. {
  101. cout << "Unrecognized option: " << (const char*)_bstr_t(argv[1]) << endl;
  102. exit(-1);
  103. }
  104. }
  105. // Set the 'Method Name'
  106. bstrParam = ::SysAllocString(L"Method Name");
  107. vValue.vt = VT_BSTR;
  108. vValue.bstrVal = ::SysAllocString(L"ShutDownAppliance");
  109. hr = pITContext->SetParameter(bstrParam, &vValue);
  110. ONFAILTHROWERROR(hr);
  111. ::SysFreeString(bstrParam);
  112. VariantClear(&vValue);
  113. // Set the 'Sleep Duration'
  114. bstrParam = ::SysAllocString(L"SleepDuration");
  115. vValue.vt = VT_I4;
  116. // Sleep time is '17 seconds
  117. vValue.lVal = 17000;
  118. hr = pITContext->SetParameter(bstrParam, &vValue);
  119. ONFAILTHROWERROR(hr);
  120. ::SysFreeString(bstrParam);
  121. VariantClear(&vValue);
  122. // Set the 'PowerOff'
  123. bstrParam = ::SysAllocString(L"PowerOff");
  124. hr = pITContext->SetParameter(bstrParam, &vPowerOff);
  125. ONFAILTHROWERROR(hr);
  126. ::SysFreeString(bstrParam);
  127. VariantClear(&vPowerOff);
  128. // Initialize the Applicance Services object
  129. hr = pIASvcs->Initialize();
  130. ONFAILTHROWERROR(hr);
  131. // ExecuteTaskAsync
  132. bstrParam = ::SysAllocString(L"ApplianceShutdownTask");
  133. hr = pIASvcs->ExecuteTaskAsync(bstrParam, pITContext);
  134. ONFAILTHROWERROR(hr);
  135. ::SysFreeString(bstrParam);
  136. SAFEIRELEASE(pITContext);
  137. SAFEIRELEASE(pIASvcs);
  138. }
  139. catch(_com_error& e)
  140. {
  141. wprintf(L"ERROR:\n\tCode = 0x%x\n\tDescription = %s\n", e.Error(),
  142. (LPWSTR) e.Description());
  143. ::SysFreeString(bstrParam);
  144. SAFEIRELEASE(pITContext);
  145. SAFEIRELEASE(pIASvcs);
  146. }
  147. }
  148. else
  149. {
  150. wprintf(L"ERROR: Invalid Usage.\nUSAGE: taskshutdown.exe <RESTART|SHUTDOWN>\n");
  151. }
  152. return 0;
  153. }