使用CreateProcess可以运行所有的命令,在使用del、dir命令的时候,出现Error=2,即ERROR_PATH_NOT_FOUND;
调用命令的函数如文章最后,调用方式如下:
RunCmd(“del \q *.raw”);
出现:GetLastError() == 2;
在使用DOS命令的时候,使用方式必须如下:
*RunCmd("cmd.exe /c del \q .raw");
表示在dos下运行,/c运行完毕关闭窗口。
使用CreateProcess启动命令的函数如下:
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
void RunCmd( TCHAR *cmd )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
cmd, // 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;
}
// Wait until child process exits.
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}