windows COMSPEC 和 PATH 环境变量应该是什么来使用 system() 定位命令解释器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2276483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What should COMSPEC and PATH environmental variables be to locate the command-interpreter, using system()?
提问by T.T.T.
Are the OS (XP) environmental variables the same used in a process running from visual studio .NET C++?
从 Visual Studio .NET C++ 运行的进程中使用的操作系统 (XP) 环境变量是否相同?
It seems the command interpreter is not found:
When using NULL as the command, system() returns 0and with command - ENOENT Command interpreter cannot be found
.
似乎没有找到命令解释器:
当使用 NULL 作为命令时,system() 返回 0并使用 command - ENOENT Command interpreter cannot be found
。
In windows (System->Environmental Variables), COMSPEC contains the path to cmd.exe
PATH does not.
在 Windows(系统->环境变量)中,COMSPEC 包含 cmd.exe 的
路径,但 PATH 不包含。
What should PATH be?
PATH 应该是什么?
Other than this, not sure why it can not find the interpreter.
除此之外,不知道为什么它找不到解释器。
Any suggestions are appreciated. Thanks.
任何建议表示赞赏。谢谢。
if( system("tail -500 log.txt") == -1)
{
//Error calling tail.exe on log
//errno is a system macro that expands int returning
//the last error. strerror() converts the error to it's
//corresponding error message.
printf("Error calling tail.exe with system(): %s",strerror( errno ));
}
EDIT1
Stepping into system() argv[0] = _tgetenv(_T("COMSPEC"));
returns a bad pointer. Being this is a cgi executable, the COMPSEC is not properly set or inherited from the OS.
I now set COMSPEC before the process is started and use CreateProcess() as in example 2
EDIT1
进入 system() 会argv[0] = _tgetenv(_T("COMSPEC"));
返回一个错误的指针。由于这是一个 cgi 可执行文件,COMPSEC 没有正确设置或从操作系统继承。
我现在在进程启动之前设置 COMSPEC 并使用 CreateProcess() 如示例 2
However, create process still returning 0? Getting closer. See any issues with this? Thanks.
但是,创建进程仍然返回0?越来越近。看到这有什么问题吗?谢谢。
if (! SetEnvironmentVariable("COMSPEC", "C:\WINDOWS\system32\cmd.exe") )
{
printf("SetEnvironmentVariable failed (%d)\n", GetLastError());
}
//r = system("dir c:\");
r = CreateProcess("dir.exe", NULL, NULL, NULL, TRUE, NULL,
NULL, // inherit parent's environment
NULL, &si, &pi);
EDIT 2
SetEnvironmentVariable() did not work. However, putenv does._putenv( "COMSPEC=C:\\WINDOWS\\system32\\cmd.exe" ); // C4996
Not sure what the difference is...?
Now that this env var is set, any request on the cgi app from the browser gives the option to save the cgi.exe instead of executing it.. Not sure why this has changed based on this env var?
编辑 2
SetEnvironmentVariable() 不起作用。但是, putenv 确实如此。_putenv( "COMSPEC=C:\\WINDOWS\\system32\\cmd.exe" ); // C4996
不知道有什么区别...?
现在设置了这个 env var,来自浏览器的 cgi 应用程序上的任何请求都提供了保存 cgi.exe 而不是执行它的选项。不知道为什么这会根据这个 env var 改变?
回答by t0mm13b
The environment variables are inherited when running a process, including system(...)
call. Unless there is something weird going on, usually running %windir%\system32\cmd.exe should do the trick, it should expand the environment variable, unless you can use the API to get the windows directory 'GetWindowsDirectory'. See here for an example from the MSDN.
运行进程时会继承环境变量,包括system(...)
调用。除非发生了一些奇怪的事情,通常运行 %windir%\system32\cmd.exe 应该可以解决问题,它应该扩展环境变量,除非您可以使用 API 来获取 Windows 目录“ GetWindowsDirectory”。有关MSDN的示例,请参见此处。
Edit:IIRC, COMSPEC environment variable, if done on the command line
编辑:IIRC、COMSPEC 环境变量,如果在命令行上完成
> echo %COMSPEC% C:\WINDOWS\system32\cmd.exe
You got a bad pointer, because it is not probably set up, the above echo command should prove that, if you get no output, it is not set, right click on 'My Computer', left-click on 'Properties', a dialog with tab-pages appear, click on 'Advanced', look for 'Environment Variables'...see the two screenshots here...
你得到了一个错误的指针,因为它可能没有设置,上面的echo命令应该证明,如果你没有输出,它没有设置,右键单击“我的电脑”,左键单击“属性”,一个出现带有标签页的对话框,单击“高级”,查找“环境变量”...请参阅此处的两个屏幕截图...
Also I should point out that you are setting the environment variable temporarily, hence it will not see the 'COMSPEC'....it is not permanent, the only permanent way to do it is follow the screenshots...
另外我应该指出您是临时设置环境变量,因此它不会看到“COMSPEC”....它不是永久性的,唯一的永久性方法是按照屏幕截图...
I am trying to get the screenshots in place....
我正在尝试将屏幕截图放置到位....
alt text http://img706.imageshack.us/img706/9434/envvars.png
替代文字 http://img706.imageshack.us/img706/9434/envvars.png
alt text http://img638.imageshack.us/img638/5743/comspec.png
替代文字 http://img638.imageshack.us/img638/5743/comspec.png
Edit#2:Just to point out this, when you set the Environment variable here, that is temporary - not permanent!
编辑#2:只是指出这一点,当您在此处设置环境变量时,这是临时的 - 不是永久的!
if (! SetEnvironmentVariable("COMSPEC", "C:\WINDOWS\system32\cmd.exe") ) { printf("SetEnvironmentVariable failed (%d)\n", GetLastError()); } //r = system("dir c:\"); r = CreateProcess("dir.exe", NULL, NULL, NULL, TRUE, NULL, NULL, // inherit parent's environment NULL, &si, &pi);
When using the call CreateProcess
, it is bound to fail, look at the comment "inherit parent's environment", that cannot happen as the environment was set up temporarily. Have you tested the simple echo command here. Something is wrong as to why the COMSPEC variable is not set..after setting it permanently - you will need to reboot the machine for it to work. Then the echo command above should show the value for that environment variable, and in turn, this
使用调用的时候CreateProcess
,肯定会失败,看一下“继承父环境”的注释,这不会发生,因为环境是临时设置的。您是否在这里测试过简单的 echo 命令。关于为什么没有设置 COMSPEC 变量的原因有问题......在永久设置之后 - 您需要重新启动机器才能使其工作。然后上面的 echo 命令应该显示该环境变量的值,反过来,这个
argv[0] = strdup(_tgetenv(_T("COMSPEC")));
should return a proper pointer...by the way, I think that should be strdup
'd also...
应该返回一个正确的指针......顺便说一句,我认为这也应该strdup
......
Edit#3: Whoops I noticed when I had '&pi' used, it came up as a pi symbol instead!...duh, that's amended now...also I have amended this 'argv' code here:
编辑#3:哎呀,当我使用 '&pi' 时,我注意到它变成了一个 pi 符号!...呃,现在已经修改了...我也在这里修改了这个 'argv' 代码:
argv[0] = _tcsdup(_tgetenv(_T("COMSPEC")));
Hope this helps, Best regards, Tom.
希望这会有所帮助,最好的问候,汤姆。
回答by Hans Passant
Start + Control Panel, System, Advanced, Environment variables. Select Path in the System variables section, Edit. At the very least it should look like this:
开始 + 控制面板、系统、高级、环境变量。在系统变量部分中选择路径,编辑。至少它应该是这样的:
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem
Ask more questions about this at superuser.com
在 superuser.com 上提出更多关于此的问题