cmWin32ProcessExecution.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /** Code from a Borland web site with the following explaination :
  93. * In this article, I will explain how to spawn a console
  94. * application and redirect its standard input/output using
  95. * anonymous pipes. An anonymous pipe is a pipe that goes only in
  96. * one direction (read pipe, write pipe, etc.). Maybe you are
  97. * asking, "why would I ever need to do this sort of thing?" One
  98. * example would be a Windows telnet server, where you spawn a shell
  99. * and listen on a port and send and receive data between the shell
  100. * and the socket client. (Windows does not really have a built-in
  101. * remote shell). First, we should talk about pipes. A pipe in
  102. * Windows is simply a method of communication, often between
  103. * process. The SDK defines a pipe as "a communication conduit with
  104. * two ends; a process with a handle to one end can communicate with
  105. * a process having a handle to the other end." In our case, we are
  106. * using "anonymous" pipes, one-way pipes that "transfer data
  107. * between a parent process and a child process or between two child
  108. * processes of the same parent process." It's easiest to imagine a
  109. * pipe as its namesake. An actual pipe running between processes
  110. * that can carry data. We are using anonymous pipes because the
  111. * console app we are spawning is a child process. We use the
  112. * CreatePipe function which will create an anonymous pipe and
  113. * return a read handle and a write handle. We will create two
  114. * pipes, on for stdin and one for stdout. We will then monitor the
  115. * read end of the stdout pipe to check for display on our child
  116. * process. Every time there is something availabe for reading, we
  117. * will display it in our app. Consequently, we check for input in
  118. * our app and send it off to the write end of the stdin pipe.
  119. */
  120. static bool BorlandRunCommand(const char* command, const char* dir,
  121. std::string& output, int& retVal, bool verbose,
  122. int timeout);
  123. private:
  124. bool PrivateOpen(const char*, const char*, int, int);
  125. bool PrivateClose(int timeout);
  126. HANDLE m_ProcessHandle;
  127. // Comment this out. Maybe we will need it in the future.
  128. // file IO access to the process might be cool.
  129. // FILE* m_StdIn;
  130. // FILE* m_StdOut;
  131. // FILE* m_StdErr;
  132. int m_pStdIn;
  133. int m_pStdOut;
  134. int m_pStdErr;
  135. int m_ExitValue;
  136. std::string m_Output;
  137. std::string m_ConsoleSpawn;
  138. bool m_Verbose;
  139. };
  140. #endif