Click here to Skip to main content
16,005,149 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: MSVCRP90D.dll Problem. Pin
Naveen9-Jun-08 19:38
Naveen9-Jun-08 19:38 
GeneralRe: MSVCRP90D.dll Problem. Pin
T.RATHA KRISHNAN9-Jun-08 19:49
T.RATHA KRISHNAN9-Jun-08 19:49 
GeneralRe: MSVCRP90D.dll Problem. Pin
Naveen9-Jun-08 20:31
Naveen9-Jun-08 20:31 
QuestionBMP file to be written to an AVI file Pin
vidya1109-Jun-08 17:30
vidya1109-Jun-08 17:30 
AnswerRe: BMP file to be written to an AVI file Pin
Naveen9-Jun-08 17:48
Naveen9-Jun-08 17:48 
AnswerRe: BMP file to be written to an AVI file Pin
Hamid_RT9-Jun-08 20:16
Hamid_RT9-Jun-08 20:16 
AnswerRe: BMP file to be written to an AVI file Pin
achainard10-Jun-08 2:36
achainard10-Jun-08 2:36 
QuestionWaitForMultipleObjects not waiting Pin
wjeanveau9-Jun-08 11:39
wjeanveau9-Jun-08 11:39 
I am making a program that will take in a txt with 3 peices of info per line, group, program, parameters. The program needs to make a CreateProcess for each program in the first group, then wait until all programs have completed, then start on the next group. In the end, it prints out the results. The WaitForMultipleObjecst is not working and when I "GetLastError()" it comes back error:6 which I believe means an invalid handle. I have other problems with the code I know (timers always come back zero), but that's okay for now.

The Code:

#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip>


#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <tchar.h>

using namespace std;


class ProgramDetails
{
public:
int m_group;
string m_programToLaunch;
string m_programParams;
DWORD m_exitCode;
SYSTEMTIME m_kTime;
SYSTEMTIME m_uTime;
ProgramDetails(string group, string programToLaunch, string programParams);
ProgramDetails(int gp, string pL, string pP, DWORD eC, SYSTEMTIME kT, SYSTEMTIME uT);
ProgramDetails();
void Display () const;
};
//constructors

ProgramDetails::ProgramDetails(){};
ProgramDetails::ProgramDetails(string gp, string pL, string pP)
{
istringstream iss(gp);
iss >> m_group; // this should be tested
m_programToLaunch = pL;
m_programParams = pP;

}
ProgramDetails::ProgramDetails(int gp, string pL, string pP, DWORD eC, SYSTEMTIME kT, SYSTEMTIME uT)
{
m_group = gp;
m_programToLaunch = pL;
m_programParams = pP;
m_exitCode = eC;
m_kTime = kT;
m_uTime = uT;
}

// setting up one line of output
void ProgramDetails::Display() const
{
cout << m_group << "\t" << m_kTime.wHour << ":" << m_kTime.wMinute << ":" << m_kTime.wSecond << ":"<< m_kTime.wMilliseconds;
cout << setw(3) << "\t" << m_uTime.wHour << ":" << m_uTime.wMinute << ":" << m_uTime.wSecond << ":" << m_uTime.wMilliseconds;
cout << setw(4)<< "\t" << m_exitCode << "\t\t" << m_programToLaunch << "\t" << m_programParams<< endl;
}
// printing out the vector
void DisplayVector(const vector<ProgramDetails>& finishedList)
{
cout << "Group\t" << "KernelTime\t" << "UserTime\t" << "ExitCode\t" << "Program\t\t" << "Parameters" << endl;
cout << "-----\t" << "----------\t" << "--------\t" << "--------\t" << "-------\t\t" << "----------" << endl;
for(unsigned int i = 0; i < finishedList.size(); ++i)
{
finishedList[i].Display();
}
}


int main(int argc, char* argv[] )
{
//const char* str = argv[1]; // remember to uncomment this line and the ifstrea fileStream(str) line
std::vector<ProgramDetails> progList;
std::vector<ProgramDetails> finishedList;
string line;
ifstream fileStream ("input3.txt");// remember to comment out this line
//ifstream fileStream(str);

while(getline(fileStream, line))
{
istringstream iss(line);

string group;
string programToLaunch;
string programParameters;

getline(iss, group, ',');
getline(iss, programToLaunch, ',');
getline(iss, programParameters);

ProgramDetails details(group, programToLaunch, programParameters);
progList.push_back(details);

}
//cout << progList.size() << endl;
STARTUPINFO sinfo ={0};
sinfo.cb = sizeof(STARTUPINFO);
PROCESS_INFORMATION pi = {0};

//int n = progList.size();
HANDLE* myThreads = NULL;

//myThreads = new HANDLE[n];
//for(int k = 0; k < n; ++k)
//{
// myThreads[k] = 0;
//}

int currGroup = progList[0].m_group;
vector<HANDLE> vecHandles;

for(unsigned int i = 0; i < progList.size();++i)
{
if(progList[i].m_group == currGroup)
{
string str1 = progList[i].m_programToLaunch;
string str2 = progList[i].m_programParams;
//cout << str1 << "," << str2 << endl;
string command = str1 + " " + str2;
unsigned long const CP_MAX_COMMANDLINE = 32768;
try
{
char* commandLine = new char[CP_MAX_COMMANDLINE];
strncpy_s(commandLine, CP_MAX_COMMANDLINE, command.c_str(), command.size());

CreateProcessA(NULL, commandLine, NULL, NULL, false, 0, NULL, NULL, &sinfo, &pi);

}
catch(std::bad_alloc& )
{
wcerr << L"Insufficient Memeory to launch application" << endl;
return 1;
}
DWORD exitCode = 0;
GetExitCodeProcess(pi.hProcess, &exitCode);

FILETIME creationTime, exitTime, kernelTime, userTime;
int Timer = GetProcessTimes(pi.hProcess, &creationTime, &exitTime, &kernelTime, &userTime);
//cout << "Timer Says: " << Timer;
SYSTEMTIME cTime, eTime, kTime, uTime;
::FileTimeToSystemTime(&creationTime, &cTime);
::FileTimeToSystemTime(&exitTime, &eTime);
::FileTimeToSystemTime(&kernelTime, &kTime);
::FileTimeToSystemTime(&userTime, &uTime);
vecHandles.push_back(pi.hProcess);
ProgramDetails finDetails(currGroup, str1, str2, exitCode, kTime, uTime);
finishedList.push_back(finDetails);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
}
else
{
myThreads = new HANDLE[i];
for(unsigned int k = 0; k < i; ++k)
{
myThreads[k] = vecHandles[k];
}
DWORD dwResults = WaitForMultipleObjects(i, myThreads, TRUE ,INFINITE);
switch(dwResults) {
case WAIT_OBJECT_0:
cout << "Got the first one" << endl;
break;
case WAIT_OBJECT_0 + 1:
cout << "Got the second one to work" << endl;
break;
case WAIT_OBJECT_0 + 2:
cout << "Got WHEEEE!!! third one to work" << endl;
break;
default:
cout << "Error: " << GetLastError();
break;
}
Sleep(2000);
currGroup = progList[i].m_group;
--i;
}
if(i == progList.size()-1)
{
myThreads = new HANDLE[i];
for(unsigned int k = 0; k < i; ++k)
{
myThreads[k] = vecHandles[k];
}
DWORD dwResults = WaitForMultipleObjects(i, myThreads, TRUE ,INFINITE);
switch(dwResults) {
case WAIT_OBJECT_0:
cout << "Got the first one" << endl;
break;
case WAIT_OBJECT_0 + 1:
cout << "Got the second one to work" << endl;
break;
case WAIT_OBJECT_0 + 2:
cout << "Got WHEEEE!!! third one to work" << endl;
break;
default:
cout << "Error: " << GetLastError();
break;
}
}

}

delete [] myThreads;
myThreads = NULL;
DisplayVector(finishedList);
return 0;
}
AnswerRe: WaitForMultipleObjects not waiting Pin
Joe Woodbury9-Jun-08 12:29
professionalJoe Woodbury9-Jun-08 12:29 
GeneralRe: WaitForMultipleObjects not waiting Pin
wjeanveau9-Jun-08 12:43
wjeanveau9-Jun-08 12:43 
GeneralRe: WaitForMultipleObjects not waiting Pin
Joe Woodbury9-Jun-08 12:59
professionalJoe Woodbury9-Jun-08 12:59 
GeneralRe: WaitForMultipleObjects not waiting Pin
wjeanveau9-Jun-08 13:10
wjeanveau9-Jun-08 13:10 
GeneralRe: WaitForMultipleObjects not waiting Pin
Joe Woodbury9-Jun-08 13:17
professionalJoe Woodbury9-Jun-08 13:17 
GeneralRe: WaitForMultipleObjects not waiting Pin
wjeanveau9-Jun-08 13:23
wjeanveau9-Jun-08 13:23 
GeneralRe: WaitForMultipleObjects not waiting Pin
Joe Woodbury9-Jun-08 13:42
professionalJoe Woodbury9-Jun-08 13:42 
GeneralRe: WaitForMultipleObjects not waiting Pin
wjeanveau9-Jun-08 14:36
wjeanveau9-Jun-08 14:36 
GeneralRe: WaitForMultipleObjects not waiting Pin
Joe Woodbury9-Jun-08 15:31
professionalJoe Woodbury9-Jun-08 15:31 
GeneralRe: WaitForMultipleObjects not waiting Pin
wjeanveau9-Jun-08 17:10
wjeanveau9-Jun-08 17:10 
Questionreading bits from file Pin
botnet9-Jun-08 11:06
botnet9-Jun-08 11:06 
AnswerRe: reading bits from file Pin
Jijo.Raj9-Jun-08 18:38
Jijo.Raj9-Jun-08 18:38 
GeneralRe: reading bits from file Pin
achainard10-Jun-08 2:45
achainard10-Jun-08 2:45 
GeneralRe: reading bits from file Pin
botnet10-Jun-08 13:09
botnet10-Jun-08 13:09 
AnswerRe: reading bits from file Pin
krmed10-Jun-08 6:08
krmed10-Jun-08 6:08 
QuestionBlank printout to printer Pin
Joe Smith IX9-Jun-08 7:04
Joe Smith IX9-Jun-08 7:04 
AnswerRe: Blank printout to printer [modified] Pin
Nelek9-Jun-08 8:12
protectorNelek9-Jun-08 8:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.