简体   繁体   English

CreateProcess无法运行批处理文件,错误代码为2

[英]CreateProcess fails to run batch file with error code 2

I am creating a child process which executes a batch file 我正在创建一个执行批处理文件的子进程

success = CodebenderccAPI::CreateProcess(
    NULL,
    (LPWSTR)command.c_str(),     // command line
    NULL,                        // process security attributes
    NULL,                        // primary thread security attributes
    TRUE,                        // Inherit pipe handles from parent process
    CREATE_NEW_CONSOLE,          // creation flags
    NULL,                        // use parent's environment
    current_dir,                 // use the plugin's directory
    &si,                         // __in, STARTUPINFO pointer
    &pi);                        // __out, receives PROCESS_INFORMATION

The above code was tested and works as it should in various machines but recently failed in a Windows 7 machine and I am still trying to figure out why... 上面的代码已经过测试,可以在各种机器上正常工作,但是最近在Windows 7机器上失败了,我仍然试图找出原因...

I have ensured that the user has administrative permissions to the folder where the batch file is installed as well as that the batch file is in the working directory. 我确保用户对安装了批处理文件的文件夹具有管理权限,并且该批处理文件位于工作目录中。 Moreover I disabled the antivirus (to verify that it didn't cause the problem) and tried again with no sucess. 此外,我禁用了防病毒软件(以验证它不会引起问题),然后再次尝试,但没有成功。 CreateProcess always fails with error code 2: ERROR_FILE_NOT_FOUND. CreateProcess总是失败,错误代码2:ERROR_FILE_NOT_FOUND。

Any ideas what could cause that failure? 有什么想法会导致该故障吗?

Finally I managed to find out what was causing CreateProcess failure. 最终,我设法找出导致CreateProcess失败的原因。 Opened cmd and cd to the folder where batch file was located, then ran the batch file without any problem. 打开cmd和cd到批处理文件所在的文件夹,然后运行该批处理文件而没有任何问题。 After this, I navigated to the folder through file system and double clicked the batch file to run it. 之后,我通过文件系统导航到该文件夹​​,然后双击批处理文件以运行它。 It failed with error message "Windows cannot find the_path_to the batch_file . Make sure you have typed the name correctly and try again." 它失败,并显示错误消息“ Windows无法找到the_path_to batch_file 。请确保您正确输入了名称,然后重试。” According to this post the above error is related with the COMSPEC entry. 根据这篇文章 ,以上错误与COMSPEC条目有关。 Checked its value in registry and found that it was different from the default value . 在注册表中检查了它的值,发现它与默认值不同。 Updated its value to the default and problem was solved! 将其值更新为默认值,问题已解决!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM