.DLL file cannot be loaded

When I put a DLL file created in c++ in the Plugins folder, it says “Could not load image ***.dll due to Metadata verifier doesn’t handle sections with SizeOfRawData < VirtualSize. Run the peverify utility against this for more information.”

I get an error like this and cannot load the DLL.

When I run [Peverify] at the developer command prompt, I get this output
“NET Framework PE Verifier Version 4.0.30319.0
Copyright (c) Microsoft Corporation.
[MD]: Unverifiable PE Header/native stub.”

I would appreciate it if you could provide me with any information, even if it is trivial, that might help me to load the file correctly.

Due to circumstances, I cannot post the c++ source.

EditorVersion : UnityEditor 2021.3.15f1

Having the exact same issue to the letter.

I have to add that all other .dlls are working just fine and it was a clean project from 3D template.
And peverify had one more line: [MD]: Error: Value class has neither fields nor size parameter.

@suu31 , were you able to overcome it?

Unity version 2021.1.7f1

Sounds like we for some reason think its a managed plugin.
If possible, can you share the plugin + the plugin’s meta file? Or better yet, a simple project where it happens

Thanks for the quick response!

It is in fact a managed plugin. It is an auto-generated .Net wrapper interface for the C++ library.
The issue complicated with a fact that some of the .dlls are property of the 3D party and we are unable to share them.

It is highly appreciated if we could deduct it without the sample project, I am eager to provide any information required.
In the worst case scenario we can create a sample project with just the wrapper part, but I think it will generate more problems down the path.

Ok lets try :slight_smile:

@IMTactica how is the assembly wrapping the interfaces for the native library. Is it just extern and DllImport?

Yes, basically Math library (let’s call it this way for simplicity) written in C++ is wrapped which results in a bunch of .dlls.

I’ve tried using those Math2C#.dlls in an empty .Net project - it was working just fine.
But when I’ve moved them to Unity I was able to get to only small portion of the namespaces containing in Math2C#.dlls.

As I was able to see everything in empty .Net project, I’ve decided to create a Math2C#Unity.dll which would contain interface for needed portion of namespaces from Math2C#.dlls.
And I was able to reference and use Math2C#Unity.dll in empty Unity project but any Run from the editor would end up with this error:

Could not load image Math2C#.dll due to Metadata verifier doesn’t handle sections with SizeOfRawData < VirtualSize
Run the peverify utility against this for more information.

I’m not quite sure how exactly is this C++ library wrapped as it was done by 3rd party, but I can get more information if needed.

@IMTactica have you tried to run it with the Mono runtime? To see if its something off there.
If that succeeds then yes it would be great to know how, but you could also open up the dll with a decompiler and share some of it. Or if fx. dotPeek even can open it

Unity uses Mono as Runtime, so that would be interesting

I’ll try those options and will get back to you with an update. :slight_smile:

1 Like

Did u find a solution? I got the same problem.
I created a simple C# dll and C++ Clr dll. C# uses C++ dll. All is good in VisualStudio but when I check it in Unity I get an error:

Could not load image ...\Assets\Plugins\CppClassLibrary.dll due to Metadata verifier doesn't handle sections with SizeOfRawData < VirtualSize

FileNotFoundException: Could not load file or assembly 'CppClassLibrary, Version=1.0.8446.20180, Culture=neutral, PublicKeyToken=null' or one of its dependencies.

C#.dll

using Cpp;

namespace Sharp {
    public class SharpClass     {
        // C# static method
        public static int SharpAddStatic(int a, int b) {return a + b;}
        // C# instance method
        public int SharpAdd(int a, int b) {return a + b;}
        // C# uses C++ static method from CppClassLibrary.dll
        public static int SharpCppAdd(int a, int b) {return CppClass.CppAddStatic(a, b);}
    }
}

C++.dll (CppClassLibrary.dll)

#pragma once
namespace Cpp {
    public ref class CppClass {
    public:
        static int CppAddStatic(int a, int b) {return a + b;}
    };
}
Debug.LogError(SharpClass.SharpAddStatic(1,2)); // all is good
Debug.LogError(new SharpClass().SharpAdd(3,2)); // all is good
Debug.LogError(SharpClass.SharpCppAdd(4,3)); // error

Ah yes its C++ clr integration you are trying to use here.
Not sure if this is supported in Unity, @JoshPeterson to confirm

@MichaelFil Unfortunately, Unity does not support C++/CLR, so that DLL won’t work. You may need to use standard C++ with p/invoke from C#, which is supported.

1 Like

So that I can’t run C/C++ code in Unity WebGL, right?

It is possible to call C or C++ code from C# with Unity WebGL. See this documentation for information: https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html

Thank you very much, but can I wrap/encrypt source code?

I don’t think that you can. Any code shipped via IL2CPP should not be considered encrypted. There are ways for people to reverse engineer it.