1

C ++のwindows.hヘッダーに導入されたCreatProcess関数に問題があります。cmdコマンドを含むTCHAR変数を渡そうとすると、エラーが返されます: CreateProcess failed (2) 。これについては、あなたの説明と解決策を待っています。

以下のコードを検討してください。

#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 );

}

注意:パスを指定してアプリケーションを起動すると、=> "c:\code.exe"; のように正常に動作します。

4

2 に答える 2

0

コマンド シェル (別名 ) によって実装されたコマンドを実行するにはcmd.exe、実際に cmd.exe を実行する必要があります。CreateProcess自動的にそれを行うわけではありません。

形式のコマンド ラインを作成しcmd.exe /c <your command here>ます。/c「1つのコマンドを実行してから終了する」ことを意味します。

于 2013-07-22T04:20:17.213 に答える