Search code examples
c++c++-cliwrapperlabview

c++\CLI dll wrapper to call virtual members in c++ class


I'm trying to write a dll wrapper in c++/CLI to connect functions in my native c++ class to be used in LabView. I've successfully written a wrapper that does this and everything works as expected (see below). The Problem is explained at the bottom (after Labview image).

Native c++ header file.

//native c++ header
#pragma once

namespace AddTwoNumbersLib
{
class AddClass
{
private:
    double _x;
    double _y;
    bool _test;

public:
    //Constructor
    AddClass();

    //Member functions
    virtual bool toggle (bool test);
    double Add(double x, double y);
    static double subtract(double z, double a);
};
}

Native c++ .cpp file.

// native c++ .cpp
#include "stdafx.h"
#include "AddClass.h"

namespace AddTwoNumbersLib
{
//Constructor
AddClass::AddClass()
{}

//Member functions
double AddClass::Add(double x, double y)
{
    _x = x;
    _y = y;
    return _x + _y;
}

double AddClass::subtract(double z, double a) 
{
    return z - a;
}

bool AddClass::toggle(bool test)
{
    _test = test;
    return _test;
}
}

Wrapper c++/CLi header file.

// ClrWrapper.h

#pragma once

using namespace System;

namespace ClrWrapper {

public ref class MathWrap
{
private:
    AddTwoNumbersLib::AddClass *_MyManagedAdd;
    AddTwoNumbersLib::AddClass *_push;

public:
    //Constructs an instance of the class and an
    //instance of the underliying native class AddClass
    MathWrap();
    ~MathWrap();

    double AddWrapper(double x, double y);
    double subWrap(double z, double a);
    bool toggleWrap(bool push);
};
}

Wrapper c++/CLI .cpp file

// This is the main DLL file.

#include "stdafx.h"

#include "ClrWrapper.h"

using namespace ClrWrapper;

MathWrap::MathWrap()
{
_MyManagedAdd = new AddTwoNumbersLib::AddClass();
_push = new AddTwoNumbersLib::AddClass();
}

MathWrap::~MathWrap()
{
delete _MyManagedAdd;
}

double MathWrap::AddWrapper(double x, double y)
{
return _MyManagedAdd->Add(x, y);
//return AddTwoNumbersLib::AddClass::Add(x, y);
}

double MathWrap::subWrap(double z, double a)
{
return AddTwoNumbersLib::AddClass::subtract(z, a);
}

bool MathWrap::toggleWrap(bool push)
{
return _push->toggle(push);
}

LabView results:

enter image description here

The Problem

when I change the header file to a a pure virtual My code no longer works. It's because I'm trying to instantiate (from a now abstract class) in the wrapper .cpp. I would really appreciate some help in a work around for this problem.

virtual bool toggle (bool test) = 0;

Solution

    1. The Pointer to the native class AddClass may be a pointer to a pure virtual class/interface, or may contain functions that are pure virtual.
    2. The class you create with new (derived from AddClass), must have all functions implemented!
    3. So you have the chance to use different derived classes from AddClass with different behaviours. But each derived class must have all virtual functions implemented!

    This doesn't matter if you have C++ or C++/CLI.