我很难把这件事办好。我创建了一个测试解决方案(VS2015),它包含一个c++ DLL项目、一个c++ ConsoleApplication和一个c# ConsoleApplication。两个console Application都在其主方法中调用DLL函数,并将结果打印到控制台。所有项目都构建在同一个目录中。
这在c++ ConsoleApplication中工作得很好,因为我只是添加了对DLL的引用并包含了标题。
在c# ConsoleApplication中,我无法让它工作。我知道它的非托管代码,我需要使用DLLImport。
当我尝试运行c#应用程序时,它会在调用test()时引发一个System.EntryPointNotFoundException。并表示无法找到入口点“测试”。
那我做错什么了?
DLL.h
#ifdef DLL_EXPORTS
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
DLL_API int test(void);
DLL.cpp
#include "DLL.h"
// This is an example of an exported function.
DLL_API int test(void)
{
return 42;
}它编译成DLL.dll。
Programm.cs
using System;
using System.Runtime.InteropServices;
namespace DLLTestCSharp
{
class Program
{
[DllImport("DLL.dll")]
public static extern int test();
static void Main(string[] args)
{
Console.WriteLine(test());
Console.Read();
}
}
}它编译成DLLTestCSharp.exe。

发布于 2016-02-03 19:58:01
由于C++名称的损坏,无法找到入口点。它以一种特殊的独特方式编码方法名,以支持重载和模板。
若要禁用此行为和导出方法名称,您需要使用extern "C"标记您的方法(或放入extern "C" { }块),或者向您的dll项目中添加具有导出名称的.def文件。
通常是在头文件中完成的:
#ifdef __cplusplus
extern "C" {
#endif
DLL_API int test();
// other functions
#ifdef __cplusplus
}
#endif这将适用于所有平台。
另一方面,Def文件主要是Windows方法。将ProjectName.def添加到解决方案中,并将其放置在导出列表中:
LIBRARY <libraryname>
EXPORTS
test关于MSDN的更多信息
https://stackoverflow.com/questions/35185678
复制相似问题