System.EntryNotFoundException:无法在 DLL 中找到入口点

发布于 2025-01-06 06:27:33 字数 1532 浏览 2 评论 0原文

我正在准备一个小型 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(选中导出符号)。有人可以建议这个问题的解决方案吗?我找了很久也没找到问题所在。

I am preparing a small C++ dll in which the functions are to be called from 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# project:

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());
        }
    }
}

I am getting the exception: "System.EntryNotFoundException: Unable to find the entry point in the DLL"

I am using the default template given by Visual Studio, when creating a new project, Visual C++ -> Win32 Project -> DLL (Export symbols checked). Can somebody please suggest the solution for this. I haven't been able to find the problem even after looking for long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窗影残 2025-01-13 06:27:33

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

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();
        }
    }
}

Works fine for me, complete files for reference:

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 Console Application for simplicity):

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();
        }
    }
}
习ぎ惯性依靠 2025-01-13 06:27:33

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

It could be that your C# process is running as 64-bits and your DLL is 32-bits or vice versa. I have seen this problem when the bit-ness of the processes and DLLs don't match.

゛时过境迁 2025-01-13 06:27:33

看来您没有定义 DLLFUNCTIONEXPOSETEST_EXPORTS 所以您使用了 import statements 。要进行测试,请使用 dumpbin /exports 查看从 dll 导出了哪些函数。

添加

#define DLLFUNCTIONEXPOSETEST_EXPORTS 1 
#include DLLTestFile.h

It looks like you don't define DLLFUNCTIONEXPOSETEST_EXPORTS so you use the import declaration . To test use dumpbin /exports to see what functions are exported from the dll.

Add

#define DLLFUNCTIONEXPOSETEST_EXPORTS 1 
#include DLLTestFile.h
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文