简体   繁体   English

ShellExecute:动词“runas”不适用于路径中有空格的批处理文件

[英]ShellExecute: Verb "runas" does not work for batch files with spaces in path

I am using ShellExecuteW to start a batch file.我正在使用 ShellExecuteW 启动批处理文件。 Code looks somewhat like this:代码看起来有点像这样:

ShellExecuteW(GetDesktopWindow(), wide_verb.c_str(), wide_filename.c_str(), wide_parameters.c_str(), NULL, SW_SHOW);

Where the wide_ variables are of type wstring .其中wide_变量的类型为wstring This code works fine for arbitrary combinations of file path and verb, except for verb "runas" (to get administrator rights) and a batch file with spaces in the given path wide_filename .此代码适用于文件路径和动词的任意组合,除了动词“runas”(以获得管理员权限)和给定路径中带有空格的批处理文件wide_filename The UAC prompt will pop up and, after confirmation, for a short moment the command prompt will flash up saying that the path could not be found, cutting off at the first space in the path.会弹出 UAC 提示,确认后,命令提示会闪一下,说找不到路径,在路径的第一个空格处切断。

I already tried wrapping the file name in additional quotes, but that didn't help.我已经尝试将文件名用附加引号括起来,但这没有帮助。

The reason is probably that .bat files aren't really "executables", but documents, and therefor原因可能是 .bat 文件并不是真正的“可执行文件”,而是文档,因此

ShellExecuteW(GetDesktopWindow(), L"runas", L"C:\\Path To\\file.bat", L"Parameters For Batch", NULL, SW_SHOW);

is internally mapped to an elevated execution of在内部映射到提升的执行

cmd.exe /C "C:\Path To\file.bat" "Parameters for Batch"

while it should be something along the lines of虽然它应该是类似的东西

cmd.exe /S /C " "C:\Path To\file.bat" Parameters for Batch "

And indeed, if I execute something like this:事实上,如果我执行这样的事情:

ShellExecuteW(GetDesktopWindow(), L"runas", L"cmd.exe", L"/S /C \" \"C:\\Path To\\file.bat\" Parameters For Batch \"", NULL, SW_SHOW);

it does the trick!它可以解决问题!

However, it doesn't seem like the cleanest approach to "hardcode" the executable (cmd.exe in this case) like that.但是,像这样对可执行文件(在本例中为 cmd.exe)进行“硬编码”的最干净方法似乎不是最干净的方法。 For batch files it really doesn't matter, but for other script-type documents that take parameters it might become relevant.对于批处理文件,这真的无关紧要,但对于其他带有参数的脚本类型文档,它可能变得相关。

So the question is: Is it possible to execute a batch file (with spaces in its path) elevated via ShellExecute or a similar mechanism, without explicitly passing it to cmd.exe?所以问题是:是否可以执行通过 ShellExecute 或类似机制提升的批处理文件(路径中有空格),而无需将其显式传递给 cmd.exe?

CreateProcessWithLogonW is what you need here. CreateProcessWithLogonW正是您所需要的。 It has a lot of arguments, but at the end of the page you'll see an Examples section instructing how to initialize the args.它有很多参数,但在页面末尾,您将看到一个示例部分,指导如何初始化参数。

As for hardcoding cmd.exe here's what CreateProcess official doc says (at the end of the section describing the lpApplicationName argument):至于硬编码cmd.exe这是CreateProcess 官方文档所说的(在描述lpApplicationName参数的部分的lpApplicationName ):

To run a batch file, you must start the command interpreter;要运行批处理文件,您必须启动命令解释器; set lpApplicationName to cmd.exe and set lpCommandLine to the following arguments: /c plus the name of the batch file.将 lpApplicationName 设置为 cmd.exe 并将 lpCommandLine 设置为以下参数:/c 加上批处理文件的名称。

So i think you're good.所以我觉得你很好。 Of course the path enclosing in " s still applies.当然,包含在"的路径仍然适用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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