0
votes

I have a unmanaged (i.e. not ref class)class in c++/CLI implementing an interface in c++/CLI. Both are in the same file Wrapper.h I want to export only the interface and access the method implementation (of the derived class ) in the native c++. I looked for this situation in two books Foundations of C++/CLI by Gordon Hogenson and C++/CLI in ACtion By Nishant Shivakumar Sir. Any workaround for this?

EDIT Below is my Interface in c++/CLI (Wrapper.h). The CSharp (c#)project contains the managed interface ICalculateArea and the class Circle implementing it. FYI...the C# project is being built successfully

 #ifdef MANAGED_EXPORT
using namespace System;
using namespace CSharp;
#endif
#ifdef MANAGED_EXPORT
#define DLLAPI  __declspec(dllexport)
#else
#define DLLAPI  __declspec(dllimport)
#pragma comment(lib,"PATH.lib")
#endif



interface class IWrapper
{
  double CalculateArea(double value);
};

and a ref class implementing this interface is as follows::(Wrapper.cpp)

#include"Wrapper.h"
using namespace CSharp;
   ref class UnmanagedCircle:IUnmanagedCalculateArea
 {

 public:
     virtual double CalculateAreaWrapper()
    {
      ICalculateArea^ getArea=dynamic_cast<ICalculateArea^>(gcnew Circle) ;
      return getArea->CalculateArea();
    }
    virtual double GetRadiusWrapper() 
    {
        Circle^ circle=gcnew Circle();
        return circle->Radius;
    }
    virtual void SetRadiusWrapper(double value )
    {
        Circle^ circle=gcnew Circle();
        circle->Radius=value;
    }
 };

Now I want to access the CalculateArea method in a different native c++ project?

1
please show some code describing what you want to do excatly, your question is not really clear.stijn
Have you any issue? Because what you describe should be possible.Pragmateek
Create a native class that wraps the ref class.David Heffernan
Or simply implement CalculateArea in c++stijn
@David HefferMan any examples...about how to do that? and where should i make that in Wrapper.h, Wrapper.cpp or in NativeCpluplus.cpp?Pratik Pattanayak

1 Answers

0
votes

I think you don't need the C++/CLI ref class layer. You can write a native class that wraps the managed objects directly. For example:

class UnmanagedCircle
{
public:
    static double CalculateAreaWrapper()
    {
        ICalculateArea^ getArea=dynamic_cast<ICalculateArea^>(gcnew Circle);
        return getArea->CalculateArea();
    }

    static double GetRadiusWrapper() 
    {
        Circle^ circle=gcnew Circle();
        return circle->Radius;
    }

    static void SetRadiusWrapper(double value)
    {
        Circle^ circle=gcnew Circle();
        circle->Radius=value;
    }
};