For a specific topic, turning the console executable into a library by modifying the C source code depends on how the command line application is processed. If it is written in such a way that input / output is routed through a small set of functions or even more efficient function pointers, then obviously this will be trivial.
If everything is done with printf, scanf and friends, then it is probably best for you to find / create an include file that includes all the source files and add a macro that redirects printf / scanf and friends to your own functions that are written like this to be able to implement a DLL. Things like printf can be built from vsnprintf (use the n version for security), so you don't need to redefine the entire C RTL I / O subsystem. However, there is no vsscanf, but there are third-party implementations on the Internet.
If the code uses fprintf, fscanf etc. to enable indirection between files and the console, you're still out of luck. The FILE structure is opaque, and unlike Pascal text files, the portable text file driver cannot be implemented. Perhaps this will be possible if you master your specific C RTL, but you are advised to change the macro route and override your own renamed FILE type.
Finally, the "popen ()" approach is possible in Delphi and has been somewhat simplified in Delphi 2009 with the TTextReader and TTextWriter classes. Combine them with a TFileStream wrapped around pipes, and specify pipes for standard input, standard output and standard error in the new process and STARTF_USESTDHANDLES etc., and it will work. If you do not want to write your own, Delphi also has third-party equivalents / samples on the Internet. Here is one.
source share