C中的CreateProcess失败

时间:2013-11-16 17:53:57

标签: c process createprocess

我正在尝试使用以下代码创建流程:

void GenerateCommandLine( char *commandLine, 
                          int maxLength, 
                          const char *imageTitle) {
    const char * COMMAND_LINE_TEMPLATE = "AnalyzeImage.exe --image_file %s";
    sprintf_s(  commandLine, 
                maxLength, 
                COMMAND_LINE_TEMPLATE, 
                imageTitle 
    );
}

int AnalyzeCurrentImage(char* imageTitle) {
    STARTUPINFO         startupInfo;
    PROCESS_INFORMATION processInfo;
    DWORD               exitCode = MAXINT;
    char                commandLine[ MAX_ELEMENT_LENGTH ];

    commandLine[ 0 ] = '\0';
    InitVariables( &startupInfo, &processInfo );
    GenerateCommandLine( commandLine, MAX_ELEMENT_LENGTH, imageTitle );
    printf( "-----------command line is: %s\n", commandLine );


    // Start the AnalyzeImage process. 
    if( !CreateProcess( NULL,       // No module name (use command line)
        commandLine,                // 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 
        &startupInfo,   // Pointer to STARTUPINFO structure
        &processInfo )  // Pointer to PROCESS_INFORMATION structure
    ) 
    {
        printf( "CreateProcess failed (%d).\n", GetLastError() );
        return;
    }
    printf("here!!!");
    // Wait until AnalyzeImage process exits.
    WaitForSingleObject( processInfo.hProcess, INFINITE );

    GetExitCodeProcess( processInfo.hProcess, &exitCode );
    printf( "------------ Exit Code: %d\n", exitCode );

    // Close process and thread handles. 
    CloseHandle( processInfo.hProcess );
    CloseHandle( processInfo.hThread );
}

当我从命令行运行此程序时,我得到以下内容:

  

“-------命令行是:AnalyzwImage.exe --imgage_file img000.jpg   CreateProess失败< 2>“

您是否知道创建过程失败的原因?

谢谢

2 个答案:

答案 0 :(得分:0)

错误代码2为File Not Found,因此CreateProcess无法找到AnalzwImage.exe。它不在当前目录中或系统路径中。尝试使用AnalzwImage.exe的完整路径。

答案 1 :(得分:0)

GetLastError()返回的代码2表示 ERROR_FILE_NOT_FOUND

简单地说,找不到 AnalyzwImage.exe 。 您应该在命令行中包含可执行文件的绝对路径,或者确保调用 CreateProcess 时的当前工作目录是AnalyzwImage.exe所在的目录。