0

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...

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.

Any ideas what could cause that failure?

KoKa
  • 797
  • 1
  • 14
  • 31
  • I don't think this is your problem, but for future reference you shouldn't pass a c_str() to CreateProcess. You're not supposed to write into the buffer returned by c_str(), and CreateProcess does. – Harry Johnston Aug 14 '14 at 23:27
  • I agree, it doesn't seem to be a code error, but I can't think of any other possible cause of ERROR_FILE_NOT_FOUND failure. – KoKa Aug 18 '14 at 06:59
  • Does the same command work if entered at the command line? Does prefixing the command with `cmd /c ` before calling CreateProcess help? Oh, and have you doublechecked that `command` is a wide string? (If it were a narrow string, the cast would hide the type mismatch.) – Harry Johnston Aug 18 '14 at 09:26
  • Command works if entered at the command line and command variable is of const std::wstring & type. :( – KoKa Aug 18 '14 at 13:34
  • Have you tried putting `cmd /c` at the start of the command? What is the exact value of the string? – Harry Johnston Aug 18 '14 at 21:13
  • Didn't tried putting cmd /c at the start of the command cause the code is compiled and the dll file is packaged in an xpi file, so I need to figure out why in this specific machine code fails while working on others. – KoKa Aug 20 '14 at 15:44
  • Moreover tried to run the batch file generated and also works fine... Could it be a Windows configuration that causes the failure? – KoKa Aug 20 '14 at 15:47
  • We don't have enough information to tell. Could be a 32/64 bit thing I suppose. I recommend you use Process Monitor (available from the MS website) to see what error (if any) is actually being returned from the file system. – Harry Johnston Aug 20 '14 at 21:30
  • I have used Process Monitor but didn't received any error related to CreateProcess, Operation Process Created didn't appear at all. Thanks for the idea! – KoKa Aug 26 '14 at 10:46
  • Not what I meant; the system is reporting that it was unable to find the file, so look for file not found errors. – Harry Johnston Aug 26 '14 at 10:57

1 Answers1

0

Finally I managed to find out what was causing CreateProcess failure. Opened cmd and cd to the folder where batch file was located, then ran the batch file without any problem. 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." According to this post the above error is related with the COMSPEC entry. 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!

KoKa
  • 797
  • 1
  • 14
  • 31