For CreateProcess, there are more detailed documents in msdn, but the lpenvironment parameter is not detailed. It only tells us how to use this parameter, however, the content that can be passed by this parameter is closed, so what kind of environment variables must be passed and what cannot be passed? This article will discuss this. Of course, you can set this parameter to null, so there is no need to look down.
1.1 normal environment variables
Windows provides a function named getenviromentstring to obtain available environment variables in the program. Let's first look at what variables are defined under normal circumstances.
Wchar * penv = getenvironmentstrings ();
While (* penv! = 0)
{
Wprintf (wchar_t *) penv );
Wprintf (L "/N ");
Penv + = wcslen (penv) + 1;
}
Freeenvironmentstrings (penv );
The above code will output all the environment variables. It is noted that the returned value of getenvironmentstrings is 0x10000. This is the address of the process environment block and cannot be directly modified! Some of the variables obtained from the code above can be seen in the environment variables of the system properties, but some cannot be seen.
1.2 create minimum environment variables for processes
In the environment variables listed above, which are necessary to create a process? Try to use this code to create a sub-process:
Wchar env [20000];
Memset (ENV, 0, sizeof (wchar) * 20000 );
Startupinfo Si;
Memset (& Si, 0, sizeof (SI ));
Si. cb = sizeof (SI );
Process_information PI;
Memset (& Pi, 0, sizeof (PI ));
Createprocessw (L "E: // windows // notepad.exe", null, false,
Create_unicode_environment, ENV, null, & Si, & PI );
The env parameter passed in this Code does not have any content. At this time, Vs will prompt that the application initialization fails, but the return value of CreateProcess is true, and Pi can get the ID of the new process. If createprocessw is called in a sub-process, no error message is displayed!
The environment variable values obtained by getenviromentstring are substituted into one by one. It can be found that at least one environment variable named systemroot is required for CreateProcess to succeed. The value of this variable cannot be rewritten and must be directed to the Windows root directory.
When debugging cygwin. dll, we found that the home, homepath, and appdata variables cannot be passed to the sub-process, but they can be passed in a separately written test program. It's strange!