CreateProcess fails with GetLastError=0


I call CreateProcess with the lpEnvironment parameter pointing to a buffer containing a lot of environment variables (see below) and the result is FALSE, with GetLastError = 0.
Then I recall CreateProcess, but with lpEnvironment = NULL, and I get the same results.

As the Winexec or ShellExececute functions returns 0 if the OS is out of memory or resources, I tried to reboot my machine (even if I run NT !!), but the CreateProcess behaviour keeps the same.

What's happening? Any comments or suggestions welcome.


my buffer format for the lpEnvironment: 'envVar1=value1§envVar2=value2§...envVarX=valueX§§', where the §§ stands for a zero byte)
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Post the code for the call.
Is it possible that there is some other API call (successful) got inbetween CreateProcess() and GetLastError()?
Seeing that you get the same strange results with the environment parameter set to NULL or not, this is the CreateProcess I use that works.  Try it out to make sure your system is behaving properly, as it is quite simple.  And then from there you can narrow down what is going wrong.

   // process initialization information
   startUpInfo.cb = sizeof(STARTUPINFO);
   startUpInfo.lpReserved = NULL;
   startUpInfo.lpDesktop = NULL;
   startUpInfo.lpTitle = NULL;
   startUpInfo.dwFlags = NULL;
   startUpInfo.cbReserved2 = 0;
   startUpInfo.lpReserved2 = NULL;
   startUpInfo.wShowWindow = SW_SHOWDEFAULT;

if (!(CreateProcess(NULL,
   cout << "Error creating process" << endl;
   dwErrorCode = GetLastError();
   return -1;

To make sure that you are getting the right response from GetLastError(), I'd switch the two lines around.  Make the call to GetLastError() before your 'cout' line.  Who knows what the implementation of cout is calling.  Since this is a console app(?), it would undoubtedly call some of the Console APIs.  These APIs (which are successful) would zero out the GetLastError() code for the thread.

So, call GetLastError first, and then print your message.  Even better, make the error code part of the error message:

cout << "Error creating process.  GetLastError() == " << dwErrorCode << endl;
You may have to make a call to AllocConsole before running it like that.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.