Read property from c++ dll into c# class?

Guapo

My code is reading from a c++ DLL, where one of the properties I need to read Coordinates is a type of Vector3f:

internal struct Entity
{
    public IntPtr Info;
    public IntPtr EntityModel;
    public Vector3f Coordinates;
}

Here is an example of how I read it:

var pAddressOfFunctionToCall = GetProcAddress(GetModuleHandle("Core.dll"), "GetTruckEntity");
var getEntity = (GetEntity)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(GetEntity));
var p = (Entity)Marshal.PtrToStructure(getEntity(), typeof(Entity));
Console.WriteLine(p.Coordinates.X);
Console.WriteLine(p.Coordinates.Y);
Console.WriteLine(p.Coordinates.Z);

The result is always 0 which is wrong so I assume the Class I made has not served as replacement to the one used by the c++.

Further looking into the DLL the Vector3f is as follow:

#ifndef VECTOR3_H
#define VECTOR3_H

#include <math.h>

#define Epsilon 0.00001f
#define EqualWithEpsilon(a,b) (((a) + Epsilon >= (b)) && ((a) - Epsilon <= (b)))
#define NullWithEpsilon(a) (((a) + Epsilon >= 0) && ((a) - Epsilon <= 0))

template <class T>
class Vector3
{
public:     // interface
    //! Default constructor (null vector).
        Vector3() {m_Data[0] = m_Data[1] = m_Data[2] = (T)0;}
        //! Constructor with three different values
        Vector3(T nx, T ny, T nz) {m_Data[0] = nx; m_Data[1] = ny; m_Data[2] = nz;}
        //! Constructor with the same value for all elements
        explicit Vector3(T n) {m_Data[0] = m_Data[1] = m_Data[2] = n;}
        //! Copy constructor
        Vector3(const Vector3<T>& other) {m_Data[0] = other.X(); m_Data[1] = other.Y(); m_Data[2] = other.Z();}

    //
    //  operators

    // operator: indexing
    const T&    operator[] (int i) const    { return m_Data[i]; }
    T&          operator[] (int i)      { return m_Data[i]; }

    // operators: math
    Vector3<T> operator-() const { return Vector3<T>(-X(), -Y(), -Z()); }

    Vector3<T>& operator=(const Vector3<T>& other) { X() = other.X(); Y() = other.Y(); Z() = other.Z(); return *this; }

    Vector3<T> operator+(const Vector3<T>& other) const { return Vector3<T>(X() + other.X(), Y() + other.Y(), Z() + other.Z()); }
    Vector3<T>& operator+=(const Vector3<T>& other) { X()+=other.X(); Y()+=other.Y(); Z()+=other.Z(); return *this; }
    Vector3<T> operator+(const T val) const { return Vector3<T>(X() + val, Y() + val, Z() + val); }
    Vector3<T>& operator+=(const T val) { X()+=val; Y()+=val; Z()+=val; return *this; }

    Vector3<T> operator-(const Vector3<T>& other) const { return Vector3<T>(X() - other.X(), Y() - other.Y(), Z() - other.Z()); }
    Vector3<T>& operator-=(const Vector3<T>& other) { X()-=other.X(); Y()-=other.Y(); Z()-=other.Z(); return *this; }
    Vector3<T> operator-(const T val) const { return Vector3<T>(X() - val, Y() - val, Z() - val); }
    Vector3<T>& operator-=(const T val) { X()-=val; Y()-=val; Z()-=val; return *this; }

    Vector3<T> operator*(const Vector3<T>& other) const { return Vector3<T>(X() * other.X(), Y() * other.Y(), Z() * other.Z()); }
    Vector3<T>& operator*=(const Vector3<T>& other) { X()*=other.X(); Y()*=other.Y(); Z()*=other.Z(); return *this; }
    Vector3<T> operator*(const T v) const { return Vector3<T>(X() * v, Y() * v, Z() * v); }
    Vector3<T>& operator*=(const T v) { X()*=v; Y()*=v; Z()*=v; return *this; }

    Vector3<T> operator/(const Vector3<T>& other) const { return Vector3<T>(X() / other.X(), Y() / other.Y(), Z() / other.Z()); }
    Vector3<T>& operator/=(const Vector3<T>& other) { X()/=other.X(); Y()/=other.Y(); Z()/=other.Z(); return *this; }
    Vector3<T> operator/(const T v) const { T i=(T)1.0/v; return Vector3<T>(X() * i, Y() * i, Z() * i); }
    Vector3<T>& operator/=(const T v) { T i=(T)1.0/v; X()*=i; Y()*=i; Z()*=i; return *this; }

