C# System.EntryNotFoundException:无法在DLL中找到入口点 我正在准备一个小的C++ DLL,其中的函数将由C→< /P>调用。

C# System.EntryNotFoundException:无法在DLL中找到入口点 我正在准备一个小的C++ DLL,其中的函数将由C→< /P>调用。,c#,c++,wpf,visual-c++,C#,C++,Wpf,Visual 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.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中找到入口点”


我使用VisualStudio提供的默认模板,创建新项目时,VisualC++(>Win32 Projest->DLL)(导出符号已选中)。有人能为这件事提出解决办法吗。即使查找了很长时间,我仍然无法找到问题。

看起来您没有定义DLLFUNCTIONEXPOSETEST\u导出,因此使用导入声明。要进行测试,请使用dumpbin/exports查看从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控制台应用程序):


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

DLLFUNCTIONEXPOSETEST\u导出由Visual Studio为DLL项目自动定义。刚刚测试了上面的代码,效果很好。这是我的Dll上Dumpbin.exe工具的屏幕截图@工作正常吗?我在这里遇到的入口点错误呢?你的名字被弄错了。这意味着外部“C”有点问题part@rerun:他的Fnsumoftwo数字在我看来没有损坏?仍然无法找到我的文件中缺少的内容。捕获异常的屏幕截图:这是DllNotFoundException,像我一样使用完整路径进行测试。#FML。对另外,如果必须在上面没有完整路径的情况下保存DLL,那么您实际将DLL保存在哪里?例如,在与应用程序相同的文件夹中。或者相对于该路径,并使用相对路径。
// 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);
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();
        }
    }
}