# 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 ++;
}
}