4

I try to compile my app which has CefSharp as dependency. Everything works when targeting x86 but for x64 I get the following error:

System.BadImageFormatException: 'Could not load file or assembly 'CefSharp, Version=67.0.0.0, Culture=neutral, PublicKeyToken=40c4b6fc221f4138' or one of its dependencies. An attempt was made to load a program with an incorrect format.'

Any ideas? Thanks.

Here is a screenshot of my configuration settings inside VS 2017,

enter image description here

and the Project properties' Build tab:

enter image description here

Martin Dimitrov
  • 4,796
  • 5
  • 46
  • 62
  • `65.0.0` is the current release. Try using an official release instead of an experimental `67` build. Past that there is insufficient detail here to know what's going on. – amaitland Jul 27 '18 at 11:33
  • @amaitland, Thanks for looking into this. I've built my own version of CEF and CefSharp to support mp3/4. I will provide a screenshof the the Configuration Manager inside Visual Studio 2017 but other than that, what other info I should give? – Martin Dimitrov Jul 27 '18 at 11:37
  • the referenced assembly CefSharp should also be build for x64. – Cee McSharpface Jul 27 '18 at 11:41
  • Possible duplicate of [Could not load file or assembly 'CefSharp.Wpf for both x64 and x86; only one works](https://stackoverflow.com/questions/31172045/could-not-load-file-or-assembly-cefsharp-wpf-for-both-x64-and-x86-only-one-wor) – Cee McSharpface Jul 27 '18 at 11:43
  • @dlatikay I've looked at this question but the solution doesn't work for me. I checked the Nuget packages and all seems OK. – Martin Dimitrov Jul 27 '18 at 11:46
  • anyway at runtime, there is a clash of bitness. use [corflags](https://stackoverflow.com/a/4947501/1132334) to confirm that the bitness of the referenced dll is 32, if the source code of that is under your control look at the build settings of your CefSharp project. – Cee McSharpface Jul 27 '18 at 11:50
  • Try one of the `CI Builds` from https://www.myget.org/gallery/cefsharp If that works then you've made a mistake somewhere in your build and I really cannot help you with that. – amaitland Jul 27 '18 at 11:54
  • 2
    The solution platform name is irrelevant for C# projects. There is only one setting that matters and its value is not related to the platform name. Right-click your EXE project > Properties > Build tab. You only get 64-bit execution with Platform target = AnyCPU and Prefer 32-bit unticked. You probably have "Prefer 32-bit" ticked now (the default), so kaboom when it tries to load x64 unmanaged code. – Hans Passant Jul 27 '18 at 11:59
  • @HansPassant Thanks a lot! This was exactly the problem. Please, put as an answer so I can accept it. Thanks again. – Martin Dimitrov Jul 27 '18 at 12:16
  • I lost count of the number of times I had to answer this question. I think that only saturating Google can avoid it, please add your own version. – Hans Passant Jul 27 '18 at 12:25

1 Answers1

6

Hans Passant wrote in the comments:

The solution platform name is irrelevant for C# projects. There is only one setting that matters and its value is not related to the platform name. Right-click your EXE project > Properties > Build tab. You only get 64-bit execution with Platform target = AnyCPU and Prefer 32-bit unticked. You probably have "Prefer 32-bit" ticked now (the default), so kaboom when it tries to load x64 unmanaged code.

That was exactly the problem. Thanks Hans!

Martin Dimitrov
  • 4,796
  • 5
  • 46
  • 62
  • Why is this package not 64-bit capable like every other package out there? – Dshiz Jan 14 '19 at 04:09
  • 3
    `CefSharp` supports both `x86` and `x64`. The question is regarding having `AnyCPU` defaulting to `Prefer 32bit` which causes problems as the `x64` unmanaged assemblies cannot be loaded. If you wish to use `AnyCPU` you should see https://github.com/cefsharp/CefSharp/issues/1714 – amaitland Jan 14 '19 at 04:32