3

当我尝试从 C++ DLL 调用此函数时,我不断收到 System.EntryPointNotFoundException 错误。

我正在使用 VS 2015,这两个项目在同一个解决方案中。C# 项目还引用了 C++ 项目。

DLL 的头文件:

// MathLibrary.h - Contains declaration of Function class
#pragma once

#ifdef MATHLIBRARY_EXPORTS
#define MATHLIBRARY_API __declspec(dllexport) 
#else
#define MATHLIBRARY_API __declspec(dllimport) 
#endif


namespace MathLibrary {
    // This class is exported from the MathLibrary.dll

        class Functions {

        public:
            // Returns a + b
            static MATHLIBRARY_API double Add(double a, double b);

            // Returns a * b
            static MATHLIBRARY_API double Multiply(double a, double b);

            // Returns a + (a * b)
            static MATHLIBRARY_API double AddMultiply(double a, double b);
        };


}

DLL 的类文件:

// MathLibrary.cpp : Defines the exported functions for the DLL application.
// Compile by using: cl /EHsc /DMATHLIBRARY_EXPORTS /LD MathLibrary.cpp

#include "stdafx.h"
#include "MathLibrary.h"

extern "C" {

    namespace MathLibrary {

        double Functions::Add(double a, double b) {
            return a + b;
        }

        double Functions::Multiply(double a, double b) {
            return a * b;
        }

        double Functions::AddMultiply(double a, double b) {
            return a + (a * b);
        }
    }
}

C# 类我尝试从以下位置调用函数:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {

        [DllImport("K:\\C++\\MathLibraryAndClient\\Debug\\MathLibrary.dll")]
        public static extern double Add(double a, double b);

        static void Main(string[] args) {



            double a; double b;
            a = Console.Read();
            b = Console.Read();

            Add(a, b);
        }
    }
}

谢谢

4

0 回答 0