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
|
for (int i = 0; i < nbFile; i++)
{
strExec = "\"C:\\test.exe\"";
lstrcpy(lpFileName, "C:\\test.txt");
//récupération du fichier d'entrée pour le passer au process
hFichierIn = CreateFile(lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if( hFichierIn == INVALID_HANDLE_VALUE)
return false;
//remplissage du startupinfo
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW |STARTF_USESTDHANDLES;
si.wShowWindow = SW_SHOW;
si.hStdInput = hFichierIn;
//création du process
if ( ! CreateProcess(NULL, strExec.GetBuffer(1000), NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &(pi[i])) )
{
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;
}
//si tout est ok on récupère le HANDLE
hProcessInfo[i] = pi[i].hProcess;
}
WaitForMultipleObjects(nbFile, hProcessInfo, true, 20000);
/* vérification que les process sont terminés,
fermeture des Handles, sauvegarde des données des fichiers traités
et suppression de ces derniers*/
for (int j = 0; j < nbFile; j ++)
{
GetExitCodeProcess(hProcessInfo[j], &dwExitCode);
if(dwExitCode == STILL_ACTIVE) //process toujours présent ?
{
// toujours la.
TerminateProcess(hProcessInfo[j], -1);
}
CloseHandle(hProcessInfo[j]);
lstrcpy(lpFileName, "C:\\test.txt");
if (FileIn.Open(lpFileName, CFile::modeRead, NULL))
{
char pbuf[500];
lstrcpy(lpFileName, sCheminL);
if (FileOut.Open(lpFileName, CFile::modeNoTruncate|CFile::modeWrite , NULL))
{
while (FileIn.Read( pbuf, 500 ) > 0)
{
FileOut.Write( pbuf, 500 );
strcpy(pbuf, " ");
}
FileOut.Close();
}
FileIn.Close();
}
//CFile::Remove("C:\\test.txt");
} |
Partager