CreateProcess时ERROR_FILE_NOT_FOUND

时间:2016-11-24 12:09:24

标签: winapi visual-c++ createprocess

尝试使用CreateProcess

运行记事本
void _tmain( int argc, TCHAR *argv[] )
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );



    // Start the child process. 
    if( !CreateProcess( "notepad.exe",   // No module name (use command line)
        NULL,        // 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 );
}

输出2出错。这意味着ERROR_FILE_NOT_FOUND。我应该从哪里开始寻找问题?这是否意味着找不到notepad.exe

1 个答案:

答案 0 :(得分:4)

lpApplicationName参数必须提供应用程序的完整路径,而不仅仅是其名称。

更确切地说:

  

该字符串可以指定模块的完整路径和文件名   执行或它可以指定部分名称。在部分的情况下   name,该函数使用当前驱动器和当前目录   完成规范。功能不会使用搜索路径。   此参数必须包含文件扩展名;没有默认值   假定延期。

但是你应该不在notepad.exe的当前文件夹中。因此,如果您只想使用名称,请在第二个参数lpCommandLine中设置它。请注意,此字符串不是常量,因为它以这种方式定义为LPTSTR参数:

  

BOOL WINAPI CreateProcess(
    _In_opt_ LPCTSTR lpApplicationName,
    _Inout_opt_ LPTSTR lpCommandLine,[...]

所以你有两个变种:

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;

WCHAR CommandLine[] = L"notepad.exe";
if( CreateProcess( 0,   // 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 
    &si,            // Pointer to STARTUPINFO structure
    &pi )           // Pointer to PROCESS_INFORMATION structure
    ) 
{
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}

STARTUPINFO si = { sizeof(si) };
PROCESS_INFORMATION pi;

if( CreateProcess( L"c:\\windows\\notepad.exe",  
    0,        // 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
    ) 
{
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
}