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.
Works fine for me, complete files for reference:
dllmain.cpp:
DLL.h:
Program.cs (Win32 Console Application for simplicity):