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.

112 lines
3.4 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright (c) 1997-2001 Microsoft Corporation, All Rights Reserved
  4. //
  5. // RunDll.cpp
  6. //
  7. // Purpose: Allow framework to be used to run a command
  8. //
  9. //***************************************************************************
  10. #include "precomp.h"
  11. // This routine is meant to be called from RUNDLL32.EXE
  12. extern "C" {
  13. __declspec(dllexport) VOID CALLBACK
  14. DoCmd(HWND hwnd, HINSTANCE hinst, LPSTR lpszCmdLine, int nCmdShow)
  15. {
  16. DWORD dwRet = WBEM_E_OUT_OF_MEMORY;
  17. BOOL bRet = FALSE;
  18. char *pBuff = (char *)calloc(strlen(lpszCmdLine) + 1, sizeof(char));
  19. if (pBuff)
  20. {
  21. dwRet = WBEM_E_FAILED;
  22. try
  23. {
  24. // Parse the passed in command line to figure out what command we
  25. // are being asked to run.
  26. sscanf(lpszCmdLine, "%s ", pBuff);
  27. dwRet = ERROR_INVALID_FUNCTION;
  28. // Find out which command
  29. if (_stricmp(pBuff, "ExitWindowsEx") == 0)
  30. {
  31. // Parse out the parameters for this command
  32. DWORD dwFlags, dwReserved;
  33. if (sscanf(lpszCmdLine, "%s %d %d ", pBuff, &dwFlags, &dwReserved) == 3)
  34. {
  35. // Clear the error (it appears ExitWindowsEx doesn't always clear old data)
  36. SetLastError(0);
  37. bRet = ExitWindowsEx(dwFlags, dwReserved);
  38. dwRet = GetLastError();
  39. }
  40. }
  41. else if (_stricmp(pBuff, "InitiateSystemShutdown") == 0)
  42. {
  43. // Parse out the parameters for this command
  44. DWORD dwFlags, dwReserved;
  45. bool bRebootAfterShutdown = false;
  46. bool bForceShutDown = false;
  47. if (sscanf(lpszCmdLine, "%s %d %d ", pBuff, &dwFlags, &dwReserved) == 3)
  48. {
  49. // Clear the error (it appears ExitWindowsEx doesn't always clear old data)
  50. SetLastError(0);
  51. if(dwFlags & EWX_REBOOT)
  52. {
  53. bRebootAfterShutdown = true;
  54. }
  55. if( dwFlags & EWX_FORCE)
  56. {
  57. bForceShutDown = true;
  58. }
  59. WCHAR wstrComputerName[MAX_COMPUTERNAME_LENGTH + 1] = { '\0' };
  60. DWORD dwSize;
  61. if(::GetComputerName(wstrComputerName, &dwSize))
  62. {
  63. bRet = InitiateSystemShutdown(
  64. wstrComputerName,
  65. NULL,
  66. 0 /* dwTimeout */,
  67. (bForceShutDown)? TRUE:FALSE,
  68. (bRebootAfterShutdown)? TRUE:FALSE );
  69. dwRet = GetLastError();
  70. }
  71. else
  72. {
  73. dwRet = GetLastError();
  74. }
  75. }
  76. }
  77. }
  78. catch ( ... )
  79. {
  80. free(pBuff);
  81. }
  82. free(pBuff);
  83. }
  84. // NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE
  85. //
  86. // We are aborting out at this point, since RunDLL32 in its finite wisdom doesn't allow
  87. // for the setting of the dos error level (who designs this stuff?).
  88. if (!bRet)
  89. {
  90. ExitProcess(dwRet);
  91. }
  92. }
  93. }