    // sort in order X, Y, Z. Equality with rounding tolerance.
    bool operator<=(const Vector3<T>& other) const
    {
        return  (X()<other.X() || EqualWithEpsilon(X(), other.X())) ||
            (EqualWithEpsilon(X(), other.X()) && (Y()<other.Y() || EqualWithEpsilon(Y(), other.Y()))) ||
            (EqualWithEpsilon(X(), other.X()) && EqualWithEpsilon(Y(), other.Y()) && (Z()<other.Z() || EqualWithEpsilon(Z(), other.Z())));
    }

    // sort in order X, Y, Z. Equality with rounding tolerance.
    bool operator>=(const Vector3<T>&other) const
    {
        return  (X()>other.X() || EqualWithEpsilon(X(), other.X())) ||
            (EqualWithEpsilon(X(), other.X()) && (Y()>other.Y() || EqualWithEpsilon(Y(), other.Y()))) ||
            (EqualWithEpsilon(X(), other.X()) && EqualWithEpsilon(Y(), other.Y()) && (Z()>other.Z() || EqualWithEpsilon(Z(), other.Z())));
    }

    // sort in order X, Y, Z. Difference must be above rounding tolerance.
    bool operator<(const Vector3<T>&other) const
    {
        return  (X()<other.X() && !EqualWithEpsilon(X(), other.X())) ||
            (EqualWithEpsilon(X(), other.X()) && Y()<other.Y() && !EqualWithEpsilon(Y(), other.Y())) ||
            (EqualWithEpsilon(X(), other.X()) && EqualWithEpsilon(Y(), other.Y()) && Z()<other.Z() && !EqualWithEpsilon(Z(), other.Z()));
    }

    // sort in order X, Y, Z. Difference must be above rounding tolerance.
    bool operator>(const Vector3<T>&other) const
    {
        return  (X()>other.X() && !EqualWithEpsilon(X(), other.X())) ||
            (EqualWithEpsilon(X(), other.X()) && Y()>other.Y() && !EqualWithEpsilon(Y(), other.Y())) ||
            (EqualWithEpsilon(X(), other.X()) && EqualWithEpsilon(Y(), other.Y()) && Z()>other.Z() && !EqualWithEpsilon(Z(), other.Z()));
    }

    // use weak float compare
    bool operator==(const Vector3<T>& other) const
    {
        return this->equals(other);
    }

    bool operator!=(const Vector3<T>& other) const
    {
        return !this->equals(other);
    }

    // functions

    //! returns if this vector equals the other one, taking floating point rounding errors into account
    bool equals(const Vector3<T>& other) const
    {
        return EqualWithEpsilon(X(), other.X()) &&
            EqualWithEpsilon(Y(), other.Y()) &&
            EqualWithEpsilon(Z(), other.Z());
    }

    //
    // named functions

    // Set Value
    void Set(T x, T y, T z);

    // return length of vector
    float       GetLength() const;

    // return length 2D of vector
    float       GetLength2() const;

    // return length of vector squared
    float       GetSquareLength() const;

    // return length 2D of vector squared
    float       GetSquareLength2() const;

    // normalize a vector
    void        Normalize();

    // perform dot product
    T           Dot(const Vector3<T>&) const;

    // perform cross product(same as operator*=)
    Vector3<T>   Cross(const Vector3<T>&) const;

    // accessor functions
    T&          X()         { return m_Data[0]; }
    const T&    X() const   { return m_Data[0]; }
    T&          Y()         { return m_Data[1]; }
    const T&    Y() const   { return m_Data[1]; }
    T&          Z()         { return m_Data[2]; }
    const T&    Z() const   { return m_Data[2]; }

    const T*    GetData() const    { return m_Data; }

    // static usefull methods
    static const Vector3<T>& GetZero() {return ms_Zero;}
    static const Vector3<T>& GetBaseI() {return ms_BaseI;}
    static const Vector3<T>& GetBaseJ() {return ms_BaseJ;}
    static const Vector3<T>& GetBaseK() {return ms_BaseK;}

private:
    T m_Data[3];

    // static usefull vectors
    static Vector3<T> ms_Zero;
    static Vector3<T> ms_BaseI;
    static Vector3<T> ms_BaseJ;
    static Vector3<T> ms_BaseK;
};
typedef Vector3<float> Vector3f;

template <class T> Vector3<T> Vector3<T>::ms_Zero = Vector3<T>((T)0);
template <class T> Vector3<T> Vector3<T>::ms_BaseI = Vector3<T>((T)1, (T)0, (T)0);
template <class T> Vector3<T> Vector3<T>::ms_BaseJ = Vector3<T>((T)0, (T)1, (T)0);
template <class T> Vector3<T> Vector3<T>::ms_BaseK = Vector3<T>((T)0, (T)0, (T)1);

template <class T> inline T
Vector3<T>::Dot(const Vector3<T>& v) const
{
    return (T)(X() * v.X() + Y() * v.Y() + Z() * v.Z());
}

