0

Is there a way to combine console tools and pack into a managed .net DLL . Then I would be able to call the functions of each console tool by calling functions from the DLL.

Ex. (just examples)

trim.exe
 Usage:trim.exe <input> <ouput>

copy.exe
 Usage:copy.exe <input> <ouput>

Then I would be able to call them like a function like so

Utilities.Trim("input.txt","ouptut.txt");
Utilities.Copy("input.txt","ouptut.txt");

I don't have access to the source code of these console tools unfortunately.

DmitryG
  • 17,677
  • 1
  • 30
  • 53
bman
  • 3,740
  • 9
  • 34
  • 40
  • No, you can't put stand-alone executables inside a DLL and then call them there. Why can't you just write the equivalent functions yourself and put them in a utility class? There doesn't appear to be anything complex about the two examples you provided. – Ken White Sep 05 '13 at 15:14
  • Thanks @ken-white, there is nothing complex because they are only examples. I am actually dealing with complex console utilities. – bman Sep 05 '13 at 15:17

2 Answers2

1

Yes, You are looking for running executable from Embedded Resource.

This Embedding an external executable inside a C# program will help.

Community
  • 1
  • 1
mswietlicki
  • 1,413
  • 12
  • 16
  • I remember I also saw solution without saving exe file but you will have to look for it. – mswietlicki Sep 05 '13 at 15:29
  • That answer doesn't "run an embedded executable". It extracts it as a resource, saves it to file, runs the file from disk, and then deletes the file it extracted. None of that is what the question here asks. – Ken White Sep 05 '13 at 15:59
1

The possible solution is running an external tool as follows:

string windowsVersion = Utilities.GetWindowsVersion(); 
//...
static class Utilities { // Just a sample of cmd.exe invocation
    public static string GetWindowsVersion() {
        using(Process versionTool = new Process()) {
            versionTool.StartInfo.FileName = "cmd.exe";
            versionTool.StartInfo.Arguments = "/c ver";
            versionTool.StartInfo.UseShellExecute = false;
            versionTool.StartInfo.RedirectStandardOutput = true;
            versionTool.Start();
            string output = versionTool.StandardOutput.ReadToEnd();
            versionTool.WaitForExit();
            return output.Trim();
        }
    }
}

You can embed these executable into your program(as resources), extract these tools into specific location at run-time and then run these tools as it specified above.

Please also take a look at the following thread: process.start() embedded exe without extracting to file first c#

Community
  • 1
  • 1
DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • Your answer doesn't "run an embedded executable". It extracts it and runs it from disk. The answer you linked to does not run it embedded, either. It extracts it as a resource, saves it to file, runs the file from disk, and then deletes the file it extracted. (See the last three lines of code in the linked answer.) Neither yours or the linked one answer the question asked her. – Ken White Sep 05 '13 at 16:00
  • @KenWhite The question is not about "running the embedded executable" but about the combining some tools into one library and using this library in some ways. And I believe, it does not matter for the OP how the thing is done if the results are acceptable. My answer is just a point to the direction of how it can be done... – DmitryG Sep 05 '13 at 16:30
  • The question asked about "embedding the console executable in a DLKL and then calling them as functions inside the DLLs", which is clearly *not* "extracting the application from the DLL and running it". – Ken White Sep 05 '13 at 16:33