简体   繁体   中英

How to execute Windows System() command from C:\Windows\System32 folder?

I have a batch file "install.bat" stored in location "

c:\\Users\\abc\\xyz

". I want to execute this batch file in administrative mode from

C:\\Windows\\System32

folder using a System() API. Can anyone kindly lemme know how do I achieve this VC++ programatically.

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. But I want to run this batch file from System32 folder?

Thanks in advance.

One option is to put a cd command as the first line of your .bat file. 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 . 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). You can specify the working directory for the new process as one of the parameters to CreateProcess .

CreateProcess is rather hard to call though. And it won't help you with requesting elevation to admin rights. Instead you can use ShellExecute . Call that passing "runas" for the verb, which will result in elevation.

ShellExecute(0, "runas", "c:\\Users\\abc\\xyz\\install.bat", NULL, 
    "C:\\Windows\\System32", SW_SHOW);

You need to use ShellExecute .

Something like the following

ShellExecute(hwnd, "runas", "c:\\Users\\abc\\xyz\\install.bat", NULL, "c:\\windows\\system", SW_SHOWNORMAL );

在不使用System()的情况下,您只需将cd定位到包含.bat文件的位置,然后仅调用批处理文件即可

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM