在可执行程序中运行 bat 批处理的问题
使用了一个第三方的 bat 处理包,整个 bat 包文件大小为 500KB(全部是 bat 文件),非常复杂,现在要对这个 bat 添加一些功能。
由于这个批处理太复杂,不想直接进行更改,目前更改想法为:建立一个 C++ 工程,在 C++ 代码中运行 bat 文件,然后再加入自己想要的功能,但出现了一些问题,功能加不上,示例代码如下:
test.bat 内容:
set PATH=%PATH%;c:\MyPath
C++ 工程中 cpp 文件内容:
#include <Windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) {
ShellExecute(NULL, L"open", L"c:\\Users\\admi\\Desktop\\test.bat", NULL, NULL, SW_SHOW);
wchar_t buffer[1024] = {0};
GetEnvironmentVariable(L"PATH", buffer, 1024);
wcout << buffer << endl;
return 0;
}
在我机器上运行结果为:
1、在 cmd 下直接运行 test.bat
C:\>test.bat
C:\>set PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;C:\Program Files\TortoiseSVN\bin;C:\Program Files\depot_tools\;C:\Program Files\OpenVPN\bin;c:\MyPath
可见,新添加的路径“c:\MyPath”已经加入了。
2、在 C++ 程序中执行的结果为:
C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Microsoft SQL Server\90\Tools\binn\;C:\Program Files\Common Files\Thunder Network\KanKan\Codecs;C:\Program Files\TortoiseSVN\bin;C:\Program Files\depot_tools\;C:\Program Files\OpenVPN\bin;C:\Program Files\Microsoft Visual Studio 8\;C:\ProgramFiles\Microsoft Visual Studio 8\VC\bin
“c:\MyPath”并没有被加入。也就是说在 C++ 中执行的批处理并没奏效,请问如何才能使执行的批处理奏效呢?