rd
、および...などの一部のシステムコマンドは、実際の実行可能イメージ(.exeファイルなど)ではないため、 (Windowsのコマンドインタープリター)に認識されている組み込みコマンドをdel
使用して実行/実行できないため、作成する必要がありますコマンドをそれに渡します:CreateProcess
cmd
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 );
注: の引数を参照してください。コマンドを渡す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
}