托管C#Win32应用程序的非托管DLL.与课堂入口
Unmanaged DLL to Managed C# win32 application. Entry with Class
我的问题是试图访问名为函数的类中定义的 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),上面是上面的示例。由于我已经在这里有#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
类内部,我不知道该怎么做。
错误是。找不到入口点Add
。
您非常接近使它起作用,并且您的怀疑是正确的。为了使您的.NET代码插入DLL中,它需要在DLL中找到预期的C型函数符号。由于您的dll是使用C 代码构建的(无论您使用的外部" C",该函数名称都被填充,因此.NET无法找到它的期望。
解决方案
要解决此问题,您的函数声明需要是纯C(并保留围绕这些声明的外部" C")。这意味着没有名称空间,没有类,没有参考等。但是,如果需要,函数实现代码可以为C 。您只需要.NET和您的DLL就可以就声明(接口)达成共识。
建议
出于某种原因,您需要按照它的方式将所有C 代码保留(因为其他代码正在使用),则只需在其顶部创建一个新的薄C层,这将是您使用的DLL公共接口。net。
相关文章:
- 挂起和取消挂起一个文件DLL
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 导入库可以跨dll版本工作吗
- 从C++dll访问C#中的一行主要参数
- 链接到自行创建的dll失败
- 为什么使用 P/Invoke 调用 dll 时,某些计算机中的 LoadLibrary 失败?
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- 如何将图像传输到c++(dll)中的缓冲区,然后在c#的缓冲区中读/写
- C++:将外部库链接到dll库
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- 不同的Visual Studio版本中缺少.dll
- 从DLL中删除类的实例
- 如何包装第三方DLL在R中使用
- 使用c#访问c++dll中带有char*参数的函数时发生AccessViolationException
- 系统.将数组移交给c#中动态加载的c++DLL时发生AccessViolationException
- 为什么导入Mixed native/CLR lib.dll的本机C++应用程序没有在Mixed lib.dll中的外部变
- 当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题
- Python ctypes:不会按预期加载 dll
- 托管C#Win32应用程序的非托管DLL.与课堂入口