- 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 "), szName, szValue);
- pEnvBlock += _tcslen(pEnvBlock) + 1;
- }
- FreeEnvironmentStrings(lpszEnvString);
- lpszEnvString = NULL;
- }
- // 测试代码
- #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