Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to load OpenCvSharpExtern.dll using .NET Framework 3.5 #352

Closed
LeonidVasilyev opened this issue May 22, 2017 · 4 comments
Closed
Labels

Comments

@LeonidVasilyev
Copy link

LeonidVasilyev commented May 22, 2017

I use OpenCvSharp3-AnyCPU 3.2.0.20170419, .NET Framework 3.5, Visual Studio 2015 on Windows 8.1 x64. Exception is thrown every time. Same code works fine using .NET Framework 4.0 and newer.

Steps to reproduce:

  1. Create console application with single Program.cs file:
using OpenCvSharp;

namespace Foo
{
    class Program
    {
        static void Main(string[] args)
        {
            var mat = new Mat(100, 100, MatType.CV_8UC1, new Scalar(0));
            Cv2.ImShow("test", mat);
            Cv2.WaitKey();
        }
    }
}
  1. Install OpenCvSharp3-AnyCPU 3.2.0.20170419 using NuGet package manager
  2. Launch application using Ctrl+F5

Actual result:
Application crashes with unhandled exception:

System.TypeInitializationException: The type initializer for 'OpenCvSharp.Mat' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'OpenCvSharpExtern': The specified module could not be found. (Exception from HRESULT: 0
x8007007E)
at OpenCvSharp.NativeMethods.core_Mat_sizeof()
at OpenCvSharp.Mat..cctor() in C:\projects\opencvsharp\src\OpenCvSharp\Module
s\core\Mat\Mat.cs:line 610
--- End of inner exception stack trace ---
at OpenCvSharp.Mat..ctor(Int32 rows, Int32 cols, MatType type, Scalar s)
at Foo.Program.Main(String[] args)

Expected result:
Black square image is shown

@thienvu20
Copy link

I got the same problem, in the mean time you can add an initialize an cvSVMparam = new cvSVMParam(); and use mat

@shimat
Copy link
Owner

shimat commented Jul 4, 2017

Please try to call OpenCvSharp.NativeMethods.IsUnix() before your code.

NativeMethods.IsUnix();

var mat = new Mat(100, 100, MatType.CV_8UC1, new Scalar(0));
Cv2.ImShow("test", mat);
Cv2.WaitKey();

This is because static constructor seems not to work in .NET 3.5 or older. 😩

static class Foo
{
    static Foo()
    {
        Console.WriteLine("static constructor");
    }

    [DllImport("kernel32")]
    public static extern int Beep(uint dwFreq, uint dwDuration);
}
class Program
{
    static void Main()
    {
        // "static constructor" not displayed
        Foo.Beep(1000, 1000);
    }
}

@LeonidVasilyev
Copy link
Author

The OpenCvSharp.NativeMethods.IsUnix(); call allows me to successfully create matrix instance, yet I couldn't reproduce issue with static constructor in Visual Studio 2015 Community.

@shimat
Copy link
Owner

shimat commented Jul 11, 2017

I found another condition. The problem occurs if P/Invoke class has [SuppressUnmanagedCodeSecurity] attribute and the .NET version is 3.5 or older.

https://github.com/shimat/opencvsharp/blob/master/src/OpenCvSharp/PInvoke/NativeMethods.cs#L20

I will remove SuppressUnmanagedCodeSecurity when .NET < 4.0

#if DOTNET_FRAMEWORK && !net20
[SuppressUnmanagedCodeSecurity]
#endif
static class NativeMethods
{
   ...

@shimat shimat closed this as completed in feec1f2 Jul 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants