[英]why Commands are not executed with CreateProcess
i hev problem with CreatProcess function introduced in windows.h header for c++. 我在c。的windows.h标头中引入了CreatProcess函数的问题。 whenever i try to pass it a TCHAR variable containing a cmd command it returns error : CreateProcess failed (2) .
每当我尝试将包含cmd命令的TCHAR变量传递给它时,都会返回错误:CreateProcess failed(2)。 and for this am waiting for your explanations and solutions.
为此,我们正在等待您的解释和解决方案。
consider code below: 考虑下面的代码:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return 0;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return 0;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
} }
notice:when i launch an application with specifying its path ..it works fine like=> "c:\\code.exe"; 注意:当我启动一个具有指定路径的应用程序时,它可以正常工作,例如=>“ c:\\ code.exe”;
If you want to run a command DOS
, you have to run the shell cmd
before. 如果要运行
DOS
命令,则必须先运行shell cmd
。
CreateProcess
doesn't do that for you. CreateProcess
不会为您做到这一点。
The option /c
of cmd
permits to run a command in the shell and terminate. cmd
的选项/c
允许在shell中运行命令并终止。 You just have to build a command line of the type cmd /c <your command here>
. 您只需要构建
cmd /c <your command here>
类型的命令行。
I compiled your code on VS2012 and I tried : test.exe "cmd /c dir"
and it works like a charm. 我在VS2012上编译了您的代码,并尝试了:
test.exe "cmd /c dir"
,它的工作原理很像。
From the Microsoft documentation : 从Microsoft文档:
To run a batch file (or a batch command), 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加上批处理文件的名称。
Source : http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx 来源: http : //msdn.microsoft.com/zh-cn/library/windows/desktop/ms682425(v=vs.85).aspx
In order to execute a command implemented by the command shell aka cmd.exe
, you need to actually run cmd.exe. 为了执行由命令外壳aka
cmd.exe
实现的命令,您需要实际运行cmd.exe。 CreateProcess
doesn't automatically do that for you. CreateProcess
不会自动为您执行此操作。
Build a command line of the form cmd.exe /c <your command here>
. 构建一个格式为
cmd.exe /c <your command here>
的命令行。 /c
means "run one command, then terminate". /c
表示“运行一个命令,然后终止”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.