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
|
bool interfaceClassWithFile::executeCmdWithRedirection(CString nomFichierEntree)
{
SECURITY_DESCRIPTOR sd;
SECURITY_ATTRIBUTES sa;
HANDLE hReadPipe,
hWritePipe;
STARTUPINFO si;
PROCESS_INFORMATION pi;
LPVOID lpMsgBuf;
DWORD BytesRead,
lu;
char dest[BUFFER],
lpFileName[100],
dateStr [9];
CString strExec;
_strdate( dateStr);
sDate = dateStr;
sDate.Replace("/", "_");
sCheminL += "\\ImportHPRIM_" + sDate + ".log";
strExec = "\"" + sCheminE + "\"<" + sCheminF;
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd, true, NULL, false);
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = true;
sa.lpSecurityDescriptor = &sd;
if (! CreatePipe(&hReadPipe, &hWritePipe, &sa, NULL))
return false;
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW |STARTF_USESTDHANDLES;
si.wShowWindow = SW_SHOW;
si.hStdOutput = hWritePipe;
si.hStdError = hWritePipe;
if ( ! CreateProcess(NULL, strExec.GetBuffer(1000), NULL, NULL, FALSE, 0, 0, 0, &si, &pi) )
{
cout << strExec.GetBuffer(1000) << endl;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &lpMsgBuf,
0, NULL );
printf( "CreateProcess failed (%s).\n", lpMsgBuf);
LocalFree(lpMsgBuf);
return false;
}
WaitForSingleObject(pi.hProcess, INFINITE);
lstrcpy(lpFileName, sCheminL);
hFichier = CreateFile(lpFileName, GENERIC_WRITE, NULL, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFichier == INVALID_HANDLE_VALUE)
return false;
memset(dest, 0, BUFFER);
while (ReadFile(hReadPipe, dest, BUFFER, &BytesRead, NULL))
{
if (!BytesRead)
break;
WriteFile(hFichier, dest, BytesRead, &lu, NULL);
if (BytesRead<BUFFER)
break;
}
CloseHandle(hFichier);
CloseHandle(hReadPipe);
CloseHandle(hWritePipe);
return true;
} |
Partager