File: Process.cpp

package info (click to toggle)
quickfix 1.13.3%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 152,548 kB
  • ctags: 679,426
  • sloc: cpp: 639,331; xml: 129,200; python: 108,722; ruby: 85,152; sh: 10,492; ansic: 9,025; java: 1,827; cs: 1,145; makefile: 523; sql: 313; perl: 108
file content (138 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <Windows.h>
#include <process.h>

#include "Process.h"

void* readOutput( void* p )
{
  HANDLE hReadPipe = p;
  HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
  char chBuf[128];
  DWORD dwRead, dwWritten;

  while(true)
  { 
    if( !ReadFile( hReadPipe, chBuf, 128, &dwRead, 
      NULL) || dwRead == 0) break; 
    if (! WriteFile(hStdOut, chBuf, dwRead, &dwWritten, NULL)) 
      break; 
  }
  return 0;
}

namespace ATRUN
{
  void Process::create()
  {
    SECURITY_ATTRIBUTES sa; 
    sa.nLength = sizeof(SECURITY_ATTRIBUTES); 
    sa.bInheritHandle = TRUE; 
    sa.lpSecurityDescriptor = NULL; 


    STARTUPINFO info;
    GetStartupInfo(&info);

    char curdir[100];
    GetCurrentDirectory(sizeof(curdir), curdir);

    SetCurrentDirectory( const_cast<char*>(m_workingDir.c_str()) );

    HANDLE hReadPipe, hWritePipe;
    if(m_sendToFile)
    {
      openOutFile();
      info.dwFlags = STARTF_USESTDHANDLES;
      CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);
      info.hStdOutput = m_outFileHandle;
      info.hStdError = m_outFileHandle;
    }

    else if(m_sendToStd)
    {
      info.dwFlags = STARTF_USESTDHANDLES;
      CreatePipe(&hReadPipe, &hWritePipe, &sa, 0);
      info.hStdOutput = hWritePipe;
      info.hStdError = hWritePipe;
    }

    m_created = CreateProcess(
      NULL,
      const_cast<char*>(m_commandLine.c_str()),
      NULL,
      NULL,
      TRUE,
      getFlags(),
      NULL,
      NULL,
      &info,
      &m_procinfo ) != 0;

    if(m_created && (m_sendToFile || m_sendToStd))
      _beginthread((void(__cdecl*)(void*))readOutput, 0, hReadPipe);

    SetCurrentDirectory( curdir );
  }

  void Process::terminate()
  {
    if(m_created)
    {
      TerminateProcess(m_procinfo.hProcess, 0);
      reset();
    }
  }

  void Process::waitFor()
  {
    WaitForSingleObject(m_procinfo.hProcess, INFINITE);
    reset();
  }

  void Process::reset()
  {
    GetExitCodeProcess(m_procinfo.hProcess, &m_exitCode);
    CloseHandle(m_procinfo.hProcess);
    m_created = false;
    m_procinfo.dwThreadId = -1;

    if( m_isOutFileOpen )
    {
      closeOutFile();
    }
  }

  void Process::openOutFile()
  {
      SECURITY_ATTRIBUTES sa; 

      sa.nLength = sizeof(SECURITY_ATTRIBUTES); 
      sa.bInheritHandle = TRUE; 
      sa.lpSecurityDescriptor = NULL; 

      m_outFileHandle = CreateFile(
        m_outFile.c_str(),
        GENERIC_WRITE,
        0, //open exclusively
        &sa,
        CREATE_ALWAYS,
        FILE_ATTRIBUTE_NORMAL,
        NULL );

      m_isOutFileOpen = true;
  }

  void Process::closeOutFile()
  {
    CloseHandle(m_outFileHandle);
    m_isOutFileOpen = false;
  }

  DWORD Process::getFlags()
  {
    if(m_showWindow)
      return CREATE_NEW_CONSOLE;

    return CREATE_NO_WINDOW;
  }
}