Setting PATH variable is not working

不打扰是莪最后的温柔 提交于 2019-12-14 04:07:43

问题


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

I have a script which I call from a Visual Studio 2013 project (C++). In that script, I try to set my path variable. When the PATH variable gets set, it seems like the it is including visual studio stuff in the path and copying the path more than once. Not sure why.

Note: Running the bat directly and running from the command prompt 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%;"
)

Path which gets 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 triggers a break point:

#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 am not entire sure what the following lines are supposed to be doing:

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

回答1:


From Windows Dev-center

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

Also: make sure your PATH variable doesn't exceed the maximum allowed size.



来源:https://stackoverflow.com/questions/25917796/setting-path-variable-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!