【问题标题】:CreateProcess() function. GetLastError() returns 3 [closed]CreateProcess() 函数。 GetLastError() 返回 3 [关闭]
【发布时间】:2012-12-20 13:46:22
【问题描述】:

我从下面的程序中得到以下错误:CreateProcess failed(3)

int __cdecl main(int argc, char **argv)
{
    USES_CONVERSION;
    string name_of_bitmap;
    cout << "Name of file: ";
    cin >> name_of_bitmap;
    string arguments = "F:\\windowsqnx\\maps\\show_simulation\\Debug\\show_simulation.exe " + name_of_bitmap;
    const char * nob;
    nob = arguments.c_str();
    std::wstring stemp = s2ws("F:\\windowsqnx\\maps\\show_simulation\\Debug\\show_simulation.exe");
    LPCWSTR path = stemp.c_str();
    // runing simulation display process
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

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

    // Start the child process. 
    if ( !CreateProcess(path,
        A2W( nob ) ,
        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() );
        Sleep(2000);
        return 1;
    }
}

我是流程新手,不知道我做错了什么。我阅读了this 并执行了以下string arguments = "\"F:\\windowsqnx\\maps\\show_simulation\\Debug\\show_simulation.exe\" " + name_of_bitmap;std::wstring stemp = s2ws("\"F:\\windowsqnx\\maps\\show_simulation\\Debug\\show_simulation.exe\" ");,然后如果我将第一个参数CreateProcess(NULL, 设为NULL,则会出现错误123 我得到了失败2。请帮助。

编辑

std::wstring s2ws(const std::string& s)
{
   int len;
   int slength = (int)s.length() + 1;
   len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0); 
   wchar_t* buf = new wchar_t[len];
   MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
   std::wstring r(buf);
   delete[] buf;
   return r;
}

【问题讨论】:

  • 请使用正确的代码缩进。
  • 通过检查您传递给CreateProcess 的参数来调试它。 s2ws 的使用在这里是相当不必要的。只需使用path = L"..."。留下A2W。我们猜不出那是什么。

标签: c++ winapi


【解决方案1】:

根据Windows error code documentation,错误2 表示ERROR_FILE_NOT_FOUND,错误3 表示ERROR_PATH_NOT_FOUND,两者都可能意味着exe 不在您告诉Windows 的位置。

【讨论】:

    【解决方案2】:

    错误:“...\show_simulation.exe\”

    好:“...\show_simulation.exe”

    尝试从命令行输入“show_simulation.exe\” - 你会明白我的意思 :)

    看起来 s2ws() 可能是添加不需要的斜杠的罪魁祸首。

    【讨论】:

    • 我按照你告诉我的"F:\\windowsqnx\\maps\\show_simulation\\Debug\\show_simulation.exe" + name_of_bitmap; 做了,但我仍然遇到同样的错误。
    【解决方案3】:

    错误2是

    ERROR_FILE_NOT_FOUND
    2 (0x2)
    The system cannot find the file specified.
    

    您指定的路径用于一个不存在的文件,或者 s2ws 正在对您的字符串做一些奇怪的事情。我们可以看到 s2ws 吗?

    【讨论】:

    • 从文件属性复制的路径如下所示:F:\windowsqnx\show_simulation\Debug。我将函数添加到上面的问题中。
    • F:\windowsqnx\show_simulation\Debug 还是 F:\windowsqnx\maps\show_simulation\Debug?您确定该文件存在并且您的进程有权读取该目录吗?尝试测试文件是否存在。这是一个示例:stackoverflow.com/questions/3828835/…
    • 该死!对不起。我是白痴,应该是"\"F:\\windowsqnx\\show_simulation\\Debug\\show_simulation.exe\" " + name_of_bitmap;
    • 很高兴你找到了它,你不是白痴。我们都做过类似的事情。
    猜你喜欢
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-11-25
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多