createprocess suspended by default [closed]

▼魔方 西西 提交于 2019-12-02 19:17:01

问题


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?


回答1:


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)};


来源:https://stackoverflow.com/questions/14407743/createprocess-suspended-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!