C++ Marchal To C# Via Edge - Js
C++ Marchal To C# Via Edge - Js
com/questions/315051/using-a-class-defined-in-a-c-dll-in-c-
sharp-code/315064#315064
I have a dll that was written in c++, I need to use this dll in my c# code.
After searching I found that using P/Invoke would give me access to the function I
need,
but these functions are defined with in a class and use non-static private member
variables.
So I need to be able to create an instance of this class to properly use the
functions.
How can I gain access to this class so that I can create an instance?
I have been unable to find a way to do this.
The basic pattern is that for every member function in class Foo,
create an associated non-member function which calls into the member function.
class Foo {
public:
int Bar();
};
extern "C" Foo* Foo_Create() { return new Foo(); }
extern "C" int Foo_Bar(Foo* pFoo) { return pFoo->Bar(); }
extern "C" void Foo_Delete(Foo* pFoo) { delete pFoo; }
[DllImport("Foo.dll")]
public static extern IntPtr Foo_Create();
[DllImport("Foo.dll")]
public static extern int Foo_Bar(IntPtr value);
[DllImport("Foo.dll")]
public static extern void Foo_Delete(IntPtr value);
The downside is you'll have an awkward IntPtr to pass around but it's a somewhat
simple matter to create a C# wrapper class around this pointer to create a
more usable model.
Even if you don't own this code, you can create another DLL
which wraps the original DLL and provides a small PInvoke layer.
////
http://www.xinterop.com/index.php/category/net/
https://www.xinterop.com/
The statement of "There is no way to directly use a C++ class in C# code"
is not correct. A static C++ function works the same way as C-style function
and the instance functions can be declared as ThisCall calling convention
by adding ThisObject pointing to the instance itself as the first argument.
For details, you may want to read my Blogs. You may also want to try our tool.
(I am the author) – xInterop Apr 11 '14 at 1:09
Though a fine answer at the time, it's arguably better to use c++/CLI so as to
avoid the manual creation of proxy functions - something that could become
quite tedious rather quickly – MickyD Feb 15 '15 at 17:40
###############
Marshal C++ Class and use the PInvoke
###C++ Code ,ClassName.h
};
######C++ Code, ClassName.cpp
CClassName::CClassName()
{
}
CClassName::~CClassName()
{
}
void CClassName::function()
{
std::cout << "Bla bla bla" << std::endl;
#include "ClassName.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#include "ClassNameCaller.h"
CClassName* CreateClassName()
{
return new CClassName();
}
C# code
[DllImport("ClassNameDll.dll")]
static public extern IntPtr CreateClassName();
[DllImport("ClassNameDll.dll")]
static public extern void DisposeClassName(IntPtr pClassNameObject);
[DllImport("ClassNameDll.dll")]
static public extern void CallFunction(IntPtr pClassNameObject);
CallFunction(pClassName);
DisposeClassName(pClassName);
pClassName = IntPtr.Zero;
///#########
////#####
You may need to write an intermediary DLL (in C++, perhaps) that handles
this for you and exposes the interface you need. Your DLL would be in charge
of loading the 3rd party DLL, creating an instance of this C++ object,
and exposing its member functions as needed via whatever API you design.
You would then use P/Invoke to get at your API and cleanly manipulate the object.
Note: For the API of your DLL, try keeping the data types limited to
primitives (long, int, char*, etc.) to prevent module boundary issues.