如何将参数传递给我调用的exe(通过CreateProcess)?

时间:2013-11-24 10:17:47

标签: c++ executable

我有一个sleeper.exe。

sleeper.exe的主要方法是:

int main(int argc, char** argv) {

    SleeperClass sleeper;
    sleeper.sleep();
    return 0;

}

和Sleeper类是:

使用namespace 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";

}

我想要实现的是这个。我想从createprocess.exe调用sleeper.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's environment block
            NULL, // Use parent's 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 传递给sleeper.exe?

感谢。

编辑:我无法使用命令行,因为我正在从文件中读取参数。

1 个答案:

答案 0 :(得分:4)

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它就会睡眠1000毫秒

createprocess.exe source:

 ...
 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's environment block
            NULL, // Use parent's 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';
    }
}