I want to export a C++ class, which has a member std::vector<int>. How to export this class so that my C# application can consume it? And how to write the corresponding .Net code?
This is what I tried so far, following the examples given here.
#include <vector>
# define BOOSTGRAPH_API __declspec(dllexport)
# define EXPIMP_TEMPLATE
EXPIMP_TEMPLATE template class BOOSTGRAPH_API std::vector<int>;
class BOOSTGRAPH_API MyClass
{
public:
std::vector<int> VectorOfInts;
public:
bool operator < (const MyClass c) const
{
return VectorOfInts < c. VectorOfInts;
}
bool operator == (const MyClass c) const
{
return VectorOfInts == c. VectorOfInts;
}
};
But then I’m stucked.
What are you trying to do? I see BOOST_GRAPH_API, what are you up to? You will not be able to use native C++ code from .Net directly, neither you should! That would force you to compile all imported dependencies on C++ standard library and boost(!) with clr support…
You need to define interfaces to your classes along with proper factory methods. These can be safely exported and wrapped by C++/CLI objects which you can directly reference within .Net programs:
Observe that only the interface
IMyClassand its creator is exported by the dll, nothing else. In a C++/CLI project, you define a C++/CLI disposable wrapper which holds a pointer toIMyClassand imports only imyclass.h. The wrapper which imprt onlyIMyClassand it’s creator should be similar to something shown below, but take care to get the finalizer semantics right, not sure at the moment what is generated autmatically by the compiler…: