У меня есть sleep.exe.
Основным методом для sleepper.exe является:
int main(int argc, char** argv) {
SleeperClass sleeper;
sleeper.sleep();
return 0;
}
и класс Sleeper:
использование пространства имен std;
SleeperClass::SleeperClass() {
}
SleeperClass::SleeperClass(const SleeperClass& orig) {
}
SleeperClass::~SleeperClass() {
}
void SleeperClass::sleep() {
cout << "Message 1\n";
Sleep(2000);
cout << "Message 2 a two seconds after Message 1";
}
Я хочу этого добиться. Я хочу вызвать sleepper.exe из createprocess.exe. Но я хочу, чтобы передать переменную, чтобы сказать, спящий exe спать, пока я хочу. (вместо 2000)
Вот мой источник createprocess.exe:
int main(int argc, TCHAR *argv[]) {
std::ifstream myReadFile;
myReadFile.open("config.mpap");
char output[400];
if (myReadFile.is_open()) {
while (!myReadFile.eof()) {
myReadFile >> output;
}
}
myReadFile.close();
std::cout << "\n";
char x = output[19];
int numberOfChildren = x - '0';
char y = output[37];
int numberOfSleeps = y - '0';
char z = output[49];
int sleepTime = z - '0';
std::cout << "Number of children: ";
std::cout << numberOfChildren;
std::cout << "\n";
std::cout << "Number of sleeps: ";
std::cout << numberOfSleeps;
std::cout << "\n";
std::cout << "Sleep Time: ";
std::cout << sleepTime;
std::cout << "\n";
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof (si));
si.cb = sizeof (si);
ZeroMemory(&pi, sizeof (pi));
while (numberOfSleeps > 0) {
std::cout<<"Child process being created";
--numberOfSleeps;
// Start the child process.
if (!CreateProcess("c:\\sleeper.exe", // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent environment block
NULL, // Use parent starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
) {
printf("CreateProcess failed (%d).\n", GetLastError());
return 0;
}
std::cout << pi.dwProcessId;
std::cout << "\n";
}
// Wait until child process exits.
WaitForSingleObject(pi.hProcess, INFINITE);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
Как передать sleeptime в файл sleepper.exe?
Благодарю.
Изменение: я не могу использовать командную строку, потому что я читаю параметры из файла.
sleeper.exe
void SleeperClass::sleep(int sleeptime) {
cout << "Message 1\n";
Sleep(sleeptime);
cout << "Message 2 a two seconds after Message 1";
}
int main(int argc, char** argv) {
int sleeptime=2000; //default sleeptime is 2000
if(argc>=2) //if sleeper.exe has any params and the first param is sleeptime
{
sleeptime=atoi(argv[1]); // here you should check whether argv[1] is legal
}
SleeperClass sleeper;
sleeper.sleep(sleeptime);
return 0;
}
в cmdline вы просто введите sleeper.exe 1000
он будет спать sleeper.exe 1000
мс
Источник createprocess.exe:
...
char sleeptime_str[10];
itoa(sleepTime,sleeptime_str);
if (!CreateProcess("c:\\sleeper.exe", // No module name (use command line)
sleeptime_str, // Command line the param is sleeptime_str
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent environment block
NULL, // Use parent starting directory
&si, // Pointer to STARTUPINFO structure
&pi) // Pointer to PROCESS_INFORMATION structure
) {
printf("CreateProcess failed (%d).\n", GetLastError());
return 0;
}
...
и функция itoa
void itoa(int value, char *str)
{
if (value < 0)
{
str[0] = '-';
value = 0-value;
}
int i,j;
for(i=1; value > 0; i++,value/=10)
str[i] = value%10+'0';
for(j=i-1,i=1; j-i>=1; j--,i++)
{
str[i] = str[i]^str[j];
str[j] = str[i]^str[j];
str[i] = str[i]^str[j];
}
if(str[0] != '-')
{
for(i=0; str[i+1]!='\0'; i++)
str[i] = str[i+1];
str[i] = '\0';
}
}