【发布时间】:2016-12-05 02:22:14
【问题描述】:
我的问题是试图访问一个名为 function 的类中定义的 dll 函数。
namespace MathLibrary
{
extern "C"
{
__declspec(dllexport) double Functions::Add(double a, double b)
{
return a + b;
}
__declspec(dllexport) double Functions::Multiply(double a, double b)
{
return a * b;
}
__declspec(dllexport) double Functions::AddMultiply(double a, double b)
{
return a + (a * b);
}
}
}
这就是我从 C++ 中创建的 MathLibrary.dll 现在我想在 C# 中使用它,所以我尝试了 PInvoke 方法。我不知道这叫什么程序,请告诉我我错了。
class Program
{
[DllImport("MathLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double Add(double a, double b);
static void Main(string[] args)
{
double myNumber = Add(10, 5); <- this is the Line. I suspect that it is because the Add is inside a class name Functions that I why I can't Access it and having error that there is no Entry point for the method Add.
Console.WriteLine(myNumber);
}
}
MathLibrary.h 看起来像这样,我什至不知道是否仍然需要在 MathLibrary.cpp 上添加 __declspec(dllexport),例如上面的示例。因为我在这里已经为 dllexport 设置了#ifdef。
#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);
};
}
所以回到问题。我怀疑我必须从 PInvoke 或 DLLImport 定义 DLL 函数,该方法位于 Function CLass 内,我不知道该怎么做。
错误是。找不到入口点Add。
【问题讨论】: