Setting the PATH variable does not work

I apologize for the vague title, was not sure how to classify my problem.

I have a script that I am calling from a Visual Studio 2013 project (C ++). In this script, I am trying to set my path variable. When the PATH variable is set, it seems that it includes the visual objects of the studio in the path and copies the path more than once. I do not know why.

Note. Executing bits directly and running from the command line does not present this error.

.cpp

int main(void) { system("CALL C:\\HFSS\\setup_vars.bat"); return 0; } 

.bat

 :: Set PATH Variable set path_str=%PATH% set addPath=%ValueValue% echo %addPath% echo %ValueValue% PAUSE echo %PATH%| find /i "%addPath%">NUL if NOT ERRORLEVEL 1 ( SETX PATH "%PATH% ) else ( SETX /M PATH "%PATH%;%addPath%;" ) 

The path to be added:

 C:\Program Files\AnsysEM\AnsysEM15.0\Win64; C:\Program Files\AnsysEM\AnsysEM15.0\Win64; C:\Program Files\AnsysEM\AnsysEM15.0\Win64; C:\Program Files\AnsysEM\AnsysEM15.0\Win64; C:\Program Files\AnsysEM\AnsysEM15.0\Win64; C:\Program Files (x86)\Microsoft Visual Studio 12.0\; C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\amd64 

UPDATE:

I did some research on CreateProcess and how to create a new environment block.

The following code launches a breakpoint:

 #include <iostream> #include <Windows.h> #define WINDOWS_LEAN_AND_MEAN #include <strsafe.h> #define BUFSIZE 4096 int main(void) { LPTSTR lpszCurrentVariable; TCHAR szAppName[] = TEXT("C:\\windows\\system32\\cmd.exe"); STARTUPINFO si; PROCESS_INFORMATION pi; BOOL fSuccess; LPSTR lpszVariable; LPWCH lpvEnv; lpvEnv = GetEnvironmentStrings(); if (lpvEnv == NULL) { std::cout << "GetEnvironmentStrings() Failed\n"; system("Pause"); return 0; } lpszCurrentVariable = lpvEnv; if (FreeEnvironmentStrings(lpvEnv) != 0) { std::cout << "Freed block!\n"; } if (FAILED(StringCchCopy(lpszCurrentVariable, BUFSIZE, L"MyNewOwnEnvSetting=ver 2.0"))) { system("Pause"); return 1; } lpszCurrentVariable += lstrlen(lpszCurrentVariable) + 1; if (FAILED(StringCchCopy(lpszCurrentVariable, BUFSIZE, L"MyNewOwnVar=MyPath"))) { std::cout << "StringCchCopy() - String copy #2 failed\n"; system("Pause"); return 1; } lpszCurrentVariable += lstrlen(lpszCurrentVariable) + 1; *lpszCurrentVariable = (WCHAR)0; SecureZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); fSuccess = CreateProcess(szAppName, L"C:\HFSS\setup_vars.bat", NULL, NULL, TRUE, NULL, (LPVOID)lpszCurrentVariable, NULL, &si, &pi); DWORD ret = WaitForSingleObject(pi.hProcess, INFINITE); system("Pause"); return 0; } 

I'm not quite sure what the following lines should do:

 if (FAILED(StringCchCopy(lpszCurrentVariable, BUFSIZE, L"MyNewOwnEnvSetting=ver 2.0"))) { system("Pause"); return 1; } lpszCurrentVariable += lstrlen(lpszCurrentVariable) + 1; if (FAILED(StringCchCopy(lpszCurrentVariable, BUFSIZE, L"MyNewOwnVar=MyPath"))) { std::cout << "StringCchCopy() - String copy #2 failed\n"; system("Pause"); return 1; } 
0
c ++ path environment-variables batch-file
source share
1 answer

From Windows Development Center

By default, a child process inherits the environment variables of its parent process. Programs launched by the shell inherit shell variables. To specify a different environment for the child process, create a new environment block and pass a pointer to it as a parameter to the CreateProcess function.

Also: make sure the PATH variable does not exceed the maximum size allowed .

+1
source share

All Articles