"Fossies" - the Fresh Open Source Software Archive

Member "tvnserver-2.0.4/win-system/Process.cpp" (3 Aug 2011, 3771 Bytes) of archive /windows/misc/tvnserver-2.0.4-src.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Process.cpp" see the Fossies "Dox" file reference documentation.

    1 // Copyright (C) 2008, 2009, 2010 GlavSoft LLC.
    2 // All rights reserved.
    3 //
    4 //-------------------------------------------------------------------------
    5 // This file is part of the TightVNC software.  Please visit our Web site:
    6 //
    7 //                       http://www.tightvnc.com/
    8 //
    9 // This program is free software; you can redistribute it and/or modify
   10 // it under the terms of the GNU General Public License as published by
   11 // the Free Software Foundation; either version 2 of the License, or
   12 // (at your option) any later version.
   13 //
   14 // This program is distributed in the hope that it will be useful,
   15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
   16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   17 // GNU General Public License for more details.
   18 //
   19 // You should have received a copy of the GNU General Public License along
   20 // with this program; if not, write to the Free Software Foundation, Inc.,
   21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   22 //-------------------------------------------------------------------------
   23 //
   24 
   25 #include "Process.h"
   26 #include "SystemException.h"
   27 #include "util/Log.h"
   28 
   29 Process::Process(const TCHAR *path, const TCHAR *args)
   30 : m_hProcess(0),
   31   m_hThread(0),
   32   m_handlesIsInherited(false),
   33   m_hStopWait(0),
   34   m_stdIn(0),
   35   m_stdOut(0),
   36   m_stdErr(0)
   37 {
   38   setFilename(path);
   39   setArguments(args);
   40 
   41   m_hStopWait = CreateEvent(0, FALSE, FALSE, 0);
   42 }
   43 
   44 Process::~Process()
   45 {
   46   cleanup();
   47 
   48   if (m_hStopWait) {
   49     CloseHandle(m_hStopWait);
   50   }
   51 }
   52 
   53 void Process::setFilename(const TCHAR *path)
   54 {
   55   m_path.setString(path);
   56 }
   57 
   58 void Process::setArguments(const TCHAR *args)
   59 {
   60   m_args.setString(args);
   61 }
   62 
   63 void Process::setStandardIoHandles(HANDLE stdIn, HANDLE stdOut, HANDLE stdErr)
   64 {
   65   m_stdIn = stdIn;
   66   m_stdOut = stdOut;
   67   m_stdErr = stdErr;
   68 }
   69 
   70 void Process::setHandleInheritances(bool handlesIsInerited)
   71 {
   72   m_handlesIsInherited = handlesIsInerited;
   73 }
   74 
   75 void Process::getStartupInfo(STARTUPINFO *sti)
   76 {
   77   ZeroMemory(sti, sizeof(STARTUPINFO));
   78   sti->cb = sizeof(STARTUPINFO);
   79   sti->hStdError = m_stdErr;
   80   sti->hStdInput = m_stdIn;
   81   sti->hStdOutput = m_stdOut;
   82   if (sti->hStdError != 0 || sti->hStdInput != 0 || sti->hStdOutput != 0) {
   83     sti->dwFlags |= STARTF_USESTDHANDLES;
   84   }
   85 }
   86 
   87 void Process::start()
   88 {
   89   cleanup();
   90 
   91   STARTUPINFO sti;
   92   getStartupInfo(&sti);
   93   PROCESS_INFORMATION pi;
   94 
   95   StringStorage commandLine = getCommandLineString();
   96 
   97   if (CreateProcess(NULL, (LPTSTR) commandLine.getString(),
   98                     NULL, NULL, m_handlesIsInherited, NULL, NULL, NULL,
   99                     &sti, &pi) == 0) {
  100     throw SystemException();
  101   }
  102 
  103   m_hThread = pi.hThread;
  104   m_hProcess = pi.hProcess;
  105 }
  106 
  107 void Process::kill()
  108 {
  109   if (TerminateProcess(m_hProcess, 0) == 0) {
  110     throw SystemException();
  111   }
  112 }
  113 
  114 void Process::waitForExit()
  115 {
  116   HANDLE handleArray[2] = { m_hProcess, m_hStopWait };
  117 
  118   WaitForMultipleObjects(2, handleArray, FALSE, INFINITE);
  119 }
  120 
  121 void Process::stopWait()
  122 {
  123   SetEvent(m_hStopWait);
  124 }
  125 
  126 DWORD Process::getExitCode()
  127 {
  128   DWORD exitCode;
  129 
  130   if (GetExitCodeProcess(m_hProcess, &exitCode) == 0) {
  131     throw SystemException();
  132   }
  133 
  134   return exitCode;
  135 }
  136 
  137 HANDLE Process::getProcessHandle()
  138 {
  139   return m_hProcess;
  140 }
  141 
  142 StringStorage Process::getCommandLineString()
  143 {
  144   StringStorage result;
  145 
  146   if (m_args.isEmpty()) {
  147     return m_path;
  148   }
  149 
  150   result.format(_T("%s %s"), m_path.getString(), m_args.getString());
  151 
  152   return result;
  153 }
  154 
  155 void Process::cleanup()
  156 {
  157   if (m_hProcess) {
  158     CloseHandle(m_hProcess);
  159     m_hProcess = 0;
  160   }
  161   if (m_hThread) {
  162     CloseHandle(m_hThread);
  163     m_hThread = 0;
  164   }
  165 }