template <class T> inline Vector3<T>
Vector3<T>::Cross(const Vector3<T>& v) const
{
    return Vector3<T> (  Y() * v.Z() - Z() * v.Y(),
                          -(X() * v.Z() - Z() * v.X()),
                          X() * v.Y() - Y() * v.X());
}

template <class T> inline void
Vector3<T>::Set(T x, T y, T z) 
{
    m_Data[0] = x;
    m_Data[1] = y;
    m_Data[2] = z;
}

template <class T> inline float
Vector3<T>::GetLength() const
{
    return ::sqrt(GetSquareLength());
}


template <class T> inline float
Vector3<T>::GetSquareLength() const
{
    return (X() * X() + Y() * Y() + Z() * Z());
}

template <class T> inline float
Vector3<T>::GetLength2() const
{
    return ::sqrt(GetSquareLength2());
}


template <class T> inline float
Vector3<T>::GetSquareLength2() const
{
    return (X() * X() + Y() * Y());
}

template <class T> inline void
Vector3<T>::Normalize()
{
    float len = GetLength();

    if (len != 0) 
    {
        float f = 1.0f / len;

        *this *= f;
    }
}

#endif // VECTOR3_H

I've attempted to write a basic version of Vector3f as such just in a foul attempt to retrieve the X,Y,Z:

public struct Vector3f
{
    public Vector3f(float x, float y, float z)
    {
        X = x;
        Y = y;
        Z = z;
    }

    public float X;
    public float Y;
    public float Z;
}

But of course that doesn't seem to work.

  • How can I read the Coordinates into my application?

I've looked around but couldn't find how to make it work for a property with a class like that, I can read the other data from it but not Vector3f.

thepirat000

If you decide to go for the C++/CLI solution, this can probably help. What I suggest to do is:

  1. Create a common assembly in C#, containing the managed Vector3f public class.
  2. Create a C++/CLI wrapper assembly, and add a reference to both: the new common assembly and the unmanaged C++ library. Add code to convert an unmanaged Vector3 pointer to an instance of a managed Vector3f.
  3. In your existing managed assembly, add a reference to the two assemblies previously created, and use the wrapper to convert the vector.

Point 1 is trivial, for the point 2, your C++/CLI code should look like:

// Marshaler.h (in Marshaler.dll)
#include "OriginalVector3f.h"
#pragma once

using namespace System;

namespace UnmanagedNamespace {
    using namespace ManagedNamespace::Common;
    public ref class Marshaler
    {
    private:
    public:
        static Vector3f^ MarshalVector(IntPtr vectorPtr)
        {
            // Cast the IntPtr to the unmanaged pointer
            Vector3* unmanaged = static_cast<Vector3*>(vectorPtr.ToPointer());
            // Create a new managed object
            Vector3f^ managed = gcnew Vector3f();
            // Map  info
            managed->X = unmanaged->X;
            managed->Y = unmanaged->Y;
            managed->Z = unmanaged->Z;
            return managed;
        }
    };
}

Here I'm assuming the following:

  • The unmanaged Vector3 is defined on file "OriginalVector3f.h".
  • The managed Vector3f is on the namespace ManagedNamespace.Common

For the point 3, you should create a class implementing ICustomMarshaler, as shown here, but for simplicity, start by manually calling the MarshalVector method. For example:

ManagedNamespace.Common.Vector3f vector = UnmanagedNamespace.Marshaler.MarshalVector(Entity.Coordinates)

Assuming Entity.Coordinates is an IntPtr.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Read Vector Data from C++ dll?

From Dev

Read property from QML singleton with C++

From Dev

Call class member of c++ DLL from c#

From Dev

Access property from outside a class in objective c

From Dev

Read class objects from file c++

From Dev

C++ How to export a static class member from a dll?

From Dev

Inhereting a virtual class interface from a DLL (C++)

From Dev

C# Get enum in class from dll by its name

From Dev

Export C++ class from main program to DLL

From Dev

IronPython get to internal class from C# dll

From Dev

Qt / QML set property from c++ class for GridView

From Dev

Access anonymous object property from static class c#

From Dev

Objective-C Set Property from Another Class Returns Null

From Dev

Qt / QML set property from c++ class for GridView

From Dev

override values of a property from base class in C#

From Dev

Pass structure (or class) from C++ dll to C# (Unity 3D)

From Dev

Pass structure (or class) from C++ dll to C# (Unity 3D)

From Dev

c++ - base class as property

From Dev

C# Class with generic property

From Dev

Add Item from another class to a property from another class. Objective-C

From Dev

Add Item from another class to a property from another class. Objective-C

From Dev

What is a thread-safe way to read/write a C# property in a class?

From Dev

read socket from C

From Dev

read socket from C

From Dev

Debug C++ dll from C#

From Dev

C# import a structure from C DLL

From Dev

c# callback from C DLL with char *

From Dev

Load Managed C++ Dll from Unmanaged C Dll?

From Dev

P/invoke DLL functions from C++ dll in C#