CProcess.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "stdafx.h"
  2. #include "CProcess.h"
  3. CProcess::CProcess()
  4. {
  5. m_dwProcessId = 0;
  6. m_execType = SYNC;
  7. // 初始化管道句柄为空句柄
  8. for (int i = 0; i < _countof(m_hPipes); ++i)
  9. for (int j = 0; j < _countof(m_hPipes[i]); ++j)
  10. m_hPipes[i][j] = INVALID_HANDLE_VALUE;
  11. }
  12. CProcess::CProcess(ExecType execType) :CProcess()
  13. {
  14. if (!Create(execType)) ClosePipes();
  15. }
  16. CProcess::~CProcess()
  17. {
  18. ClosePipes();
  19. }
  20. BOOL CProcess::Create(ExecType execType)
  21. {
  22. BOOL bRet = TRUE;
  23. bRet |= CreatePipes();
  24. m_execType = execType;
  25. return bRet;
  26. }
  27. BOOL CProcess::Execute(LPCTSTR Command)
  28. {
  29. BOOL bRet = TRUE;
  30. STARTUPINFO si;
  31. ZeroMemory(&si, sizeof(si));
  32. si.cb = sizeof(si);
  33. si.hStdInput = m_hPipes[pIn][Read];
  34. si.hStdOutput = m_hPipes[pOut][Write];
  35. si.hStdError = m_hPipes[pErr][Write];
  36. si.wShowWindow = SW_HIDE;
  37. si.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
  38. PROCESS_INFORMATION pi;
  39. TCHAR cmd[MAX_PATH];
  40. _tcscpy_s(cmd, _countof(cmd), Command);
  41. bRet |= ::CreateProcess(NULL, cmd, NULL, NULL, TRUE, NULL, NULL, NULL, &si, &pi);
  42. if (!bRet) {
  43. ClosePipes();
  44. return FALSE;
  45. }
  46. m_dwProcessId = pi.dwProcessId;
  47. // 关闭不要的读写句柄
  48. ::CloseHandle(m_hPipes[pIn][Read]); m_hPipes[pIn][Read] = INVALID_HANDLE_VALUE;
  49. ::CloseHandle(m_hPipes[pOut][Write]); m_hPipes[pOut][Write] = INVALID_HANDLE_VALUE;
  50. ::CloseHandle(m_hPipes[pErr][Write]); m_hPipes[pErr][Write] = INVALID_HANDLE_VALUE;
  51. if (m_execType == SYNC) WaitForSingleObject(pi.hProcess, INFINITE);
  52. ::CloseHandle(pi.hProcess);
  53. ::CloseHandle(pi.hThread);
  54. return bRet;
  55. }
  56. size_t CProcess::ReadLine(LPVOID lpBuffer, size_t len, char seq)
  57. {
  58. if (!IsOk()) return 0;
  59. DWORD totalRead = 0;
  60. DWORD bytesRead;
  61. char* buffer = new char[len + 1];
  62. ZeroMemory(buffer, len + 1);
  63. char ch;
  64. do {
  65. if (!::ReadFile(m_hPipes[pOut][Read], &ch, 1, &bytesRead, NULL)) break;
  66. buffer[totalRead] = ch;
  67. ++totalRead;
  68. } while (bytesRead == 1 && ch != seq && totalRead < len + 1);
  69. if (totalRead > 0) strcpy_s((char*)lpBuffer, len, buffer);
  70. delete[] buffer;
  71. return totalRead;
  72. }
  73. size_t CProcess::WriteSome(LPVOID lpBuffer, size_t len)
  74. {
  75. if (!IsOk()) return 0;
  76. DWORD totalWritten = 0;
  77. while (len > 0)
  78. {
  79. DWORD chunkWritten;
  80. if (!::WriteFile(m_hPipes[pIn][Write], lpBuffer, len, &chunkWritten, NULL)) break;
  81. if (!chunkWritten) break;
  82. lpBuffer = (char*)lpBuffer + chunkWritten;
  83. totalWritten += chunkWritten;
  84. len -= chunkWritten;
  85. }
  86. return totalWritten;
  87. }
  88. BOOL CProcess::CreatePipes()
  89. {
  90. BOOL bRet = TRUE;
  91. for (int i = 0; i < pOut + 1; ++i)
  92. {
  93. SECURITY_ATTRIBUTES security;
  94. security.nLength = sizeof(security);
  95. security.lpSecurityDescriptor = NULL;
  96. security.bInheritHandle = TRUE;
  97. bRet |= ::CreatePipe(&m_hPipes[i][Read], &m_hPipes[i][Write], &security, 0);
  98. }
  99. bRet |= ::SetHandleInformation(m_hPipes[pIn][Write], HANDLE_FLAG_INHERIT, 0);
  100. bRet |= ::SetHandleInformation(m_hPipes[pOut][Read], HANDLE_FLAG_INHERIT, 0);
  101. bRet |= ::SetHandleInformation(m_hPipes[pErr][Read], HANDLE_FLAG_INHERIT, 0);
  102. return bRet;
  103. }
  104. void CProcess::ClosePipes()
  105. {
  106. for (int i = 0; i < _countof(m_hPipes); ++i) {
  107. for (int j = 0; j < _countof(m_hPipes[i]); ++j) {
  108. if (m_hPipes[i][j] != INVALID_HANDLE_VALUE) {
  109. ::CloseHandle(m_hPipes[i][j]);
  110. m_hPipes[i][j] = INVALID_HANDLE_VALUE;
  111. }
  112. }
  113. }
  114. }