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

本文关键字:类中 属性 读取 c++ dll | 更新日期: 2023-09-27 18:03:08

我的代码是从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。

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

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

    在c#中创建一个公共程序集,包含托管Vector3f公共类。
  1. 创建一个c++/CLI包装程序集,并添加对两者的引用:新的通用程序集和非托管c++库。添加代码将非托管Vector3指针转换为托管Vector3f的实例。
  2. 在现有的托管程序集中,添加对先前创建的两个程序集的引用,并使用包装器转换矢量。

第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被定义在文件"OriginalVector3f.h"。
  • 托管的Vector3f在命名空间ManagedNamespace上。常见的

对于第3点,您应该创建一个实现ICustomMarshaler的类,如下所示,但是为了简单起见,从手动调用MarshalVector方法开始。例如:

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

假设的实体。坐标是IntPtr