1

I am trying to use my C++ dll on Unity, so I copied it in Assets/Plugins and at the root of the project but I have DllFoundException when I use the Play button or run the .exe file generated by the build. It doesn't even work when I use the absolute path of the dll file in the DllImport.

However, it works fine when I Build&Run the project.

Unity Code

using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using Dummiesman;

public class morph_face : MonoBehaviour
{
    bool morphed;


    [DllImport(@"facemorph", CallingConvention=CallingConvention.Cdecl)]
    static extern void morphModelsPoints(string src_model, string src_csv,
            string dst_csv, string output_path);


    public GameObject model;

    // Start is called before the first frame update
    void Start()
    {
        morphed = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (!morphed && Input.GetKeyDown("space")) {
            Debug.Log("SpaceBar pressed the model will be modified.");
            morphModelsPoints("Data/src.obj", "Data/src.csv", "Data/dst.csv",
                "Data/res.obj");

            //disable old mesh
            model.SetActive(false);

            OBJLoader obj = new OBJLoader();
            model = obj.Load("Data/res.obj");

            //displays new mesh
            model.SetActive(true);

            morphed = true;
        }
    }
}

The Dll was built with this configuration: Release/Win32.

Here's the dll import settings : enter image description here

D.B
  • 596
  • 4
  • 17
Xavier
  • 13
  • 3
  • Can you show us the dll unity import settings (inspector view of you dll). Maybe you didn't check the Editor plateform. Look [here](https://docs.unity3d.com/Manual/PluginInspector.html). – D.B Oct 13 '20 at 08:02
  • Here are the import settings: [link](https://cdn.discordapp.com/attachments/369053408256327680/765485130248618034/unknown.png). – Xavier Oct 13 '20 at 08:05
  • I'm not sure but can you try to build your library for x64 too and use the x64 for editor and the x86 for build ? – D.B Oct 13 '20 at 08:16
  • I just did it and it works fine, thanks. – Xavier Oct 13 '20 at 08:39

1 Answers1

1

※Please correct me if I am wrong

If I am not mistaken, you can not use 32 dlls in Editor because UNITY is 64bit. If you can just rebuild your dll to 64bit. If you build a standalone then you must set your architecture to x86 instead of x86_64 in Build Settings.

Hasan Emrah Süngü
  • 3,488
  • 1
  • 15
  • 33
  • @Xavier, Glad I was of help to you. If it solved your problem, you might want to accept the answer so that it would help other people with similar problems – Hasan Emrah Süngü Oct 13 '20 at 09:21