0% found this document useful (0 votes)
126 views

C++ Marchal To C# Via Edge - Js

The document discusses how to call methods of a C++ class defined in a DLL from C# code using P/Invoke. It provides a sample C++ code that defines a class and external "caller" functions that create/delete instances of the class and call its methods. It then shows how to declare and call these external functions from C# using DllImport to access the class's methods indirectly through the IntPtr pointer to instances.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

C++ Marchal To C# Via Edge - Js

The document discusses how to call methods of a C++ class defined in a DLL from C# code using P/Invoke. It provides a sample C++ code that defines a class and external "caller" functions that create/delete instances of the class and call its methods. It then shows how to declare and call these external functions from C# using DllImport to access the class's methods indirectly through the IntPtr pointer to instances.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

https://stackoverflow.

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.

There is no way to directly use a C++ class in C# code.


You can use PInvoke in an indirect fashion to access your type.

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; }

Now it's a matter of PInvoking these methods into your C# code

[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

Should the C++ proxy functions be surrounded by extern "C" { .... }


or is it fine to do it purely in C++? – divB Apr 1 '15 at 6:26

###############
Marshal C++ Class and use the PInvoke
###C++ Code ,ClassName.h

class __declspec(dllexport) CClassName


{
public:
CClassName();
~CClassName();
void function();

};
######C++ Code, ClassName.cpp

CClassName::CClassName()
{
}

CClassName::~CClassName()
{
}

void CClassName::function()
{
std::cout << "Bla bla bla" << std::endl;

#####C++ Code, ClassNameCaller.h file for the caller function

#include "ClassName.h"

#ifdef __cplusplus
extern "C" {
#endif

extern __declspec(dllexport) CClassName* CreateClassName();

extern __declspec(dllexport) void DisposeClassName(CClassName* a_pObject);

extern __declspec(dllexport) void function(CClassName* a_pObject);

#ifdef __cplusplus
}
#endif

#####C++ Code, ClassNameCaller.cpp file for the caller function

#include "ClassNameCaller.h"
CClassName* CreateClassName()
{
return new CClassName();
}

void DisposeClassName(CClassName* a_pObject)


{
if(a_pObject!= NULL)
{
delete a_pObject;
a_pObject= NULL;
}
}

void function(CClassName* a_pObject)


{
if(a_pObject!= NULL)
{
a_pObject->function();
}
}

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);

//use the functions


IntPtr pClassName = CreateClassName();

CallFunction(pClassName);

DisposeClassName(pClassName);

pClassName = IntPtr.Zero;

///#########

Here is a sample how to call C++ class method from VB - for C#


you only have to rewrite the sample program in Step 4.
https://www.codeproject.com/Articles/6244/Step-by-Step-Calling-C-DLLs-from-VC-and-
VB-Part-3

////#####
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.

You might also like