tags:

views:

987

answers:

5

Hello

does somebody know how can I embedd an exe file into a dll ?

I have a tool which is an exe file that I call from c# code.

The thing is that I want to have 1 dll containing this tool (exe file) and the dll containg my c# code.

Is it possible to embedd this exe file within the resources?

Thx in advance

+5  A: 

Sure it is. You can add any file as RC_DATA in application as resource. But I believe you will need to extract it to disk first before calling it!

Which IDE/Language you are using?

[EDIT]

Sorry! you did mention that you are using C#.

  1. Add a resource file to you application (right click application in IDE and select "Add new item".
  2. Use the toolbar in resource editor to add an existing file.
  3. Then extract the exe whenever required by calling code something like: System.IO.File.WriteAllBytes (@"C:\MyEXE\", Resource1.MyEXE);
Hemant
The rules goes, except, you can load an Assembly from a byte[], so no need to create the file.
leppie
The same rules goes, .... (oops typo)
leppie
thx for your answerSo finaly, I would need to write it on the filesystem to execute it anyway? there is no possibility to execute it without doing this ?
If its a .Net EXE, then it can be loaded as an assembly. Otherwise, no. Extracting and running separately is the only thing you can do.
Joel Lucsy
ok thx you very much !
+5  A: 

It's worth baring in mind that your uses may not be too happy about you doing this. Embedding an executable that they've got no control over into a DLL that you'll extract and run will probably make people worry about the running a Trojan on their machine.

It's better to leave the .EXE in the filesystem and be transparent about what your application is doing.

Sean
+2  A: 

You can load an Assembly from a byte[]. This can be obtained via the ManifestResourceStream of an embedded resource.

leppie
+1  A: 

An alternative may be to not embed the .exe itself, but rather include its functionality in the dll, and use rundll32[1] to execute it.

Logan Capaldo
A: 

On a side note, remember that when you pull a file from your resources to disk and then execute code on it, you may trigger Windows Data Execution Prevention - basically, Windows tries to automatically detect if something is supposed to be code or data, and if it looks like data (which a resource would), then it will prevent that data from being executed as code.

This becomes a particularly sticky issue if your .NET assembly is going to be used over a network instead of from a local drive - there are all sorts of .NET security configurations that might prevent this from working correctly.

Another option, and not knowing the details of your project, take this with a grain of salt: add a .exe.readme file to your install that describes to any curious users or IT people why there is an executable they weren't expecting in the installation directory :)

Mike