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.

148 lines
3.3 KiB

  1. // Copyright (c) 2001 Microsoft Corporation
  2. //
  3. // Implementation of IConfigureYourServer::InstallService
  4. //
  5. // 31 Mar 2000 sburns
  6. // 05 Feb 2001 jeffjon Copied and modified for use with a Win32 version of CYS
  7. #include "pch.h"
  8. #include "resource.h"
  9. HRESULT
  10. CreateTempFile(const String& name, const String& contents)
  11. {
  12. LOG_FUNCTION2(createTempFile, name);
  13. ASSERT(!name.empty());
  14. ASSERT(!contents.empty());
  15. HRESULT hr = S_OK;
  16. HANDLE h = INVALID_HANDLE_VALUE;
  17. do
  18. {
  19. hr =
  20. FS::CreateFile(
  21. name,
  22. h,
  23. GENERIC_WRITE,
  24. 0,
  25. CREATE_ALWAYS,
  26. FILE_ATTRIBUTE_NORMAL);
  27. BREAK_ON_FAILED_HRESULT(hr);
  28. // NTRAID#NTBUG9-494875-2001/11/14-JeffJon
  29. // write to file with the Unicode BOM and end of file character.
  30. wchar_t unicodeBOM = (wchar_t)0xFEFF;
  31. hr = FS::Write(h, unicodeBOM + contents + L"\032");
  32. BREAK_ON_FAILED_HRESULT(hr);
  33. }
  34. while (0);
  35. Win::CloseHandle(h);
  36. return hr;
  37. }
  38. bool
  39. InstallServiceWithOcManager(
  40. const String& infText,
  41. const String& unattendText,
  42. const String& additionalArgs)
  43. {
  44. LOG_FUNCTION(InstallServiceWithOcManager);
  45. LOG(infText);
  46. LOG(unattendText);
  47. LOG(additionalArgs);
  48. ASSERT(!unattendText.empty());
  49. // infText may be empty
  50. bool result = false;
  51. HRESULT hr = S_OK;
  52. bool deleteInf = true;
  53. String sysFolder = Win::GetSystemDirectory();
  54. String infPath = sysFolder + L"\\cysinf.000";
  55. String unattendPath = sysFolder + L"\\cysunat.000";
  56. // create the inf and unattend files for the oc manager
  57. do
  58. {
  59. if (infText.empty())
  60. {
  61. // sysoc.inf is in %windir%\inf
  62. infPath = Win::GetSystemWindowsDirectory() + L"\\inf\\sysoc.inf";
  63. deleteInf = false;
  64. }
  65. else
  66. {
  67. hr = CreateTempFile(infPath, infText);
  68. BREAK_ON_FAILED_HRESULT(hr);
  69. }
  70. hr = CreateTempFile(unattendPath, unattendText);
  71. BREAK_ON_FAILED_HRESULT(hr);
  72. String fullPath =
  73. String::format(
  74. IDS_SYSOC_FULL_PATH,
  75. sysFolder.c_str());
  76. String commandLine =
  77. String::format(
  78. IDS_SYSOC_COMMAND_LINE,
  79. infPath.c_str(),
  80. unattendPath.c_str());
  81. if (!additionalArgs.empty())
  82. {
  83. commandLine += L" " + additionalArgs;
  84. }
  85. DWORD exitCode = 0;
  86. hr = ::CreateAndWaitForProcess(fullPath, commandLine, exitCode);
  87. BREAK_ON_FAILED_HRESULT(hr);
  88. // @@ might have to wait for the service to become installed as per
  89. // service manager
  90. if (exitCode == ERROR_SUCCESS)
  91. {
  92. result = true;
  93. break;
  94. }
  95. }
  96. while (0);
  97. // Ignore errors from these deletions. The worst case is we
  98. // leave these temp files on the machine. Since the user doesn't
  99. // know we are creating them we wouldn't know what to do with
  100. // the errors anyway.
  101. HRESULT deleteHr = S_OK;
  102. if (deleteInf)
  103. {
  104. deleteHr = FS::DeleteFile(infPath);
  105. ASSERT(SUCCEEDED(deleteHr));
  106. }
  107. deleteHr = FS::DeleteFile(unattendPath);
  108. ASSERT(SUCCEEDED(deleteHr));
  109. LOG_BOOL(result);
  110. LOG_HRESULT(hr);
  111. return result;
  112. }