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.

87 lines
2.3 KiB

  1. /*****************************************************************************
  2. *
  3. * pipe.cpp
  4. *
  5. * Run a command, reading its output.
  6. *
  7. *****************************************************************************/
  8. #include "sdview.h"
  9. void ChildProcess::Start(LPCTSTR pszCommand)
  10. {
  11. SECURITY_ATTRIBUTES sa;
  12. sa.nLength = sizeof(sa);
  13. sa.lpSecurityDescriptor = NULL;
  14. sa.bInheritHandle = TRUE;
  15. HANDLE hWrite;
  16. BOOL fSuccess = CreatePipe(&_hRead, &hWrite, &sa, 0);
  17. if (fSuccess) {
  18. STARTUPINFO si = { 0 };
  19. PROCESS_INFORMATION pi;
  20. si.cb = sizeof(si);
  21. si.dwFlags = STARTF_USESTDHANDLES;
  22. si.hStdInput = GetStdHandle(STD_INPUT_HANDLE);
  23. si.hStdOutput = hWrite;
  24. // Dup stdout to stderr in case client closes one of them.
  25. if (DuplicateHandle(GetCurrentProcess(), hWrite,
  26. GetCurrentProcess(), &si.hStdError, 0,
  27. TRUE, DUPLICATE_SAME_ACCESS)) {
  28. TCHAR szCommand[MAX_PATH];
  29. lstrcpyn(szCommand, pszCommand, ARRAYSIZE(szCommand));
  30. fSuccess = CreateProcess(NULL, szCommand, NULL, NULL, TRUE,
  31. CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW | DETACHED_PROCESS,
  32. NULL, NULL, &si, &pi);
  33. if (fSuccess) {
  34. CloseHandle(pi.hThread);
  35. _hProcess = pi.hProcess;
  36. _dwPid = pi.dwProcessId;
  37. }
  38. CloseHandle(si.hStdError);
  39. }
  40. CloseHandle(hWrite);
  41. }
  42. }
  43. void ChildProcess::Stop()
  44. {
  45. if (_hProcess) {
  46. CloseHandle(_hProcess);
  47. _hProcess = NULL;
  48. }
  49. if (_hRead) {
  50. CloseHandle(_hRead);
  51. _hRead = NULL;
  52. }
  53. _dwPid = 0;
  54. }
  55. void ChildProcess::Kill()
  56. {
  57. if (_dwPid) {
  58. GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, _dwPid);
  59. }
  60. }
  61. SDChildProcess::SDChildProcess(LPCTSTR pszCommand)
  62. {
  63. String str;
  64. str << QuoteSpaces(GlobalSettings.GetSdPath()) << TEXT(" ") <<
  65. GlobalSettings.GetSdOpts() << TEXT(" ");
  66. if (!GlobalSettings.GetFakeDir().IsEmpty()) {
  67. str << TEXT("-d ") << QuoteSpaces(GlobalSettings.GetFakeDir()) << TEXT(" ");
  68. }
  69. str << pszCommand;
  70. Start(str);
  71. }