0

I have a DLL reader to extract resources(images) from dynamically created DLL. In order to test it, I have developed a DLL to specify the requirements needed for my DLL reader to extract images. The DLL worked as expected as I can manually include images into the resource folder. However, in the process of generating dynamic DLL I dont really understand the method to include resources into the solution before compiling it into DLL.

So the tools I have are:

  1. DLL reader (work)
  2. DLL with image resources (work)
  3. Dynamically generated DLL with image resource (not work)

(1) - (2) works well , (1) - (3) unable to get image

After debugging I found out that images were not added into the Dynamically added DLL.

Source code:

DLL created manually

Namespace

Images included to DLL's resources, under namespace named DLLforWPF

When I execute my DLL reader to get the image and convert it into BitmapImage, it works.

Assembly currentAssembly = Assembly.LoadFrom("DLLforWPF.dll");
string[] resources = currentAssembly.GetManifestResourceNames();
//Please see the attached image for the results
Stream myStream = currentAssembly.GetManifestResourceStream("DLLforWPF.Resources.img1.png");
            var bitmap = new BitmapImage();
            bitmap.BeginInit();
            bitmap.StreamSource = myStream;
            bitmap.CacheOption = BitmapCacheOption.OnLoad;
            bitmap.EndInit();
            bitmap.Freeze();

enter image description here

It displays the resources that I have added into the DLL.

Then I proceed to develop a solution to dynamically generate DLL with image resources. I have used stringbuilder to construct the code below:

//in stringbuilder
using System;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Dynamic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using System.Windows.Forms;

namespace ben
{
    public class Imgstored
    {
            [STAThread]
            public static void Main(string[] args)
            {
            }
    }
}
string executable = Application.ExecutablePath;
string baseDir = Path.GetDirectoryName(executable);
CodeDomProvider compiler = new CSharpCodeProvider(options.CompilerOptions);
CompilerParameters parameters = new CompilerParameters{ OutputAssembly = "ben.dll"};
parameters.WarningLevel = 4;
parameters.GenerateExecutable = false;
parameters.GenerateInMemory = true;

if (compiler.Supports(GeneratorSupport.Resources))
{
parameters.EmbeddedResources.Add($"{Application.StartupPath}\\Temp\\audio.png");
}

I have tried changing the directory such as directory\audio1.png and it gave me exception of image not found. So I am sure that the resource pointed to the image directory.

When I execute DLL reader, enter image description here

This is the result of my resources in dynamically created DLL. I am not able to convert it to BitmapImage as it is not an image resource. I suspect the process of adding image resource to CSharpCodeProvider is having some error, please tell me the problem of adding resource using CompilerParameters.EmbeddedResources.Add(image path). The result is totally different from the manually created DLL. There are very limited material regarding adding resources into dynamic DLL through CSharpCodeProvider. Thank you for your time.

ben Lim
  • 33
  • 5

1 Answers1

0

In my functional code, i have just that:

        FileInfo fi = new FileInfo(@".\files_freepie\curves\CurveAssembly.dll");
        CSharpCodeProvider provider = new CSharpCodeProvider();
        CompilerParameters compilerparams = new CompilerParameters();
        compilerparams.OutputAssembly = @".\files_freepie\curves\CurveAssembly.dll";
        CompilerResults results = provider.CompileAssemblyFromFile(compilerparams, filename);

filename is the file to compile....

i dont use

CodeDomProvider compiler = new CSharpCodeProvider(options.CompilerOptions)
Frenchy
  • 16,386
  • 3
  • 16
  • 39
  • I have tried your way of compiling the DLL, still no luck to obtain the resources from the DLL created. It compiled successfully, but it generated 2 files which is name.dll and name.Resources (0kb). – ben Lim Sep 25 '19 at 01:54
  • hum dunno understand why yours isn't functional. Is it possible to have a solution. I could try to undertand why you have a problem..? – Frenchy Sep 25 '19 at 07:44
  • Okay sure... I will zip 3 solutions and upload it for you. Thank you for helping out Frenchy. – ben Lim Sep 25 '19 at 07:57
  • Please remember to accept and vote up the answer if your original issue has been solved and then ask a new question if you have another issue: https://stackoverflow.com/help/someone-answers – Frenchy Apr 28 '20 at 14:55