I'm trying to create a simple MFC DLL, exports its functions and consume in a C# .net project In the code below I have two methods in the DLL, I have taken the suggestions of the posters to the original question. I'm reposting the current code, I get the error
{ "Unable to find an entry point named '_Worker_Create@0'in DLL 'C:\Users\mspath\Documents\MDS_SIMCA\DevSIMCA_Dll\TestDLL\Debug\MFClib.dll'.":"" }
I have used the dumpbin.exe to get the correct mangled names.
MFC DLL .cpp
#include "stdafx.h"
#include "Worker.h"
Worker::Worker(void)
{
}
Worker::~Worker(void)
{
}
int Worker::GetInteger()
{
return 44;
}
int Worker::DoMath(int iOne, int iTwo)
{
return iOne * iTwo;
}
MFC DLL .h
class Worker { public: Worker(void); ~Worker(void);
static int GetInteger ();
static int DoMath (int iOne, int iTwo);
};
MFCLib.cpp
extern "C"
{
__declspec(dllexport) void * __stdcall Worker_Create()
{
return new Worker();
}
__declspec(dllexport) void * __stdcall Worker_Destroy(Worker * instance)
{
delete instance;
instance = 0;
return instance;
}
__declspec(dllexport) int __stdcall Worker_DoMath(Worker * instance, int i, int j)
{
return instance->DoMath(i, j);
}
}
C# .net
public partial class MainWindow : Window
{
/*
* from dumbin.exe /exports:
*
1 0 00011519 _Worker_Create@0 = @ILT+1300(_Worker_Create@0)
2 1 00011230 _Worker_Destroy@4 = @ILT+555(_Worker_Destroy@4)
3 2 000110D2 _Worker_DoMath@12 = @ILT+205(_Worker_DoMath@12)
*/
[DllImport("C:\\Users\\mspath\\Documents\\MDS_SIMCA\\DevSIMCA_Dll\\TestDLL\\Debug\\MFClib.dll",
EntryPoint = "_Worker_Create@0",
ExactSpelling = false,
CallingConvention = CallingConvention.StdCall)]
public extern static IntPtr Worker_Create();
[DllImport("C:\\Users\\mspath\\Documents\\MDS_SIMCA\\DevSIMCA_Dll\\TestDLL\\Debug\\MFClib.dll",
EntryPoint = "_Worker_Destroy@4",
CallingConvention = CallingConvention.StdCall)]
public extern static IntPtr Worker_Destroy(IntPtr iptr);
[DllImport("C:\\Users\\mspath\\Documents\\MDS_SIMCA\\DevSIMCA_Dll\\TestDLL\\Debug\\MFClib.dll",
EntryPoint = "_Worker_DoMath@12",
CallingConvention = CallingConvention.StdCall)]
public extern static int Worker_DoMath(IntPtr instance, int i, int j);
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
IntPtr instance = Worker_Create();
int i = Worker_DoMath(instance, 4, 2);
Worker_Destroy(instance);
}
}
GetIntegerandDoMathare instance functions of the classWorker, the easiest way around this is to create a non-instance function in the DLL that creates the instance and returns the result, otherwise you have to figure out a way to create an instance ofWorkerand then you can run those functions on it. As it stands, they are not entry points in the DLL and can't be individually run. - Ron Beyer/clris the compiler command line option to compile C++/CLI, not a project type. - IInspectable