views:

2757

answers:

7

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

Before I start down this path I am curious how much work I should expect to be in for? Are we talking a major undertaking, or is there a straight forward solution? I know some C, but am by no means an expert.

I am not expecting SoX specific details, just EXE console application conversion to DLL in general. If someone is familiar with SoX though, even better.

+4  A: 

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

Adam Pierce
The calling program is written in Delphi. Delphi doesn't have a popen() function.
Barry Kelly
Delphi can talk to the Windows API, though, which can do something similar.
Thomas
+5  A: 

In Windows, you just call CreateProcess with the SoX command line. I don't know the Delphi bindings for Win32, but I've done this exact thing in both Win32 and C#.

And now that you know CreateProcess is what you want to call, a google search on how to do that from Delphi should give you all the code you need.

Delphi Corner Article - Using CreateProcess to Execute Programs

Calling CreateProcess() the easy way

indiv
+3  A: 

Disclaimer: I know nothing about SoX. It might be that the code is structured to make this easy, or it might be more hard. Either way, the process is the same:

First you want to find the functions in the SoX application that you want to call. Most likely the console app has code to parse the command line and call the appropriate functions. So first off, find the functions you want to use.

Next, check out the info on exporting functions in DLLs from C at this site: Creating And Using DLLs

Then make a new makefile or visual studio project file with the target being a DLL, and add the sourcefiles from the SoX program that you have modified to be exported.

kevin42
+6  A: 

For the specific topic of turning a console executable into a library by modifying the C source code, it depends on how the command-line application is factored. If it's written in such a way that I/O is funneled through a small set of functions or even better function pointers, then obviously it will be trivial.

If it's all done with printf, scanf and friends, then you'll probably be best off by finding / creating an include file that all the source files include and adding a macro that redirects printf/scanf and friends to your own functions that are written so as to be amenable to DLL implementation. Things like printf can be built from vsnprintf (use the n-version for safety), so you don't need to reimplement the whole C RTL I/O subsystem. However, there is no vsscanf, but there are third-party implementations on the web.

If the code is using 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, a portable text file driver cannot be implemented. It might still be possible if you spelunk in your specific C RTL, but you'd be better advised going down the macro route and reimplementing your own renamed FILE type.

Finally, the "popen()" approach is possible in Delphi and made somewhat easier in Delphi 2009 with the TTextReader and TTextWriter classes. Combine these with 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 don't feel like writing your own, there are third-party equivalents / samples on the web for Delphi too. Here's one.

Barry Kelly
+2  A: 

Run the process, the way Indiv advised and capture the output like how Adam has shown.

However if you still want to do the DLL conversion, this will get you started

  1. Configure SOX for windows and compile it
  2. Create an empty DLL project using your C++ tool
  3. Add the SOX files to be part of the project
  4. Add a new Function called DLLMain

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

  5. Add a .DEF file (use the project name as the file name) that lists the exports in the DLL - Add the following content to it

    LIBRARY      "name.DLL"
    

    EXPORTS

    CallOldMain PRIVATE

  6. Rename the main of SOX as CallOldMain

  7. Write a CUSTOM function to log the output / return error etc.

  8. Find all printfs / cout in the SOX application and replace it with calls to your custom function above

  9. Once the DLL is compiled you can now call the function CallOldMain with the same parameters main programs of C expects. You could modify this signature to return the errors / output from above.

+1  A: 

You don't mention what your toolchain is, but if you configure gcc in Windows, you can use the normal config;make;make install to just compile sox. In the process, it will create a dll file, and the console app. Or, you can just specify the make target to only make the dll. This will compile a windows native library that only depends on the MS C runtime dll, and you can use this in your own app.

ramanman
+1  A: 

You can execute a console application and capture its output using pipes. You use une side of the pipe as stdout for the CreateProcess and you read from the other side like a common file.

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

Giacomo Degli Esposti