ShellExecute函数没有给出预期的结果。 [英] ShellExecute function is not give expected result.

查看:66
本文介绍了ShellExecute函数没有给出预期的结果。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码集。我想使用命令行设置环境变量的值。这就是我使用ShellExecute的原因。
函数执行成功。但它不会改变环境变量值。为什么?

Please consider the set of below code. I want to set the value of an environment variable using command line. That''s why i am used ShellExecute.
Function execute successfully. But it will not change the environment variable value. why?

CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
ShellExecute(0, _T( "open" ), _T( "cmd.exe" ), _T( "/C set ALLUSERSPROFILE=C:\Program" ), 0, SW_SHOWNORMAL);

推荐答案

我认为这不会像你尝试的那样工作,使用 set ,更改将不会全局可见。

尝试 setx.exe 命令,这将提供可供其他进程使用的环境变量的更改。

请参阅 http://stackoverflow.com/questions/3803581/setting-a-system-environment-variable-from-a-windows-batch-file [ ^ ]了解更多详情。



对于您的示例,这将是(此机器上的所有用户都可以看到环境变量):

I think this will not work the way you tried it, using set within the cmd.exe shell, the change will not be globally visible.
Try the setx.exe command, this will provide a change of the environment variable that is available to other processes.
See http://stackoverflow.com/questions/3803581/setting-a-system-environment-variable-from-a-windows-batch-file[^] for more details.

For your example this would be (the environment variable will be visible to all users on this machine):
ShellExecute(0, _T( "open" ), _T( "setx.exe" ), _T( "-m ALLUSERSPROFILE C:\Program" ), 0, SW_SHOWNA);


你可以使用
_putenv()

看这里http://msdn.microsoft.com/en-us/library/83zh4e6k%28v=vs.80 %29.aspx [ ^ ]


这篇关于ShellExecute函数没有给出预期的结果。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