tags:

views:

181

answers:

5

There is a DLL that is made when I compile one of my programs. The DLL contains images that the software displays. I have made another software, a configurator. I want it to be able to open the DLL a replace the banner, and only the banner, image with whatever image the user chooses and then recompile into a dll. How can I do this?

+1  A: 

You probably want to work with resources API - UpdateResource and friends.

EFraim
Are you sure, UpdateResource can change resources in .NET assemblies? The format of resources in native DLL and in managed resources are not same.
TcKs
@TcKs: good point. I really don't know.
EFraim
+1  A: 

There are Microsoft-sanctioned strategies for that, in particular, the resource sections of a PE image.

However, I would recommend to have a large array in C which starts with a certain magic sequence, and extends over a known size. Then, on replacing the array, search for the magic sequence, and just replace the bytes in the DLL.

Martin v. Löwis
A: 

Look at Mono - Cecil. Cecil is tool (API) which allows you change/rewrite IL code of assemblies. It should also rewrite the resources.

TcKs
A: 

Be aware that any approach you do with this is going to have side effects - in particular, it will break any code signing on the assembly.

You may want to consider having a separate resource file that your assembly loads. This makes it trivial to update. You could even use some form of simple DB if you have multiple resources.

Reed Copsey
A: 

You should probably consider compiling the DLL on the fly using the built-in C# or VB.NET compiler available in .NET. The exact classes implementing the compilers are Microsoft.CSharp.CSharpCodeProvider and Microsoft.VisualBasic.VBCodeProvider. Both are defined in the System.dll assembly.

Alternatively, you can also use the csc.exe, vbc.exe and resgen.exe applications installed with the .NET Framework to produce the DLL.

This could be done via spawning a process using the System.Diagnostics.Process class. Using that, you can capture the Console.In and Console.Out streams to redirect console I/O to a StreamReader/StreamWriter, for tight integration with the Configurator function.

Having said that, I think the better option is the first one, where you are using the actual compiler classes, rather than calling the executables (which internally just call the compiler classes too).

If all else fails, there is a compiler constructor project on CodePlex that might be of some use. It has things like PEAssemblyReader and PEAssemblyWriter classes (not too sure about the exact names), but the classes allow one to read and write PE files. PE stands for portable executable and is the format used by Windows for executable files. In this option though, you will need to know exactly how to modify the PE structure in-memory and them write out the modified PE structure.

I still think the first option is the one to use, unless you won't have the sources for those DLLs available at run-time.

How can I use the compiler to create the dll. Could you elaborate on the first option, thank you.
Jake