Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

DllNotFoundException

Discussion in 'Editor & General Support' started by dershver_unity, Jun 2, 2020.

  1. dershver_unity

    dershver_unity

    Joined:
    Jun 2, 2020
    Posts:
    8
    I am testing this code in the Editor. Using Unity 2018 4.23f1. A script has the following lines:
    [DllImport("ObjLoad")] // loads C++ DLL
    public static extern int iLoadObjs( IntPtr pcIP, int iPort, out int iObjs, out IntPtr ptVT);


    When it executes in the Editor, I get the following:
    Plugins: Failed to load 'Assets/Plugins/x64/ObjLoad.dll' with error 'The specified module could not be found.
    DllNotFoundException: ObjLoad


    ObjLoad.dll is in the Assets/plugins\x64 folder. It was built in Visual Studio as an x64 DLL.
    It was linked with the /MD option i.e. C Runtime Library DLL.
    As a workaround I tried to link with /MT i.e. static lib. This failed in Visual Studio with a MSB8024 error.
    Any ideas what is causing the DllNotFoundException ?
     
  2. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
  3. dershver_unity

    dershver_unity

    Joined:
    Jun 2, 2020
    Posts:
    8
    Yes, I did view it in the Inspector. It is an x64 and available for all platforms.
    Is there anything else, I should check ?
    By the way, the DLL was built for UWP in Visual Studio 2019.
     
  4. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
    Do you maybe have another dependency from your `ObjLoad.dll` to another DLL that you're not including?

    Maybe you can test first using an almost empty library with a very basic function and check if you can make it work properly within Unity. This will help you to determine if it's because of the library content or it's a problem of setup.


    Good luck with it!
     
  5. dershver_unity

    dershver_unity

    Joined:
    Jun 2, 2020
    Posts:
    8
    Thank you for your reply. The DLL is in C++ and does not call any other dll's except for the standard C runtime dll's.
    I tried re-installing the VC++ redistributables and I even copied them into Assets\Plugins\x64.
    It still fails.


    Anyway, I have finally resolved this issue and I hope this post will save others from many wasted hours.
    I rebuilt the DLL as a Windows DLL instead of UWP. It now works with the editor. This program is intended for the Hololens. Now I am confused because the Hololens uses the UWP platform. Do I need 2 DLL's, one in editor mode
    and the other for the Hololens ? Maybe the Hololens will run a Windows DLL.
    However, it seems to me there is an issue with Unity not accepting a UWP DLL.
    I did find the following post:

    https://stackoverflow.com/questions/49793560/build-c-plugin-for-unity?rq=1
     
  6. DiegoDePalacio

    DiegoDePalacio

    Unity Technologies

    Joined:
    Oct 28, 2009
    Posts:
    506
    I'm glad that you find the way of having your DLL working in Unity!

    And thank you for sharing your findings.

    On the other hand, even taking into account that a build for Hololens uses UWP, doesn't necessarily mean that Unity is able to use UWP plugins.

    Unity is able to create a UWP build with Hololens as a target device, so my recommendation for you is to make a simple build using your DLL in some way, just for testing purposes.

    Then install the build and check if it's working and using your DLL.


    Good luck with your project!
     
  7. dershver_unity

    dershver_unity

    Joined:
    Jun 2, 2020
    Posts:
    8

    I finally built the app for the HoloLens. It works fine in the Editor. However,
    on the HoloLens it crashes with the "DLL not found exception." Should I go back and change the DLL
    to a UWP build ? Or is there some other step that I need to perform to include the DLL in the build.
    It is in the Plugins/x86 folder.