我正在准备一个小的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(选中导出符号)。有人可以为此建议解决方案。在找了很久之后,我一直无法找到问题。
答案 0 :(得分:2)
对我来说很好,完整的文件供参考:
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();
}
}
}
答案 1 :(得分:1)
可能是您的C#进程以64位运行,而您的DLL是32位,反之亦然。当进程和DLL的位数不匹配时,我已经看到了这个问题。
答案 2 :(得分:0)
看起来您没有定义DLLFUNCTIONEXPOSETEST_EXPORTS,因此您使用import声明。测试使用dumpbin / exports以查看从dll导出的函数。
添加
#define DLLFUNCTIONEXPOSETEST_EXPORTS 1
#include DLLTestFile.h