[英]How to execute Windows System() command from C:\Windows\System32 folder?
I have a batch file "install.bat" stored in location " 我有一个批处理文件“ install.bat”存储在位置“
c:\\Users\\abc\\xyz
c:\\ Users \\ abc \\ xyz
". I want to execute this batch file in administrative mode from “。我想以管理模式从以下位置执行此批处理文件
C:\\Windows\\System32
C:\\ Windows \\ System32
folder using a System() API. 使用System()API的文件夹。 Can anyone kindly lemme know how do I achieve this VC++ programatically.
任何人都可以请我知道如何以编程方式实现此VC ++。
My code snippet:: 我的代码段:
int ret = System("c:\Users\abc\xyz\install.bat");
If I give this command, batch file is of course executing from "c:\\Users\\abc\\xyz" folder. 如果我给出此命令,则批处理文件当然是从“ c:\\ Users \\ abc \\ xyz”文件夹执行的。 But I want to run this batch file from System32 folder?
但是我想从System32文件夹运行此批处理文件吗?
Thanks in advance. 提前致谢。
One option is to put a cd
command as the first line of your .bat file. 一种选择是将
cd
命令作为.bat文件的第一行。 You could change the working directory of the calling process, but that's using a hammer to crack a nut. 您可以更改调用过程的工作目录,但这是使用锤子敲碎螺母。
If you move away from the system
function you can call CreateProcess
. 如果您离开
system
功能,则可以调用CreateProcess
。 That allows you to specify all the gory details you need when creating a new process. 这样一来,您就可以指定创建新流程时所需的所有细节。 You need to run the command interpreter (find that by reading the
COMSPEC
environment variable). 您需要运行命令解释器(通过读取
COMSPEC
环境变量来查找该解释器)。 You can specify the working directory for the new process as one of the parameters to CreateProcess
. 您可以将新流程的工作目录指定为
CreateProcess
的参数之一。
CreateProcess
is rather hard to call though. CreateProcess
很难调用。 And it won't help you with requesting elevation to admin rights. 而且它对您提升管理员权限没有帮助。 Instead you can use
ShellExecute
. 相反,您可以使用
ShellExecute
。 Call that passing "runas"
for the verb, which will result in elevation. 将该动词称为传递的
"runas"
,这将导致提升。
ShellExecute(0, "runas", "c:\\Users\\abc\\xyz\\install.bat", NULL,
"C:\\Windows\\System32", SW_SHOW);
You need to use ShellExecute . 您需要使用ShellExecute 。
Something like the following 类似于以下内容
ShellExecute(hwnd, "runas", "c:\\Users\\abc\\xyz\\install.bat", NULL, "c:\\windows\\system", SW_SHOWNORMAL );
在不使用System()的情况下,您只需将cd
定位到包含.bat文件的位置,然后仅调用批处理文件即可
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.