in src/ServiceMonitor/IISConfigUtil.cpp [249:367]
HRESULT IISConfigUtil::UpdateEnvironmentVarsToConfig(WCHAR* pstrAppPoolName)
{
HRESULT hr = S_OK;
LPTCH lpvEnv = NULL;
LPTSTR lpszVariable = NULL;
wstring pstrAddCmd;
wstring pstrRmCmd;
BOOL fMoreData = TRUE;
unordered_map<wstring, LPTSTR> filter;
vector<pair<wstring, wstring>> envVec;
vector<pair<wstring, wstring>>::iterator envVecIter;
POPULATE(filter) ;
lpvEnv = GetEnvironmentStrings();
if (lpvEnv == NULL)
{
_tprintf(L"Failed to call GetEnvironmentStrings! \n");
hr = E_FAIL;
goto Finished;
}
lpszVariable = (LPTSTR)lpvEnv;
while (*lpszVariable)
{
LPTSTR pEqualChar = wcschr(lpszVariable, L'=');
if (pEqualChar != lpszVariable)
{
DWORD dwStatus = 0;
LPTSTR pstrValue = pEqualChar + 1;
LPTSTR pstrName = lpszVariable;
pEqualChar[0] = L'\0';
wstring strNameCheck(pstrName);
if (FilterEnv(filter, CharUpper((LPWSTR)strNameCheck.c_str()), pstrValue))
{
pEqualChar[0] = L'=';
lpszVariable += lstrlen(lpszVariable) + 1;
continue;
}
envVec.emplace_back(wstring(pstrName), wstring(pstrValue));
pEqualChar[0] = L'=';
}
//
// move to next environment variable
//
lpszVariable += lstrlen(lpszVariable) + 1;
}
envVecIter = envVec.begin();
while (fMoreData)
{
pstrRmCmd.clear();
fMoreData = FALSE;
hr = BuildAppCmdCommand(envVec, envVecIter, pstrAppPoolName, pstrRmCmd, APPCMD_RM);
if (hr != ERROR_MORE_DATA && FAILED(hr))
{
goto Finished;
}
if (hr == ERROR_MORE_DATA)
{
hr = S_OK;
fMoreData = TRUE;
}
//
//allow appcmd to fail if it is trying to remove environment variable
//
RunCommand(pstrRmCmd, TRUE);
}
fMoreData = TRUE;
envVecIter = envVec.begin();
while (fMoreData)
{
pstrAddCmd.clear();
fMoreData = FALSE;
hr = BuildAppCmdCommand(envVec, envVecIter, pstrAppPoolName, pstrAddCmd, APPCMD_ADD);
if (hr != ERROR_MORE_DATA && FAILED(hr))
{
goto Finished;
}
if (hr == ERROR_MORE_DATA)
{
hr = S_OK;
fMoreData = TRUE;
}
//
//appcmd must succeed when add new environment variables
//
hr = RunCommand(pstrAddCmd, FALSE);
if (FAILED(hr))
{
goto Finished;
}
}
Finished:
if (lpvEnv != NULL)
{
FreeEnvironmentStrings(lpvEnv);
lpvEnv = NULL;
}
return hr;
}