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.

92 lines
2.9 KiB

  1. BOOL TimeStartService(WCHAR * wszServiceName, WCHAR *pwszRegServiceArgs, HANDLE *phProcess)
  2. {
  3. SC_HANDLE hSCManager;
  4. SC_HANDLE hService;
  5. WCHAR *pwszServiceArgs = NULL;
  6. ULONG cArgs = 0;
  7. WCHAR *apwszArgs[MAX_SERVICE_ARGS];
  8. *phProcess = NULL;
  9. // Get a handle to the Service Control Manager
  10. if (hSCManager = OpenSCManager(NULL, NULL, GENERIC_EXECUTE))
  11. {
  12. // Open a handle to the requested service
  13. if (hService = OpenService(hSCManager, wszServiceName, GENERIC_EXECUTE))
  14. {
  15. // Close the service manager's database
  16. CloseServiceHandle(hSCManager);
  17. // Formulate the arguments (if any)
  18. if (pwszRegServiceArgs)
  19. {
  20. UINT k = 0;
  21. // Make a copy of the service arguments
  22. pwszServiceArgs = (WCHAR *) PrivMemAlloc(
  23. (lstrlenW(pwszRegServiceArgs) + 1) * sizeof(WCHAR));
  24. if (pwszServiceArgs == NULL)
  25. {
  26. CloseServiceHandle(hService);
  27. return FALSE;
  28. }
  29. lstrcpyW(pwszServiceArgs, pwszRegServiceArgs);
  30. // Scan the arguments
  31. do
  32. {
  33. // Scan to the next non-whitespace character
  34. while(pwszServiceArgs[k] &&
  35. (pwszServiceArgs[k] == L' ' ||
  36. pwszServiceArgs[k] == L'\t'))
  37. {
  38. k++;
  39. }
  40. // Store the next argument
  41. if (pwszServiceArgs[k])
  42. {
  43. apwszArgs[cArgs++] = &pwszServiceArgs[k];
  44. }
  45. // Scan to the next whitespace char
  46. while(pwszServiceArgs[k] &&
  47. pwszServiceArgs[k] != L' ' &&
  48. pwszServiceArgs[k] != L'\t')
  49. {
  50. k++;
  51. }
  52. // Null terminate the previous argument
  53. if (pwszServiceArgs[k])
  54. {
  55. pwszServiceArgs[k++] = L'\0';
  56. }
  57. } while(pwszServiceArgs[k]);
  58. }
  59. // Start the service
  60. if (StartService(hService, cArgs,
  61. cArgs > 0 ? (LPCTSTR *) apwszArgs : NULL))
  62. {
  63. CloseServiceHandle(hService);
  64. PrivMemFree(pwszServiceArgs);
  65. return TRUE;
  66. }
  67. else
  68. {
  69. CloseServiceHandle(hService);
  70. PrivMemFree(pwszServiceArgs);
  71. }
  72. }
  73. else
  74. {
  75. CloseServiceHandle(hSCManager);
  76. }
  77. }
  78. DWORD err = GetLastError();
  79. return FALSE;
  80. }