BatchGuy
BatchGuy

Reputation: 65

CreateProcess won't start the proccess with arguments

So I used this code in order to start a console application with arguments:

#include <iostream>
#include <windows.h>

using namespace std;

void StartProgram(char argv[])
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory(&si, sizeof(si));
    si.cb = sizeof(si);
    ZeroMemory(&pi, sizeof(pi));

    CreateProcess
    (
        TEXT("PlayerDebug.exe"),
        (LPSTR)argv,
        NULL,NULL,FALSE,
        CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW,
        NULL,NULL,
        &si, &pi
    );
};

int main()
{
    StartProgram("sound.wav");
    return 0;
}

"PlayerDebug.exe" display the arguments used to call it. But when I run it with CreateProcess the way I showed, it doesen't display anything. I checked and in Task Manager it seems to appear, but still does not display the arguments. I also tried to write cout << argv; in function void StartProgram(char argv[]) and it returned "sound.wav", which is correct. But it seems the argument is not passed to PlayerDebug.exe and I don't know why. What I did wrong? (I'm new at C++ programming)

Upvotes: 3

Views: 2338

Answers (1)

Bob Moore
Bob Moore

Reputation: 6894

The second parameter to CreateProcess is the full command line, not just the parameters to the EXE. Lets take two examples :

CreateProcess ("c:\\notepad.exe",
               "c:\\notepad.exe c:\\wibble.txt",
               ...);

will work fine (if there is a copy of notepad.exe and a file called wibble.txt in the root of C:), whereas

CreateProcess ("c:\\notepad.exe",
               "c:\\wibble.txt",
               ...);

will launch the EXE but fail to open the file. What this means is that when the help systems calls the second parameter the command line, it ain't lying - it wants the whole command line.

Note that you can use NULL as the first parameter if the whole command line is in the second param. That's how I normally use it in fact.

Upvotes: 10

Related Questions