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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
| #include "stdafx.h"
HRESULT HResultFromError(DWORD err)
{
HRESULT hr = HRESULT_FROM_WIN32(err);
if(SUCCEEDED(hr))
hr = E_UNEXPECTED;
return hr;
}
HRESULT CreatePipeWithInheritableReadHandle(HANDLE *phReadInherit, HANDLE *phWrite)
{
HANDLE hRead= NULL;
BOOL bOK;
if(phReadInherit==NULL || phWrite==NULL)
return E_POINTER;
*phReadInherit = NULL;
*phWrite = NULL;
bOK = CreatePipe(&hRead, phWrite, NULL, 0);
if(!bOK)
return HResultFromError(GetLastError());
/*From now on, pipes are created*/
bOK = DuplicateHandle(GetCurrentProcess(), hRead, GetCurrentProcess(), phReadInherit,
0, TRUE, DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS
);
if(!bOK)
{
DWORD err = GetLastError();
/* hRead already closed even if DuplicateHandle() failed. */
CloseHandle(*phWrite), *phWrite=NULL;
*phReadInherit = NULL;
return HResultFromError(err);
}
return S_OK;
}
HRESULT CreatePipeWithInheritableWriteHandle(HANDLE *phRead, HANDLE *phWriteInherit)
{
HANDLE hWrite = NULL;
BOOL bOK;
if(phRead==NULL || phWriteInherit==NULL)
return E_POINTER;
*phRead = NULL;
*phWriteInherit = NULL;
bOK = CreatePipe(phRead, &hWrite, NULL, 0);
if(!bOK)
return HResultFromError(GetLastError());
/*From now on, pipes are created*/
bOK = DuplicateHandle(GetCurrentProcess(), hWrite, GetCurrentProcess(), phWriteInherit,
0, TRUE, DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS
);
if(!bOK)
{
DWORD err = GetLastError();
CloseHandle(*phRead), *phRead=NULL;
/* hWrite already closed even if DuplicateHandle() failed. */
*phWriteInherit = NULL;
return HResultFromError(err);
}
return S_OK;
}
void CloseHandles(HANDLE *pHandles, size_t handleArraySize)
{
size_t i;
for(i=0 ; i<handleArraySize ; i++)
{
/* Always call CloseHandle, even if handle is NULL or INVALID_HANDLE_VALUE.
Don't bother checking for a return value */
CloseHandle(pHandles[i]);
}
}
/* NOTE: if the function fails, THE HANDLE VALUES ARE UNDEFINED!
They are NOT guaranteed to be set to NULL nor INVALID_HANDLE_VALUE!! */
HRESULT CreateRedirectedChildProcess(LPCTSTR sczAppName, LPTSTR szCommandLine, DWORD flags, LPCTSTR sczDir, PROCESS_INFORMATION *pPi, HANDLE *phWriteStdin, HANDLE *phReadStdout, HANDLE *phReadStderr)
{
STARTUPINFO sInfo = {0};
HANDLE handlesToCloseOnError[8] = {0};
size_t iHandle = 0;
HRESULT hr;
BOOL bOK;
HANDLE hReadStdinInherit=NULL, hWriteStdoutInherit=NULL, hWriteStderrInherit=NULL;
if(sczAppName==NULL && szCommandLine==NULL)
return E_POINTER;
if(pPi==NULL)
return E_POINTER;
if(phWriteStdin==NULL || phReadStdout==NULL)
return E_POINTER;
/* stdin */
hr = CreatePipeWithInheritableReadHandle(&hReadStdinInherit, phWriteStdin);
if(FAILED(hr))
return hr;
handlesToCloseOnError[iHandle++] = hReadStdinInherit;
handlesToCloseOnError[iHandle++] = *phWriteStdin;
/* stdout */
hr = CreatePipeWithInheritableWriteHandle(phReadStdout, &hWriteStdoutInherit);
if(FAILED(hr))
{
CloseHandles(handlesToCloseOnError, ARRAYSIZE(handlesToCloseOnError));
return hr;
}
handlesToCloseOnError[iHandle++] = hWriteStdoutInherit;
handlesToCloseOnError[iHandle++] = *phReadStdout;
/* stderr */
if(phReadStderr != NULL)
{
hr = CreatePipeWithInheritableWriteHandle(phReadStderr, &hWriteStderrInherit);
if(FAILED(hr))
{
CloseHandles(handlesToCloseOnError, ARRAYSIZE(handlesToCloseOnError));
return hr;
}
handlesToCloseOnError[iHandle++] = hWriteStderrInherit;
handlesToCloseOnError[iHandle++] = *phReadStderr;
}
else
{
hWriteStderrInherit = hWriteStdoutInherit;
}
sInfo.cb = sizeof sInfo;
sInfo.dwFlags = STARTF_USESTDHANDLES;
sInfo.hStdInput = hReadStdinInherit;
sInfo.hStdOutput = hWriteStdoutInherit;
sInfo.hStdError = hWriteStderrInherit;
bOK = CreateProcess(sczAppName, szCommandLine, NULL, NULL, TRUE, flags, NULL, sczDir, &sInfo, pPi);
if(!bOK)
{
DWORD err = GetLastError();
CloseHandles(handlesToCloseOnError, ARRAYSIZE(handlesToCloseOnError));
return HResultFromError(err);
}
else
{
CloseHandle(hReadStdinInherit);
CloseHandle(hWriteStdoutInherit);
CloseHandle(hWriteStderrInherit);
return S_OK;
}
}
void SetCurrentDirectoryToExeDirectory(void)
{
TCHAR fileName[MAX_PATH] = _T("");
TCHAR *pBackslash = NULL;
GetModuleFileName(NULL, fileName, MAX_PATH);
pBackslash = _tcsrchr(fileName, _T('\\'));
if(pBackslash)
{
*pBackslash = _T('\0');
SetCurrentDirectory(fileName);
}
}
void DisplayError(LPCTSTR parent, DWORD err)
{
LPTSTR msg = NULL;
int resMsg = FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, 0,
(LPTSTR)&msg, /* special case for FORMAT_MESSAGE_ALLOCATE_BUFFER */
0, NULL
);
if(resMsg > 0)
{
_ftprintf(stderr, _T("Error in %s: %s"), parent, msg);
LocalFree(msg);
}
}
void TestChildProcess(void)
{
HANDLE hWriteStdin;
HANDLE hReadStdout;
HANDLE hReadStderr;
PROCESS_INFORMATION pi = {0};
HRESULT hr;
SetCurrentDirectoryToExeDirectory();
hr = CreateRedirectedChildProcess(_T("rot13.exe"), NULL, 0, NULL, &pi, &hWriteStdin, &hReadStdout, &hReadStderr);
if(SUCCEEDED(hr))
{
DWORD nWritten;
char const * str = "Super Test de Medinoc\r\n";
BOOL bOK = WriteFile(hWriteStdin, str, (DWORD)strlen(str), &nWritten, NULL);
CloseHandle(hWriteStdin);
if(bOK)
{
char buf[40];
DWORD nRead;
do {
bOK = ReadFile(hReadStdout, buf, nWritten, &nRead, NULL);
} while(bOK && nRead==0);
if(bOK)
{
buf[nRead] = '\0';
printf("Reçu :\n");
printf("\"%s\"\n", buf);
printf("(%lu caractères)\n", nRead);
}
else
{
DisplayError(_T("ReadFile()"), GetLastError());
}
}
else
DisplayError(_T("WriteFile()"), GetLastError());
/* clean up */
CloseHandle(hReadStdout);
CloseHandle(hReadStderr);
WaitForSingleObject(pi.hProcess, 5000);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
else
{
if(HRESULT_FACILITY(hr)==FACILITY_WIN32)
{
DisplayError(_T("CreateRedirectedChildProcess()"), HRESULT_CODE(hr));
}
else
{
fprintf(stderr, "CreateRedirectedChildProcess() failed with HRESULT 0x%08lX.\n", hr);
}
}
} |
Partager