0

I would like to know how to loop a function to open an executable program multiple times. I tried just putting system(path) and CreateProcess(Lpath, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL) in a while, do while, and for loop, but that only opens the program once.

Here's what the relevant code looks like right now:

for(int i=0; i<10; i++)
    {    
        CreateProcess(L"C:\\Users\\Ben\\Documents\\Visual Studio 2010\\Projects\\RANDWritter\\Debug\\RANDWritter.exe", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
    }

Anyone know what to do?

Soren
  • 14,402
  • 4
  • 41
  • 67
  • That's what `for` and `do`/`while` are for. Please get a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on C++ and learn the basics of the language. – Captain Obvlious Feb 26 '14 at 22:07
  • I'll have you know I've had two years formal experience and plenty of informal experience on the language. I've tried a for loop before and that didn't work and I just tried a do while and that didn't work either. – TheMohawkNinja Feb 26 '14 at 22:12
  • @TheMohawkNinja: Any of the things you described should work fine, so you need to show what you tried as a [small, selfcontained, compilable example](http://sscce.org) if you want people to tell you what you are doing wrong. – Chris Dodd Feb 26 '14 at 22:19
  • 2
    Possible hint: look if you can open the app in question yourself (manually) multiple times simultaneously. Some apps register themselves as "running" and disallow 2nr (or more) concurrent runs. The 2nd instance to run might just activate the 1st and quit immediately... – Carl Colijn Feb 26 '14 at 22:27
  • Are you capturing errors? – Anthony Feb 26 '14 at 22:36
  • @Carl Colijin I can run multiple instances manually. – TheMohawkNinja Feb 26 '14 at 22:43
  • @anthony-arnold I don't quite know what you mean. If you are asking if any errors pop up in runtime then no, and if you mean if I have any try/catch statements, that would also be a no. – TheMohawkNinja Feb 26 '14 at 22:44
  • The `CreateProcess` function doesn't throw an exception. It returns 0 if it fails, which you should check for, after which you can call `GetLastError` to get an error code. – Anthony Feb 26 '14 at 22:57
  • @anthony-arnold I put in: DWORD dw=GetLastError(); cout< – TheMohawkNinja Feb 27 '14 at 00:18
  • @anthony-arnold Sorry, I keep hitting 'enter' to go to a new line instead of 'shift+enter'. It outputs 87. – TheMohawkNinja Feb 27 '14 at 00:20
  • Well, the whole point of the program was to open up multiple instances of a program to write to a text file, and I am getting a huge increase in the output to the text file all of a sudden, so it's almost as if there are multiple instances running. – TheMohawkNinja Feb 27 '14 at 00:39
  • @TheMohawkNinja: If the processes really do get created you should see evidence if them in TaskManager. Also, if there's a sudden huge increase in the text file's size, then it might be write buffers not getting flushed properly? – Carl Colijn Feb 27 '14 at 07:58

1 Answers1

1

Before checking with GetLastError, it's always wise to see if the function you called returned an error code or not. If it just returned a success code, then the value you get from GetLastError might be bogus. So do also use BOOL createdOK = CreateProcess(...), and check for the returned value being != FALSE before calling GetLastError.

But if it's genuinely error 87 you get, then that explains: it's name is ERROR_INVALID_PARAMETER (you can look them up at MSDN's System Error Codes page). Looking at the MSDN docs for CreateProcess, you probably cannot omit the lpStartupInfo and lpProcessInformation parameters. It's also just a good idea to fill these in regardless, since they give you info on the newly created process. More so since the handles returned in the ProcessInformation must also be CloseHandle'd manually after you're done with them.

Carl Colijn
  • 1,423
  • 9
  • 29