bobpoekert
bobpoekert

Reputation: 1014

CreateProcessW Acess Violation in RtlInitUnicodeString

When I execute the following process-spawning code with a valid command string, I get an access violation from inside CreateProcessW trying to call RtlInitUnicodeString. I know that this can happen when you pass a const command string since CreateProcessW (for reasons beyond my understanding) mutates the command string. But I'm copying the command string on to the heap with _wcsdup, so that shouldn't be an issue.

One interesting thing to note is that the access violation only happens when the command string is valid. If there's a parse error in it or it refers to an executable that doesn't exist, there's no access violation.

Process(wchar_t *command_string) {
    error = 0;
    SECURITY_ATTRIBUTES security_attrs;
    STARTUPINFO startup_info;

    //I'm copying the string here because CreateProcessW mutates its arguments
    wchar_t *new_commands = _wcsdup(command_string);

    security_attrs.nLength = sizeof(SECURITY_ATTRIBUTES);
    security_attrs.bInheritHandle = TRUE;
    security_attrs.lpSecurityDescriptor = NULL;

    CreatePipe(&_stdout, &stdout_in, &security_attrs, 0);
    SetHandleInformation(_stdout, HANDLE_FLAG_INHERIT, 0);

    CreatePipe(&stdin_out, &_stdin, &security_attrs, 0);
    SetHandleInformation(_stdin, HANDLE_FLAG_INHERIT, 0);

    printf(">>launching process: %ls\n", new_commands);
    if (!CreateProcessW(
        NULL, //process (extracted from the arg list instead)
        (LPTSTR) new_commands, //arg list
        &security_attrs,
        &security_attrs,
        TRUE, //inherit handles
        0, //flags
        NULL, //use env of parent
        NULL, //use cwd of parent
        &startup_info,
        &info
    )) {
        error = GetLastError();
        printf(">>failed to create process: %d\n", error);
    } else {
        printf(">>launched process\n");
        printf(">>process id: %d\n", info.dwProcessId);
    }
    free(new_commands);
}

Upvotes: 0

Views: 764

Answers (1)

martona
martona

Reputation: 5914

You're not initializing STARTUPINFO. I would suspect the problem lies with the stack garbage that Windows thinks you're passing it.

Upvotes: 3

Related Questions