Got “The system cannot find the file specified” when I run NETSH from CreateProcess but it works ok on Command Prompt?

后端 未结 2 1682
灰色年华
灰色年华 2021-01-05 14:53

I have an NT service that calls a console program written in Delphi 7, let\'s call it failover.exe that in turn calls NETSH using a pr

相关标签:
2条回答
  • 2021-01-05 15:34

    Have you tried this?

    if not CreateProcess(PChar('C:\Windows\system32\netsh.exe'), PChar(Arguments), ...) then
    begin
      // Do somehting with `GetLastError`
    end;
    

    Of course it would be better to detect the path of C:\Windows\system32 at runtime as this could be on another driver or in another directory.

    When you run it this way you can get an error message from Windows using the GetLastError call right after CreateProcess.

    The ExecConsoleApp procedure is flawed, because it doesn't return the GetLastError or even any indication that CreateProcess failed.

    You should fix this first. Maybe add raise EExecConsoleAppCreateProcessFailed.Create(SysErrorMessage(GetLastError)) before Exit to the code.

    You shouldn't use cmd.exe /c as a prefix. It's redundant and it makes error diagnostics more difficult. GetLastError might not reflect the correct error code, because you're delegating the creation of the acutal netsh.exe process to cmd.

    0 讨论(0)
  • 2021-01-05 15:36

    The "cannot find the file specified" error may also occur if an implicitly loaded DLL required by the executable is not available. In this situation, that is the most likely cause - some essential DLL is not being found when netsh.exe is being run in a non-interactive context.

    Use Process Monitor (available for download from Microsoft's web site) to record the file system operations that are taking place during the attempt. Look for file not found errors either in the context of your service process or in the context of the netsh.exe process.

    0 讨论(0)
提交回复
热议问题