【问题标题】:System.EntryNotFoundException: Unable to find the entry point in the DLLSystem.EntryNotFoundException:无法在 DLL 中找到入口点
【发布时间】:2012-02-17 05:41:16
【问题描述】:

我正在准备一个小的 C++ dll,其中的函数将从 C# 中调用。

DLLTestFile.h

#ifdef DLLFUNCTIONEXPOSETEST_EXPORTS
#define DLLFUNCTIONEXPOSETEST_API __declspec(dllexport)
#else
#define DLLFUNCTIONEXPOSETEST_API __declspec(dllimport)
#endif

extern "C" DLLFUNCTIONEXPOSETEST_API int fnSumofTwoDigits(int a, int b);

DLLTestfile.cpp

#include "stdafx.h"
#include "DLLFunctionExposeTest.h"
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    return TRUE;
}
DLLFUNCTIONEXPOSETEST_API int fnSumofTwoDigits(int a, int b)
{
    return a + b;
}

C# 项目:

static class TestImport
    {
        [DllImport("DLLFunctionExposeTest.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "fnSumofTwoDigits")]
        public static extern int fnSumofTwoDigits(int a, int b);
    }
public partial class MainWindow : Window
    {
        int e = 3, f = 4;
        public MainWindow()
        {
            try
            {
            InitializeComponent();
            int g = TestImport.fnSumofTwoDigits(e, f);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

我收到异常:“System.EntryNotFoundException:无法在 DLL 中找到入口点”

我在创建新项目时使用 Visual Studio 提供的默认模板,Visual C++ -> Win32 项目 -> DLL(已检查导出符号)。有人可以为此提出解决方案吗?找了很久也没找到问题。

【问题讨论】:

    标签: c# c++ wpf visual-c++


    【解决方案1】:

    对我来说很好,完整的文件供参考:

    dllmain.cpp:

    // dllmain.cpp : Defines the entry point for the DLL application.
    #include "stdafx.h"
    #include "DLL.h"
    
    BOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
                         )
    {
        return TRUE;
    }
    
    DLL_API int fnSumofTwoDigits(int a, int b)
    {
        return a + b;
    }
    

    DLL.h:

    // The following ifdef block is the standard way of creating macros which make exporting 
    // from a DLL simpler. All files within this DLL are compiled with the DLL_EXPORTS
    // symbol defined on the command line. This symbol should not be defined on any project
    // that uses this DLL. This way any other project whose source files include this file see 
    // DLL_API functions as being imported from a DLL, whereas this DLL sees symbols
    // defined with this macro as being exported.
    #ifdef DLL_EXPORTS
    #define DLL_API __declspec(dllexport)
    #else
    #define DLL_API __declspec(dllimport)
    #endif
    
    extern "C" DLL_API int fnSumofTwoDigits(int a, int b);
    

    Program.cs(为简单起见,Win32 控制台应用程序):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication2
    {
        class Program
        {
            [DllImport("C:\\Users\\Kep\\Documents\\Visual Studio 2010\\Projects\\SODLL\\Debug\\DLL.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "fnSumofTwoDigits")]
            public static extern int fnSumofTwoDigits(int a, int b);
    
            static void Main(string[] args)
            {
                int A = fnSumofTwoDigits(3, 4);
                Console.WriteLine("A = " + A);
                Console.ReadLine();
            }
        }
    }
    

    【讨论】:

    • 仍然无法找出我的文件中缺少的内容。捕获到的异常截图:i.imgur.com/PJfxz.png
    • 那是 DllNotFoundException,像我一样使用完整路径进行测试。
    • #FML。是的! P.S 如果必须在没有上述完整路径的情况下完成 DLL,您实际上将 DLL 保存在哪里?
    • 例如,在与应用程序相同的文件夹中。或相对于它并使用相对路径。
    【解决方案2】:

    可能是您的 C# 进程以 64 位运行,而您的 DLL 是 32 位,反之亦然。当进程和 DLL 的位数不匹配时,我已经看到了这个问题。

    【讨论】:

      【解决方案3】:

      看起来您没有定义 DLLFUNCTIONEXPOSEEST_EXPORTS 所以您使用导入声明。测试使用 dumpbin /exports 来查看从 dll 中导出了哪些函数。

      添加

      #define DLLFUNCTIONEXPOSETEST_EXPORTS 1 
      #include DLLTestFile.h
      

      【讨论】:

      • DLLFUNCTIONEXPOSETEST_EXPORTS 由 Visual Studio 自动为 DLL 项目定义。刚刚测试了上面的代码,效果很好。
      • 这是我的 Dll 上的 Dumpbin.exe 工具的截图。 i.imgur.com/YXrvR.png
      • @ccKep:工作正常吗?我在这里遇到的入口点错误呢?
      • 你的名字有问题。这意味着外部“C”部分有问题
      • @rerun:据我所知,他的 fnSumofTwoDigits 没有损坏?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 2021-08-05
      • 2017-08-31
      • 1970-01-01
      • 2015-09-28
      相关资源
      最近更新 更多