Sunny
Sunny

Reputation: 7604

Crash during creation of three new processes using C in Windows

I am creating three child processes in C using the following:

    typedef struct{
        STARTUPINFO si;
        PROCESS_INFORMATION pi;
    }PROCESS_PARAMS;

    PROCESS_PARAMS pChildren[3];

    wchar_t *szCmdline[3] = {L"nmtest -s \"TS\" -r \"Test DMR\" -tLD -d \" /child1/tclient\"",
 L"nmtest -s \"TS\" -r \"Test DMR\" -tLD -d \" /child2/tclient\"", 
L"nmtest -s \"TS\" -r \"Test DMR\" -tLD -d \" /child3/tclient\""};


    for (i=0; i<3; i++)
                {
                    pChildren[i].si.cb = sizeof(pChildren[i].si);
                    GetStartupInfo(&pChildren[i].si);
                    ZeroMemory(&pChildren[i].pi, sizeof(pChildren[i].pi));

                    // Start the child processes.
                    CreateProcess(NULL,   // No module name (use command line)
                                  szCmdline[i],          // 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
                                  &pChildren[i].si,            // Pointer to STARTUPINFO structure
                                  &pChildren[i].pi);         // Pointer to PROCESS_INFORMATION structure
                }

But the code crashes while creating the new processes and i suspect something is wrong with the command line arguments. But I am not sure what is wrong. Could someone please help me with this? Thanks.

PS: it works when for one instance when i give it in the following way:

wchar_t szCmdline[] = L"nmctest -s \"TwonkyServer[julka]\" -r \"NMC Test DMR [julka960]\" -tLDMR -d \" /child1/twonkyclient\""; 

And then in i do:

CreateProcess(NULL,   // No module name (use command line)
                                  szCmdline,          // 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
                                  &pChildren[i].si,            // Pointer to STARTUPINFO structure
                                  &pChildren[i].pi); 

Note that i give szCmdline instead of szCmdline[i]. THen it works but i am not sure when is the problem in the topmost code.

Upvotes: 1

Views: 116

Answers (1)

Michael Burr
Michael Burr

Reputation: 340218

From the docs for CreateProcess():

The Unicode version of this function, CreateProcessW, can modify the contents of this string. Therefore, this parameter cannot be a pointer to read-only memory (such as a const variable or a literal string). If this parameter is a constant string, thefunction may cause an access violation.

And it appears that you're using the Unicode version and passing in pointers to string literals (which are not modifiable).

This isn't a problem for the CreateProcessA() version of the function because internally it converts the ANSI command line to Unicode, and the converted string ends up in modifiable memory.

In the version that works you're initializing the string into an array that's modifiable and passing the address of that array instead of passing a pointer to a string literal.

For some history on this situation, see Raymond Chen's "Why does the CreateProcess function modify its input command line?".

Example code if you need a bit more help:

// a helper function to duplicate a unicode string
wchar_t* xwcsdup( wchar_t const* s)
{
    wchar_t* p = _wcsdup(s);

    if (!p) {
        abort();    // or however you want to fail miserably
    }

    return p;
}



// and your loop to create the processes...

for (i=0; i<3; i++)
{
    wchar_t* cmdline = xwcsdup(szCmdline[i]);

    pChildren[i].si.cb = sizeof(pChildren[i].si);
    GetStartupInfo(&pChildren[i].si);
    ZeroMemory(&pChildren[i].pi, sizeof(pChildren[i].pi));

    // Start the child processes.
    CreateProcess(NULL,   // No module name (use command line)
                  cmdline,        // 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
                  &pChildren[i].si,            // Pointer to STARTUPINFO structure
                  &pChildren[i].pi);         // Pointer to PROCESS_INFORMATION structure

    free(cmdline);
}

Upvotes: 2

Related Questions