问题描述
我试图使用CreateProcess函数执行dos命令:
LPWSTR cmd =(LPWSTR)QString \\windows\\system32\\cmd.exe subst+ DLetter +\+ mountPath +\)。utf16();
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(& si,sizeof(si));
si.cb = sizeof(si);
ZeroMemory(& pi,sizeof(pi));
if(CreateProcessW(0,//应用程序名称
cmd,//应用程序参数
NULL,
NULL,
TRUE,
0,
NULL,
LC:\\windows\\ system32,//工作目录
& si,
& pi) TRUE)
{...
它提供最后一个错误3 = ERROR_PATH_NOT_FOUND,当I从命令中分离应用程序路径C:\\windows\\ system32 \\ cmd.exe
,它显示控制台而不执行my subst命令
任何帮助将不胜感激。
请在 cmd.exe
的选项中包含/ C或/ K。
/ C执行由字符串指定的命令,然后终止
/ K执行由字符串指定的命令,但保留
没有这些选项,您传递的 subst
命令将被忽略。 / p>
说到这里, subst
,至少在我的Windows 7上, > cmd.exe 。它是一个单独的可执行文件。因此,您可以直接调用它,并绕过 cmd.exe
。
> CreateProcess 我有以下注释:
- 不要包括路径
C :\\windows\\system32
。只需调用subst.exe
,并让系统使用标准搜索路径定位可执行文件即可。 - 通过
FALSE
bInheritHandles
。 - 传递
NULL
作为工作目录。这里没有必要指定。
I am trying to execute a dos command using CreateProcess function :
LPWSTR cmd=(LPWSTR)QString("C:\\windows\\system32\\cmd.exe subst " + DLetter+" \""+mountPath+"\"").utf16();
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if ( CreateProcessW(0, // Application name
cmd, // Application arguments
NULL,
NULL,
TRUE,
0,
NULL,
L"C:\\windows\\system32", // Working directory
&si,
&pi) == TRUE)
{ ...
it give as last error 3 = ERROR_PATH_NOT_FOUND, when I separate the application path "C:\\windows\\system32\\cmd.exe"
from the command it shows the console without executing my subst command.
Any help will be appreciated.
You need to include either /C or /K in the options to cmd.exe
.
/C Carries out the command specified by string and then terminates /K Carries out the command specified by string but remains
Without one these options, the subst
command that you pass is simply ignored.
Having said that, subst
, at least on my Windows 7 box, is not implemented inside cmd.exe
. It is a separate executable. So you can invoke it directly and bypass cmd.exe
completely.
Regarding your call to CreateProcess
I have the following comments:
- Don't include the path
C:\\windows\\system32
. Just invokesubst.exe
and let the system locate the executable using the standard search path. - Pass
FALSE
forbInheritHandles
. You aren't passing any handles to the new process and so you don't need the new process to inherit your handles. - Pass
NULL
as the working directory. There's just no need to specify it here.
这篇关于CreateProcess来执行Windows命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!