call C++ dll from C# without name mangling
-
Hi All, I am calling a c++ dll from C# application. with out name mangling how can i call c++ function and class from c# application? Thanks in advance.
G.Paulraj
You are going to have to use Interop(System.Runtime.Interopservices).Something to the tune of :-
[Dllimport("your dll")]
public static extern void functionName(parameters);When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
You are going to have to use Interop(System.Runtime.Interopservices).Something to the tune of :-
[Dllimport("your dll")]
public static extern void functionName(parameters);When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
Hi... thanks for ur quick reply. in .h file i have declared like
__declspec(dllexport) char* BaseClassFunction();
in .CPP file the definition is like
extern "C" __declspec(dllexport) char* __stdcall ISim::BaseClassFunction()
{
static char* pszCPUType = "derived class function called";
return pszCPUType;
}from c# i am calling like following...
const String _dllLocation1 = @"D:\Project\Dll.dll";
[DllImport(_dllLocation1)]
static extern String BaseClassFunction();String Baseclass = BaseClassFunction();
MessageBox.Show(Baseclass.ToString());the above code is not working... any help will be appricated..
G.Paulraj
-
Hi... thanks for ur quick reply. in .h file i have declared like
__declspec(dllexport) char* BaseClassFunction();
in .CPP file the definition is like
extern "C" __declspec(dllexport) char* __stdcall ISim::BaseClassFunction()
{
static char* pszCPUType = "derived class function called";
return pszCPUType;
}from c# i am calling like following...
const String _dllLocation1 = @"D:\Project\Dll.dll";
[DllImport(_dllLocation1)]
static extern String BaseClassFunction();String Baseclass = BaseClassFunction();
MessageBox.Show(Baseclass.ToString());the above code is not working... any help will be appricated..
G.Paulraj
Paulraj G wrote:
the above code is not working.
That is not a very helpful piece of information; exactly what is not working; does the program crash, do you see the wrong results, etc.? You have defined a DLL function as
extern "C" __declspec(dllexport) char* __stdcall ISim::BaseClassFunction()
and I suspect the
ISim::
prefix may be affecting your exported name; you can check with Dependency Walker[^]. Also I am not sure whether achar*
returned from C++ will be accepted as aString
in C#, you should check the PInvoke rules for marshalling, and also this article[^], and this one[^], by Luc Pattyn[^].Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman