juce_ChildProcess.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_CHILDPROCESS_H_INCLUDED
  22. #define JUCE_CHILDPROCESS_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. Launches and monitors a child process.
  26. This class lets you launch an executable, and read its output. You can also
  27. use it to check whether the child process has finished.
  28. */
  29. class JUCE_API ChildProcess
  30. {
  31. public:
  32. //==============================================================================
  33. /** Creates a process object.
  34. To actually launch the process, use start().
  35. */
  36. ChildProcess();
  37. /** Destructor.
  38. Note that deleting this object won't terminate the child process.
  39. */
  40. ~ChildProcess();
  41. /** These flags are used by the start() methods. */
  42. enum StreamFlags
  43. {
  44. wantStdOut = 1,
  45. wantStdErr = 2
  46. };
  47. /** Attempts to launch a child process command.
  48. The command should be the name of the executable file, followed by any arguments
  49. that are required.
  50. If the process has already been launched, this will launch it again. If a problem
  51. occurs, the method will return false.
  52. The streamFlags is a combinations of values to indicate which of the child's output
  53. streams should be read and returned by readProcessOutput().
  54. */
  55. bool start (const String& command, int streamFlags = wantStdOut | wantStdErr);
  56. /** Attempts to launch a child process command.
  57. The first argument should be the name of the executable file, followed by any other
  58. arguments that are needed.
  59. If the process has already been launched, this will launch it again. If a problem
  60. occurs, the method will return false.
  61. The streamFlags is a combinations of values to indicate which of the child's output
  62. streams should be read and returned by readProcessOutput().
  63. */
  64. bool start (const StringArray& arguments, int streamFlags = wantStdOut | wantStdErr);
  65. /** Returns true if the child process is alive. */
  66. bool isRunning() const;
  67. /** Attempts to read some output from the child process.
  68. This will attempt to read up to the given number of bytes of data from the
  69. process. It returns the number of bytes that were actually read.
  70. */
  71. int readProcessOutput (void* destBuffer, int numBytesToRead);
  72. /** Blocks until the process has finished, and then returns its complete output
  73. as a string.
  74. */
  75. String readAllProcessOutput();
  76. /** Blocks until the process is no longer running. */
  77. bool waitForProcessToFinish (int timeoutMs) const;
  78. /** If the process has finished, this returns its exit code. */
  79. uint32 getExitCode() const;
  80. /** Attempts to kill the child process.
  81. Returns true if it succeeded. Trying to read from the process after calling this may
  82. result in undefined behaviour.
  83. */
  84. bool kill();
  85. private:
  86. //==============================================================================
  87. class ActiveProcess;
  88. friend struct ContainerDeletePolicy<ActiveProcess>;
  89. ScopedPointer<ActiveProcess> activeProcess;
  90. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcess)
  91. };
  92. #endif // JUCE_CHILDPROCESS_H_INCLUDED