VC++ DLL throws error when call it from C#[EntryPointNotFound]
-
Here i created in DLL project in vc++ 2008. Following are two code files lib.h and lib.cpp. lib.h
#include "stdafx.h";
class \_\_declspec(dllexport) test { public: test(); static void hello(); static void hello1(); }; class \_\_declspec(dllexport) test1 { public: test1(); void hello\_test1(); void hello1\_test1(); };
lib.cpp
#include "stdafx.h"
#include "lib.h"
#include void test::hello()
{
printf("Hello");
}void test::hello1() { printf("Hello1"); } void test1::hello\_test1() { printf("Hello\_test1"); } void test1::hello1\_test1() { printf("Hello1\_test1"); }
stdafx.h
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN
// Windows Header Files:
#includedllMain.cpp
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}I have written C# code to call the method of test and test1 classes: consoleApp
[DllImport("lib.dll" )]
public static extern void hello();\[DllImport("lib.dll")\] public static extern void hello1(); \[DllImport("lib.dll")\] public static extern void hello\_test1(); \[DllImport("lib.dll")\] public static extern void hello1\_test1();
static void Main()
{
hello();
hello1();
hello_test1();
hello1_test1();
Console.ReadKey();
}when i run above code i have got following error: EntryPointNotFoundException: Unable to find an entry point named 'hello' in DLL 'lib.dll' 1****<-Click 1 for image I know about how to call function only(without using Class) of vc++ DLL from C# but i don't know how to call method of any class and how to code that in proper way in vc++. I know somewhere is mistake in my above code, please experts guide me about my mistake because i tried all from my side. If anyone has full example like above then suggest me. Thanks in advance..
-
Here i created in DLL project in vc++ 2008. Following are two code files lib.h and lib.cpp. lib.h
#include "stdafx.h";
class \_\_declspec(dllexport) test { public: test(); static void hello(); static void hello1(); }; class \_\_declspec(dllexport) test1 { public: test1(); void hello\_test1(); void hello1\_test1(); };
lib.cpp
#include "stdafx.h"
#include "lib.h"
#include void test::hello()
{
printf("Hello");
}void test::hello1() { printf("Hello1"); } void test1::hello\_test1() { printf("Hello\_test1"); } void test1::hello1\_test1() { printf("Hello1\_test1"); }
stdafx.h
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN
// Windows Header Files:
#includedllMain.cpp
#include "stdafx.h"
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}I have written C# code to call the method of test and test1 classes: consoleApp
[DllImport("lib.dll" )]
public static extern void hello();\[DllImport("lib.dll")\] public static extern void hello1(); \[DllImport("lib.dll")\] public static extern void hello\_test1(); \[DllImport("lib.dll")\] public static extern void hello1\_test1();
static void Main()
{
hello();
hello1();
hello_test1();
hello1_test1();
Console.ReadKey();
}when i run above code i have got following error: EntryPointNotFoundException: Unable to find an entry point named 'hello' in DLL 'lib.dll' 1****<-Click 1 for image I know about how to call function only(without using Class) of vc++ DLL from C# but i don't know how to call method of any class and how to code that in proper way in vc++. I know somewhere is mistake in my above code, please experts guide me about my mistake because i tried all from my side. If anyone has full example like above then suggest me. Thanks in advance..
There are two main issues to deal with here. 1. If you want to call functions like that in a C++ DLL, you shouldn't put them inside classes. 2. There is an issue called C++ Name Mangling. This is where the linker exports the functions with additional characters that describe the function signature. To call the functions by their names directly, you must place the definitions inside an "extern C" declaration like so:
extern "C"
{
void Hello()
{ printf("Hello");}void Hello1() { printf("Hello 1");}
}
The difficult we do right away... ...the impossible takes slightly longer.