从c ++ dll读取属性到c#类?

时间:2015-07-16 00:46:25

标签: c#

我的代码是从c ++ DLL读取的,其中我需要阅读的Coordinates属性之一是Vector3f的类型:

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

以下是我如何阅读它的示例:

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);

结果始终为0,这是错误的,所以我假设我制作的类没有替代c ++使用的那个。

进一步研究DLL,Vector3f如下:

#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

我试图写一个Vector3f的基本版本,只是在试图找回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;
}

但当然这似乎不起作用。

  • 如何将坐标读入我的应用程序?

我环顾四周,但无法找到如何使其适用于具有类似类别的属性,我可以从中读取其他数据,但不能读取Vector3f。

1 个答案:

答案 0 :(得分:2)

如果您决定使用C ++ / CLI解决方案,这可能会有所帮助。 我建议做的是:

  1. 在C#中创建一个包含托管Vector3f的公共程序集 公共课。
  2. 创建一个C ++ / CLI包装程序集,并添加对两者的引用:新的通用程序集和非托管的C ++库。添加代码以将非托管Vector3指针转换为托管Vector3f的实例。
  3. 在现有的托管程序集中,添加对先前创建的两个程序集的引用,并使用包装器转换向量。
  4. 第1点是微不足道的,对于第2点,您的C ++ / CLI代码应如下所示:

    // 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;
            }
        };
    }
    

    我在此假设以下内容:

    • 非托管Vector3在文件&#34; OriginalVector3f.h&#34;上定义。
    • 托管的Vector3f位于命名空间ManagedNamespace.Common

    对于第3点,您应该创建一个实现ICustomMarshaler的类,如图here所示,但为简单起见,首先手动调用MarshalVector方法。 例如:

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

    假设Entity.Coordinates是IntPtr。