winapicreateprocesscompatibility-mode

Why Doesn't CreateProcess() Work?


I have a program that attempts to restart Explorer in Windows 7; it first ends the process, then starts it again.

Simple?

So it looks -- it indeed seems to work. However, I noticed something funny: When I try to start a program (say, XP's Power Calculator powertoy) under compatibility mode, it no longer works! Nothing happens -- the process is created and immediately quits.

If I run Explorer through the Run dialog of Task Manager, the program runs fine.

What's going on?!

#include <windows.h>
int main()
{
    PROCESS_INFORMATION pi;
    STARTUPINFO si = {sizeof(si)};
    TCHAR path[] = TEXT("explorer");
    DWORD f = 0; //I tried a variety of these flags; didn't work
    return CreateProcess(NULL, path, NULL, NULL, FALSE, f, NULL, NULL, &si, &pi);
}

Edit 1:

Edit 2:

The same exact code just worked for me a couple of times, and then stopped working again... huh?


Solution

  • This isn't really an answer to the "why", but I managed to find out how to fix it myself:

    Instead of copying environmental variables from the current process, if I copy them with CreateEnvironmentBlock, then it works.

    I still haven't figured out what's causing it, though...