Convert exe console to dll in C

I am interested in invoking SoX , an open source console application, from another Windows GUI program (naturally written in Delphi). Instead of parsing and hiding the console window, I would just like to convert the application to a DLL that I can call from my application.

Before embarking on this journey, I am curious how much work I should expect? Are we talking about a major undertaking, or is there a direct solution? I know a little C, but I'm by no means an expert.

I do not expect much information about SoX, just converting EXE applications to DLLs in general. If someone is familiar with SoX, but even better.

+4
source share
7 answers

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.

+6
source

On Windows, you simply call CreateProcess using the SoX command line. I do not know the Delphi bindings for Win32, but I did it exactly in both Win32 and C #.

And now that you know that CreateProcess is what you want to call, a Google search on how to do it from Delphi should provide you with all the necessary code.

Delphi Corner Article - Using CreateProcess to Run Programs

Calling CreateProcess () is an easy way.

+5
source

You may not even need a DLL, you can use the popen () function to launch a console application and collect any output text.

+4
source

Disclaimer: I do not know anything about SoX. Perhaps the code is structured to make it easy, or it can be more complicated. In any case, the process is the same:

First you want to find the features in the SoX application that you want to call. Most likely, the console application has code for parsing the command line and calling the corresponding functions. So first find the features you want to use.

Then, read about exporting functions to DLLs with C on this site: Creating and Using DLLs

Then create a new makefile or visual studio project file with the target being a DLL, and add the source files from the SoX program that you modified for export.

+3
source

Start the process, as Indiv advised and captured the conclusion, as Adam showed.

However, if you still want to perform the conversion to a DLL, this will start

  • Configure SOX for Windows and compile it
  • Create an empty DLL project using the C ++ tool
  • Add SOX files to the project
  • Add New DLLMain Function

    BOOL APIENTRY DllMain (HANDLE hModule, DWORD ul_reason_for_call, LPVOID) {return TRUE;}

  • Add a .DEF file (use the project name as the file name), which indicates the export to the DLL. Add the following content to it.

    LIBRARY "name.DLL" 

    Export

    CallOldMain PRIVATE

  • Rename the main SOX as CallOldMain

  • Write a CUSTOM function to register an output / return error, etc.

  • Find all printfs / cout in a SOX application and replace it with your custom function calls above

  • After compiling the DLL, you can now call the CallOldMain function with the same parameters as the main C programs. You can change this signature to return errors / output from above.

+3
source

You do not specify what your toolchain is, but if you configure gcc on Windows, you can use the usual configuration; make; make install to just compile sox. In the process, he will create a dll file and a console application. Or you can just specify the make target only to create the dll. This compiles the native Windows library, which depends only on the MS C runtime dll, and you can use it in your own application.

+1
source

You can run the console application and execute its output using pipes. You use une side pipe as stdout for CreateProcess, and you read on the other hand, like a regular file.

Here you can see a working example written in delphi: http://delphi.about.com/cs/adptips2001/a/bltip0201_2.htm

+1
source

Source: https://habr.com/ru/post/1277144/


All Articles