BadImageFormatException while loading external DLL in dotnet core or framework

-2

I'm trying to load a DLL from SysWOW64 on a C# application. The DLL in question is "nvEncodeAPI.dll", which is an nVidia h264 encoder that I am planning to use on a future application.

Here is my code:

string filePath = Environment.ExpandEnvironmentVariables("%windir%");
            if (!filePath.Trim().EndsWith(@"\"))
                filePath += @"\";
            filePath += @"SysWOW64\nvEncodeAPI.dll";

            try
            {
                Assembly assem = Assembly.LoadFile(filePath);
            }
            catch (BadImageFormatException e)
            {
                Console.WriteLine("Unable to load {0}.", filePath);
                Console.WriteLine(e.Message.Substring(0,
                                  e.Message.IndexOf(".") + 1));
            }

The exception actured is indeed a BadImageFormatException with the message as follows:

Message = "The module was expected to contain an assembly manifest. (Exception from HRESULT: 0x80131018)"

I have tried:

  • Targeting dotnet core 3.1
  • Targeting .NET Framework 4.8
  • Targeting x86 (on both core 3.1 and Framework 4.8)
  • Targeting x64 (on both core 3.1 and Framework 4.8)
  • Using Assembly.Load Assembly.LoadFile Assembly.LoadFrom
c#
dll
assembly-loading
asked on Stack Overflow Jan 28, 2020 by LordSidious • edited Jan 29, 2020 by Ferit Yiğit BALABAN

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0