【问题标题】:Dll Import : Unable to find Entry Point "fnMultiply" in DLL "ImportDLL"Dll 导入:无法在 DLL“ImportDLL”中找到入口点“fnMultiply”
【发布时间】:2012-09-30 08:59:44
【问题描述】:

我正在尝试使用 DLLImport 在 C# 中使用 Win32 dll 方法。

Win32 dll C++ // .h 文件

#ifdef IMPORTDLL_EXPORTS
#define IMPORTDLL_API __declspec(dllexport)
#else
#define IMPORTDLL_API __declspec(dllimport)
#endif

// This class is exported from the ImportDLL.dll
class IMPORTDLL_API CImportDLL {
public:
    CImportDLL(void);
    // TODO: add your methods here.
    int Add(int a , int b);
};

extern IMPORTDLL_API int nImportDLL;

IMPORTDLL_API int fnImportDLL(void);
IMPORTDLL_API int fnMultiply(int a,int b);

// .cpp 文件

// ImportDLL.cpp : 定义 DLL 应用程序的导出函数。 //

#include "stdafx.h"
#include "ImportDLL.h"


// This is an example of an exported variable
IMPORTDLL_API int nImportDLL=0;

// This is an example of an exported function.
IMPORTDLL_API int fnImportDLL(void)
{
    return 42;
}

IMPORTDLL_API int fnMultiply(int a , int b)
{
    return (a*b);
}

一旦我构建了这个,我就会得到 ImportDLL.dll

现在我创建 Windows 应用程序并将此 dll 添加到调试文件夹中并尝试使用 DLLImport 使用此方法

[DllImport("ImportDLL.dll")]
 public static extern int fnMultiply(int a, int b);

我尝试在 C# 中调用它 int a = fnMultiply(5, 6); // 这行给出错误无法找到入口点

任何人都可以告诉我我错过了什么吗? 谢谢。

【问题讨论】:

  • @HansPassant 这不是实例方法。它是 DLL 中的常规公共函数。检查标题声明。事实上,这是 VS 吐出的“用导出的符号为我创建一个 DLL”项目的股票。他不是在尝试调用实例方法。他正在尝试连接一个导出的函数。
  • 你是对的,被班级绊倒了。

标签: c# c++ dllimport


【解决方案1】:

如果您从本机 DLL 导出 C 函数,您可能希望使用 __stdcall calling convention(相当于 WINAPI,即大多数 Win32 API C 接口函数使用的调用约定,它是.NET P/Invoke 的默认值):

extern "C" MYDLL_API int __stdcall fnMultiply(int a, int b)
{
    return a*b;
}

// Note: update also the .h DLL public header file with __stdcall.

此外,如果您想避免名称混淆,您可能需要export using .DEF files。 例如将 .DEF 文件添加到您的本机 DLL 项目中,然后编辑其内容,如下所示:

LIBRARY MYDLL
EXPORTS
  fnMultiply @1
  ...

(你可以使用命令行工具DUMPBIN/EXPORTS,或者像Dependency Walker这样的GUI工具,查看函数的实际名称从 DLL 中导出。)

然后你可以在 C# 中像这样使用 P/Invoke:

[DllImport("MyDLL.dll")]
public static extern int fnMultiply(int a, int b);

【讨论】:

    【解决方案2】:

    为您导出的函数关闭名称修改。应该大有帮助。或者,您可以加载名称 mangled(有一种方法可以配置 DllImport 属性来执行此操作,所以我听说了,但我不是 C# 工程师,所以我把它留给您查找它是否存在)。

    extern "C" IMPORTDLL_API int fnMultiply(int a , int b)
    {
        return (a*b);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 2012-12-08
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多