cmWin32ProcessExecution.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #ifndef cmWin32ProcessExecution_h
  14. #define cmWin32ProcessExecution_h
  15. /*
  16. * Portable 'popen' replacement for Win32.
  17. *
  18. * Written by Bill Tutt <[email protected]>. Minor tweaks
  19. * and 2.0 integration by Fredrik Lundh <[email protected]>
  20. * Return code handling by David Bolen <[email protected]>.
  21. *
  22. * Modified for CMake.
  23. *
  24. * For more information, please check Microsoft Knowledge Base
  25. * Articles Q190351 and Q150956.
  26. */
  27. #include "cmStandardIncludes.h"
  28. #include "windows.h"
  29. class cmMakefile;
  30. /** \class cmWin32ProcessExecution
  31. * \brief A process executor for windows
  32. *
  33. * cmWin32ProcessExecution is a class that provides a "clean" way of
  34. * executing processes on Windows.
  35. */
  36. class cmWin32ProcessExecution
  37. {
  38. public:
  39. cmWin32ProcessExecution()
  40. {
  41. this->SetConsoleSpawn("w9xpopen.exe");
  42. this->Initialize();
  43. }
  44. /**
  45. * Initialize the process execution datastructure. Do not call while
  46. * running the process.
  47. */
  48. void Initialize()
  49. {
  50. this->m_ProcessHandle = 0;
  51. this->m_ExitValue = -1;
  52. // Comment this out. Maybe we will need it in the future.
  53. // file IO access to the process might be cool.
  54. //this->m_StdIn = 0;
  55. //this->m_StdOut = 0;
  56. //this->m_StdErr = 0;
  57. this->m_pStdIn = -1;
  58. this->m_pStdOut = -1;
  59. this->m_pStdErr = -1;
  60. }
  61. /**
  62. * Start the process in the directory path. Make sure that the
  63. * executable is either in the path or specify the full path. The
  64. * argument verbose specifies wether or not to display output while
  65. * it is being generated.
  66. */
  67. bool StartProcess(const char*, const char* path, bool verbose);
  68. /**
  69. * Wait for the process to finish. If timeout is specified, it will
  70. * break the process after timeout expires. (Timeout code is not yet
  71. * implemented.
  72. */
  73. bool Wait(int timeout);
  74. /**
  75. * Get the output of the process (mixed stdout and stderr) as
  76. * std::string.
  77. */
  78. const std::string GetOutput() const { return this->m_Output; }
  79. /**
  80. * Get the return value of the process. If the process is still
  81. * running, the return value is -1.
  82. */
  83. int GetExitValue() const { return this->m_ExitValue; }
  84. /**
  85. * On Windows 9x there is a bug in the process execution code which
  86. * may result in blocking. That is why this workaround is
  87. * used. Specify the console spawn, which should run the
  88. * Windows9xHack code.
  89. */
  90. void SetConsoleSpawn(const char* prog) { this->m_ConsoleSpawn = prog; }
  91. static int Windows9xHack(const char* command);
  92. private:
  93. bool PrivateOpen(const char*, const char*, int, int);
  94. bool PrivateClose(int timeout);
  95. HANDLE m_ProcessHandle;
  96. // Comment this out. Maybe we will need it in the future.
  97. // file IO access to the process might be cool.
  98. // FILE* m_StdIn;
  99. // FILE* m_StdOut;
  100. // FILE* m_StdErr;
  101. int m_pStdIn;
  102. int m_pStdOut;
  103. int m_pStdErr;
  104. int m_ExitValue;
  105. std::string m_Output;
  106. std::string m_ConsoleSpawn;
  107. bool m_Verbose;
  108. };
  109. #endif