Accessing an OpenCV C++ DLL from a C# program in VS 2022

Following a Pluralsight course on OpenCV, I created a simple wrapper class in C++ and a consumer program in C#, all in the VS2022 IDE on Windows 11, using OpenCV 4.7.0. When I tried to call the dll from the C# program, the following exception occurred:
System.BadImageFormatException: ‘Could not load file or assembly ‘C++CLIWrapperToOpenCV, Version=1.0.8438.18274, Culture=neutral, PublicKeyToken=null’ or one of its dependencies. An attempt was made to load a program with an incorrect format.’

Inner exception
BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

The DLL and the C~ program are targetted at X64.

Any help gratefully received.


can you be a bit more specific ?

havent done this in ages, but:

  • make absolutely sure, you compile / link your wrapper dlll in exactly the same way, the opencv libs were built (.eg. /MT, NO clr/.net flags !)
  • your dll will depend on other opencv dlls (unless you linked it statically). make sure, those are on the PATH, use tools like to find out, what exactly you need there