C++ 如何使用 CreateProcess 执行简单的命令行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12921680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute a simple command line with CreateProcess?
提问by DragonGamer
I'd like to execute a simple command line but without to make a window appear. Therefore I can't use System and have to use CreateProcess as far as I know. So I have the following code for example:
我想执行一个简单的命令行,但不显示窗口。因此,据我所知,我不能使用 System 并且必须使用 CreateProcess 。例如,我有以下代码:
//.../
CreateProcess(NULL,input,NULL,NULL,false,NORMAL_PRIORITY_CLASS |
CREATE_NO_WINDOW,NULL,NULL,&startInf,&procInf);//)
//.../
If input is a line like "ping www.google.com -n 2"it seems to work. What I need is the deletion-function though. Therefore I tried a lot of variations like:
如果输入是像“ping www.google.com -n 2”这样的行,它似乎可以工作。我需要的是删除功能。因此,我尝试了很多变体,例如:
input = "rd /S /Q \"D:\ALEX_DATEN\PC\C++\bla\"";
and
和
input = "rd /S /Q \"D:/DATEN/PC/C++/bla\"";
But nothing happens and the function returns failure :/ If I write it as a .bat file (without using the "\" escaping chars) the deleting works perfectly!
但是没有任何反应,函数返回失败:/如果我将它写为 .bat 文件(不使用“\”转义字符),删除工作完美!
Does anyone know what I'm doing wrong?
有谁知道我做错了什么?
P.s. no, I'm not writing a destructive virus.. if that would have been my target, I would have definitely have found simpler ways...
Ps 不,我不是在写破坏性的病毒......如果那是我的目标,我肯定会找到更简单的方法......
采纳答案by BigBoss
Some system command like rd
, del
and ... are not actual executable Images (e.g. .exe Files), so you can't execute/run them using CreateProcess
they are built-in commands that known to cmd
(command interpreter of windows) so you should create cmd
and pass your command to it:
一些系统命令,例如rd
,del
和 ... 不是实际的可执行映像(例如 .exe 文件),因此您无法使用CreateProcess
它们来执行/运行它们是已知的内置命令cmd
(Windows 的命令解释器),因此您应该创建cmd
并将您的命令传递给它:
wchar_t cmd[ MAX_PATH ];
size_t nSize = _countof(cmd);
_wgetenv_s( &nSize, cmd, L"COMSPEC" );
BOOL b = CreateProcessW( cmd, input, NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, NULL, &startInf, &procInf );
Note: Please see arguments of cmd
, you have to use /C
to pass your command. So your command is as follow:
注意:请参阅 的参数cmd
,您必须使用它/C
来传递您的命令。所以你的命令如下:
wchar_t input[] = L"some command";
wchar_t cmd[MAX_PATH] ;
// initialize cmd
wchar_t cmdline[ MAX_PATH + 50 ];
swprintf_s( cmdline, L"%s /c %s", cmd, input );
STARTUPINFOW startInf;
memset( &startInf, 0, sizeof startInf );
startInf.cb = sizeof(startInf);
// If you want to redirect result of command, set startInf.hStdOutput to a file
// or pipe handle that you can read it, otherwise we are done!
PROCESS_INFORMATION procInf;
memset( &procInf, 0, sizeof procInf );
BOOL b = CreateProcessW( NULL, cmdline, NULL, NULL, FALSE,
NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, NULL, &startInf, &procInf );
DWORD dwErr = 0;
if( b ) {
// Wait till cmd do its job
WaitForSingleObject( procInf.hProcess, INFINITE );
// Check whether our command succeeded?
GetExitCodeProcess( procInfo.hProcess, &dwErr );
// Avoid memory leak by closing process handle
CloseHandle( procInfo.hProcess );
} else {
dwErr = GetLastError();
}
if( dwErr ) {
// deal with error here
}
回答by Remy Lebeau
As others have stated, rd
cannot be executed with CreateProcess()
directly, you have to execute cmd.exe
with /C rd ...
as its command-line parameter. Rather than using CreateProcess()
like this, you should use SHFileOperation()
instead:
正如其他人所说,rd
不能直接执行CreateProcess()
,您必须执行cmd.exe
with/C rd ...
作为其命令行参数。而不是CreateProcess()
像这样使用,你应该使用SHFileOperation()
:
SHFILEOPSTRUCT FileOp = {0};
FilOp.wFunc = FO_DELETE;
FileOp.pFrom = "D:\ALEX_DATEN\PC\C++\bla##代码##";
FileOp.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI.
int ErrorCode = SHFileOperation(&FileOp);
if (ErrorCode == 0)
{
if (FileOp.fAnyOperationsAborted)
// not everything was deleted
else
// delete was successful
}
else
{
// delete failed
// note that ErrorCode might not be a Win32 error code,
// so check the SHFileOperation() documentation for
// possible alternatives
}