system() and CreateProcess() / CreateProcessW()

前端 未结 3 1319
春和景丽
春和景丽 2020-12-21 04:56

I want to execute a TEST.exe in a C program. While I use

system( \"TEST.exe  output-file\" );

I can get what I expected.<

相关标签:
3条回答
  • 2020-12-21 05:44

    I do what CreateProcess and command line arguments tells me to do, and fix the problem! Thank you guys for your attention!

    For your convenience, here is the quotation of the answer:

    You cannot use command-line redirection operators with CreateProcess() directly. You have to spawn an instance of cmd.exe and pass the operators to it instead, eg:

    CreateProcess( "C:\\windows\\system32\\cmd.exe", t_str2, ...)) 
    

    Where t_str2 is "/C C:\Temp\sift.exe < C:\img1.pgm > C:\img1.key". The actual path to cmd.exe can be determined by reading the %COMSPEC% environment variable.

    0 讨论(0)
  • 2020-12-21 05:47

    WaitForSingleObject() returns a wait result, and not the exit code. https://msdn.microsoft.com/en-us/library/windows/desktop/ms687032(v=vs.85).aspx

    258 is a WAIT_TIMEOUT. You should retry on this error code until you get the return value of 0 (WAIT_OBJECT_0), or some other error.

    After this, use GetExitCodeProcess https://msdn.microsoft.com/en-us/library/windows/desktop/ms683189(v=vs.85).aspx to get the exit code of the process.

    0 讨论(0)
  • 2020-12-21 05:54

    system actually spawns a cmd instance in which your command is run:

    The system function passes command to the command interpreter, which executes the string as an operating-system command. system refers to the COMSPEC and PATH environment variables that locate the command-interpreter file (the file named CMD.EXE in Windows NT). If command is NULL, the function simply checks to see whether the command interpreter exists.
    —Documentation of system

    This is why redirection operators such as < and > work. This is not the case for CreateProcess which really just spawns a process instead of a shell that executes another process. Since the redirection operators are a feature of the shell and not the OS you'd have to do input and output to the process manually.

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