System.EntryNotFoundException:在DLL中找不到入口点
本文关键字:找不到 入口 DLL EntryNotFoundException System | 更新日期: 2023-09-27 18:24:59
我正在准备一个小的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(已选中导出符号)。有人能提出解决这个问题的办法吗。我找了很久也没能找到问题。
对我来说很好,完整的文件供参考:
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();
}
}
}
可能是您的C#进程以64位运行,而DLL是32位,反之亦然。当进程和DLL的位不匹配时,我看到过这个问题。
看起来您没有定义DLLFUNCTIONEXPOSETEST_EXPORTS,所以您使用导入声明。要进行测试,请使用dumpbin/exports查看从dll导出了哪些函数。
添加
#define DLLFUNCTIONEXPOSETEST_EXPORTS 1
#include DLLTestFile.h