Home > Software design >  Trouble using DLL
Trouble using DLL

Time:09-17

I am completely new to C#. I am trying to run a C# code that uses a DLL written in C . The DLL file is,

#include "pch.h"
#include <vector>
#include <iostream>

extern "C" __declspec(dllexport) void __cdecl analysis(double* values, int len)
{
    using namespace std;

    vector<int> ind(values, values   len);
    int n = ind.size();
    for (int i = 0; i < n; i  )
    {
        cout << ind[i] << endl;
    }
}

And the C# code,

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

namespace Test_run
{
    class Program
    {
        [DllImport("Test.dll")]
        public static extern void analysis([MarshalAs(UnmanagedType.LPArray)] double[] values, int len);

        static void Main(string[] args)
        {
            List<double> lst = new List<double>();
            lst.Add(1.2);
            lst.Add(2.3);
            lst.Add(3.4);

            analysis(lst.ToArray(), lst.Count);
        }
    }
}

Both files compile individually. But when I run the program it raises exception "Unable to load DLL "Test.dll" or one of its dependencies: The specified module could not be found. (0x8007007E)". They are both in same solution and the C# project has the DLL added.

CodePudding user response:

It isn't sufficient to add the DLL to the project, but you need to make sure the DLL file is actually copied over to the output directory of the Executable.

If you added the DLL file in your C# project, you can set the "Build Action" => "None", and "Copy to Output Directory" => "Copy if newer". That should make sure your dll is in the right place.

enter image description here

  • Related