System() to start a process, but using CreateProcess fails?

0

So, curious problem, I'm trying to create a process, and then resume it, mostly exploring the Windows API. I've noticed that if I do this:

system("C:\\Windows\\System32\\calc.exe");

It will open a calculator exe, however if I try to do the same thing using CreateProcessA, I get this:

STARTUPINFO starting_info;
PROCESS_INFORMATION process_info;

// let's try and make a process
if (!CreateProcessA(NULL, "C:\\Windows\\System32\\calc.exe", NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &starting_info, &process_info)) {
    return;
}
// resume thread
NtResumeThread(process_info.hThread, NULL);

This for some reason throws an error of 0xc0000142 most of the times when it "creates" the process, else it just fails.

What's going on?

c++
windows
process
asked on Stack Overflow Feb 11, 2016 by Rivasa

1 Answer

2

See the following MSDN sample code for creating a process: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682512(v=vs.85).aspx

You need to zero out the si and pi structs, also set

si.cb = sizeof(si);

In the end, close process and thread handles.

CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
answered on Stack Overflow Feb 11, 2016 by Garland

User contributions licensed under CC BY-SA 3.0