简体   繁体   English

如何从C:\\ Windows \\ System32文件夹执行Windows System()命令?

[英]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.

相关问题 使用 Fortran 和/或 C++ 将文件从 C:\Windows\System32 文件夹复制到 C:\Windows\SysWOW64 文件夹 - Copying a file from C:\Windows\System32 folder to C:\Windows\SysWOW64 folder using Fortran and/or C++ C++ 重命名文件@C:\\Windows\\System32\\Drivers - C++ Rename File @ C:\Windows\System32\Drivers 找不到可执行文件“C:\Windows\System32\Fodhelper.exe” - Executable “C:\Windows\System32\Fodhelper.exe” not found 如何允许64位窗口上的32位应用程序执行Windows \\ System32中提供的64位应用程序 - How to allow 32 bit apps on 64 bit windows to execute 64 bit apps provided in Windows\System32 重新打开 C:\\windows\\system32 中的文件时,ReOpenFile Windows API 失败并显示“错误无效名称” - ReOpenFile Windows API fails with "error invalid name" when reopening a file in C:\windows\system32 警告MSB3305:正在从路径“ C:\\ Windows \\ system32 \\ hnetcfg.dll”处理COM引用“ NETCONLib” - warning MSB3305: Processing COM reference “NETCONLib” from path “C:\Windows\system32\hnetcfg.dll” 使用 C++ 显示 C:\Windows\System32\config 的内容 - Show contents of C:\Windows\System32\config using C++ 找不到c:\\ windows \\ system32 \\ mfc100d.dll - Unable to find c:\windows\system32\mfc100d.dll 终端进程“C:\WINDOWS\System32\cmd.exe”以退出代码终止:1 - The terminal process "C:\WINDOWS\System32\cmd.exe" terminated with exit code: 1 错误模块路径:C:\\Windows\\system32\\KERNELBASE.dll - Faulting module path: C:\Windows\system32\KERNELBASE.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM