Minimum environment variables required by CreateProcess

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.