سؤال

I have this function inside a dll:

int createChildProcess()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;
    int res;

    si.cb=sizeof(STARTUPINFO);

    STARTUPINFO* ptr=&si;
    if(!CreateProcess(L"c:\\windows\\notepad.exe", NULL, 0, 0, false, CREATE_NEW_CONSOLE, 0, 0, &si, &pi))
    {
        mylog << "CreateProcess error: " << GetLastError() << std::endl;
        res = 0;
    }
    else
        res = pi.dwProcessId;

    CloseHandle( pi.hProcess );
    CloseHandle( pi.hThread );

    return res;
}

And i found 2 problems:

1) The first time the function is called, return always error code 87 (The parameter is incorrect.).

2) The second time, the function works, but creates a child process in suspended status and the cpu usage in the father process goes to 100%.

Im testing with winXP sp3, and my dll was compiled in vs 2010.

any help?

هل كانت مفيدة؟

المحلول

You have initialized the cbSize field of your STARTUPINFO structure, but you've left the rest uninitialized. The first time you call CreateProcess, the uninitialized values are evidently so wrong that the function recognizes that they're wrong and gives up. You were lucky.

The second time you call it, the values are apparently such that the CreateProcess thinks you have asked it to do something it knows how to do, and so it proceeds. Such is the nature of undefined behavior.

To fix this, initialize all of the struct:

STARTUPINFO si = {sizeof(STARTUPINFO)};
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top