5

This is the source code for "sleeper.exe" I have:

int main(int argc, char** argv) {
    cout<<argv[1];
    return 0;
}

When I call from command line like this:

C:\sleeper 5

I see

5

in command line so this works fine..

Now I am trying to call this exe from some other exe like this:

std::cout << "ret is:" << ret;
std::cout << "\n";

CreateProcess("sleeper.exe", // No module name (use command line)
               ret, // 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
             )

Here ret is 5 as well and I am sure because I see it in the commandline fine:

ret is: 5

There is a file called config.mpap in the same directory and I read the value from here like this:

std::ifstream myReadFile;
myReadFile.open("config.mpap");

char output[400];
if (myReadFile.is_open()) {
    while (!myReadFile.eof()) {
        myReadFile >> output;
    }
}
myReadFile.close();

char y = output[37];
int numberOfSleeps = y - '0'; // So now numberOfSleeps is 5

And then I convert numberOfSleeps to ret like this:

char* ret = NULL;
int numChars = 0;
bool isNegative = false;
// Count how much space we will need for the string
int temp = sleepTime;
do {
    numChars++;
    temp /= 10;
} while (temp);
ret = new char[ numChars + 1 ];
ret[numChars] = 0;
if (isNegative) ret[0] = '-';
int i = numChars - 1;
do {
    ret[i--] = sleepTime % 10 + '0';
    sleepTime /= 10;
} while (sleepTime);

Can please someone help me why ret is not passed to sleeper.exe from createprocess.exe?

EDIT:

It works like this:

if (!CreateProcess(NULL, // No module name (use command line)
                   "sleeper 5", // Command line

However this does not even compile:

std::string sleeper("sleeper ");
sleeper += ret;
if (!CreateProcess(NULL, // No module name (use command line)
                   sleeper, // Command line
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319

1 Answers1

3

The command line (second parameter of CreateProcess) takes the full command line, including the executable name. If the first argument is not NULL, it is used as the executable to run, but the command line still has to include an executable name. In the past even prepending a single space (giving an empty executable name) worked for me.

Philipp
  • 48,066
  • 12
  • 84
  • 109
  • This concurs with the description of CreateProcess() [here](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx). – Simon Nov 24 '13 at 19:49
  • Sorry my English is not good, do you mean passing "sleeper " as first argument should work? If you say so, it does not in my case. – Koray Tugay Nov 24 '13 at 20:07
  • @KorayTugay Your second code in the edit is in principle correct, but you need to pass a pointer to the C function. E.g. `std::vector buffer{sleeper.cbegin(), sleeper.end()}; buffer.push_back(0); CreateProcess(..., sleeper.data(), ...);` – Philipp Nov 24 '13 at 20:38
  • thanks for your answer. can you please tell me what I need to include so I can compile vector, cbegin and push_back ? – Koray Tugay Nov 24 '13 at 20:45
  • I am getting: main.cpp:78:5: error: ‘vector’ is not a member of ‘std’ std::vector buffer{sleeper.cbegin(), sleeper.end()}; – Koray Tugay Nov 24 '13 at 20:46
  • @Philipp I have tried this: const char *ptr1 = 0; ptr1= sleeper.data(); Does not work either... ( I tried passing ptr1 as argument ) – Koray Tugay Nov 24 '13 at 20:55
  • I have also tried this: const char *ptr1 = sleeper.c_str ( ); -> Not working. – Koray Tugay Nov 24 '13 at 20:58
  • This worked: const char *ch = sleeper.data(); LPSTR lpstr = const_cast(ch); – Koray Tugay Nov 24 '13 at 21:20
  • @KorayTugay using `const_cast` here may be UB. Copying to a vector is always well-defined. `#include ` should suffice. These are all very basic questions; please read [some introductory text](http://stackoverflow.com/questions/388242) first. – Philipp Nov 24 '13 at 21:30