Bill Walton
Bill Walton

Reputation: 823

Returning pointer to unmanaged class from C++/CLI wrapper which can be imported into C#

I have a C++ class that I need to create several instances of in a C# application. Apparently this means I'll need to make a C++/CLI wrapper as you can't import C++ classes into C#, but I've never used it before. The C++ class inherits from a base class which just contains several pure virtual functions, and no data. The DLL exports just one function which creates a new instance of the class and returns a pointer to the base class.

What C++/CLI type can be used to call that function and get the pointer to the C++ class, but which can also be imported into C#?

Thanks.

Upvotes: 1

Views: 1476

Answers (1)

svick
svick

Reputation: 244767

You don't need any special “C++/CLI type”, you should be able to call that function like from normal C++. But if you want to use the C++ class from C#, you really need to write C++/CLI managed wrapper class that you will be able to use from C#.

The managed wrapper class will contain a filed with a pointer to the unmanaged class. It will also contain the same members as the unmanaged class, that forward to their unmanaged equivalents.

For an example of how to do that, see How to: Wrap Native Class for Use by C# on MSDN.

Upvotes: 1

Related Questions