CreateEnvironmentBlock returning block too large for CreateProcessWithLogon on windows vista

643 views Asked by At

I'm using microsoft's CreateProcessWithLogonW function to run a program as a specific user. In almost all of our test scenario's everything is great. Our code is similar to the example on microsoft's page. The problem arises on windows vista. We create an environment block using:

CreateEnvironmentBlock(&lpvEnv, hToken, TRUE)

we then pass this to CreateProcessWithLogonW:

CreateProcessWithLogonW(argv[1], NULL, argv[2], 
        LOGON_WITH_PROFILE, NULL, argv[3], 
        CREATE_UNICODE_ENVIRONMENT, lpvEnv, szUserProfile, 
        &si, &pi)

The problem is that the environment block is larger than 5120 characters. The only location I've seen this mentioned is in the comments section of the microsoft page:

"Undocumented limitation on size of environment block The environment block passed with parameter 'lpEnvironment' must not contain more than 5120 characters including the terminating \0 characters and the trailing \0 block delimiter.

If the environment block exceeds that limit, then the call will issue error "0x000006F7: The stub received bad data."

On Windows 7 SP1, the documented limits are 32767 bytes for the environment and 8192 bytes for each variable. However, the CreateProcessWithLogonW function has not been adapted to these new limits. It still does not accept more than 5120 characters.


Is there anything that can be done about this? I'm hesitant to go hacking up the environment block.

1

There are 1 answers

0
Harry Johnston On

Hopefully by now this specific problem has been corrected. However, for future reference, the simplest workaround in this sort of situation is to use CreateProcessWithLogonW() to launch a proxy executable rather than the program you actually want to run. The proxy executable then sets up the environment as required and launches the target program using plain old CreateProcess().