Load CLR assembly (with unmanaged code) as embedded resource in C# exe

1

I would like to have a CLR assembly stored as an embedded resource within my C# application and loaded during runtime. I included the dll as an embedded resource and attempted to retrieve the data bytes during runtime and load the assembly as follows

        using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyNamespace.MyCLRAssembly.dll"))
        {
            byte[] assemblyData = new byte[stream.Length];
            stream.Read(assemblyData, 0, assemblyData.Length);
            return Assembly.Load(assemblyData);
        }

However, this failed, reporting

Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)

To prevent this error, the CLR dll needs to be compiled with the /CLR:Pure switch, however this is not possible because the CLR dll contains unmanaged code.

Therefore I tried an alternative solution to extract the assembly and copy to a temp folder in the user AppData path, then setting the PATH variable accordingly. This failed, reporting file not found exception when the application attempted to access the CLR assembly. I can copy to the installation path of the C# application and this runs okay but not in a remote folder.

As a test, I copied the CLR dll to a folder within the PATH variable but this failed. Can someone tell me what I am doing wrong? Does the problem relate to a difference in assembly types i.e. C# assembly loading an assembly with managed and unmanaged C++.

Thanks.

c#
clr
embedded-resource
unmanaged
managed
asked on Stack Overflow May 2, 2018 by user5265160

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0