Console to get and change environment variables

Source: Internet
Author: User

# Include <windows. h>
# Include <stdio. h>
# Include <tchar. h>
Void dumpenvvariables (ptstr penvblock []);
Void mydumpenvvariables0 (ptstr penvblock []);
Void mydumpenvvariables1 (ptstr penvblock []);
Void printenvironmentvariable (pctstr pszvariablename );
Int _ tmain (INT argc, tchar * argv [], tchar * envp [])
{
Dumpenvvariables (envp );
// Mydumpenvvariables0 (envp );
// Mydumpenvvariables1 (envp );
Printenvironmentvariable (L "Temp ");
Int I;
Scanf ("jjjj", & I );
Return 0;
}
Void printenvironmentvariable (pctstr pszvariablename ){
Ptstr pszvalue = NULL;
// Get the size of the buffer that is required to store the value
DWORD dwresult = getenvironmentvariable (pszvariablename, pszvalue, 0 );
If (dwresult! = 0 ){
// Allocate the buffer to store the environment variable value
DWORD size = dwresult * sizeof (tchar );
Pszvalue = (ptstr) malloc (size );
Getenvironmentvariable (pszvariablename, pszvalue, size );
_ Tprintf (text ("% s = % s \ n"), pszvariablename, pszvalue );
Free (pszvalue );
}
Else {

_ Tprintf (text ("'% s' = <unknown value> \ n"), pszvariablename );
}
}
Void dumpenvvariables (ptstr penvblock [])
{
Int current = 0;
Ptstr * pelement = penvblock;
Ptstr pcurrent = NULL; // The Initialization is null to avoid being a wild pointer.
While (pelement! = NULL)
{
Pcurrent = (ptstr) (* pelement );
If (pcurrent = NULL) // if it is the last item, the pelement is assigned null to exit the loop.
{
Pelement = NULL; // condition for loop exit
}
Else
{
_ Tprintf (text ("[% u] % s \ r \ n"), current, pcurrent );
Current ++;
Pelement ++;
}

}
}
Void mydumpenvvariables0 (ptstr penvblock []) // self-modified version, avoiding if judgment in the loop
{
Int current = 0;
Ptstr * pelement = penvblock;
Ptstr pcurrent = NULL;
Pcurrent = * pelement; // assign a value once

_ Tprintf (text ("\ r \ n getenv by mydumpenvvariables0 \ r \ n "));
While (pcurrent! = NULL) // use pcurrent as the variable for loop judgment
{

_ Tprintf (text ("[% u] % s \ r \ n"), current, pcurrent );
Pelement ++;
Current ++;
Pcurrent = * pelement;
}
}
Void mydumpenvvariables1 (ptstr penvblock []) // self-modified version to make the program simpler and easier to understand
{
Int current = 0;
Ptstr * pelement = penvblock;

_ Tprintf (text ("\ r \ n getenv by mydumpenvvariables1 \ r \ n "));
While (* pelement! = NULL) // use pcurrent as the variable for loop judgment
{

_ Tprintf (text ("[% u] % s \ r \ n"), current, * pelement );
Pelement ++;
Current ++;

}

}

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.