[CPP]View PlainCopy
-
- void Dumpenvironmentstrings ()
-
- {
-
- #define MAX_ENVIRONMENT_NAME_LENGTH (128)
-
- #define MAX_ENVIRONMENT_VALUE_LENGTH (1024)
-
- LPTSTR lpszenvstring = getenvironmentstrings ();
-
- if (NULL = = lpszenvstring)
-
- {
-
- return;
-
- }
-
-
-
- tchar* penvblock = lpszenvstring;
-
- Const TCHAR sztoken = _t (' = ');
-
- TCHAR Szname[max_environment_name_length] = {0};
-
- TCHAR Szvalue[max_environment_value_length] = {0};
-
- While (' *penvblock '! =)
-
- {
-
- if (Sztoken = = *penvblock)
-
- {
-
- Penvblock + = _tcslen (penvblock) + 1;
-
- continue;
-
- }
-
- tchar* PToken = _TCSCHR (Penvblock, Sztoken);
-
- if (NULL = = PToken)
-
- {
-
- Break ;
-
- }
-
- memset (szName, 0, sizeof (szName));
-
- memset (szvalue, 0, sizeof (szvalue));
-
- Stringcchcopyn (SzName, _countof (szName), Penvblock, Ptoken-penvblock);
-
- stringcchprintf (Szvalue, _countof (szvalue), _t ("%s"), PToken + 1);
-
- _tprintf (_t ("%s:%s\r\n\r\n"), SzName, szvalue);
-
- Penvblock + = _tcslen (penvblock) + 1;
-
- }
-
- Freeenvironmentstrings (lpszenvstring);
-
- lpszenvstring = NULL;
-
- }
-
-
-
- Test code
-
- #include "stdafx.h"
-
- #include <Windows.h>
-
- #include <strsafe.h>
-
-
-
- int _tmain (int argc, _tchar* argv[])
-
- {
-
- Dumpenvironmentstrings ();
-
- return 0;
-
- }
http://blog.csdn.net/visualeleven/article/details/8122081
The environment variable information associated with the output process (using Getenvironmentstrings to get information, and then using Stringcchcopyn and stringcchprintf to ensure that the string does not cross)