2

我的 C++ 应用程序中有这个结构:

struct Animation
{
    UINT To;
    UINT From;
    USHORT AnimationID;
};

struct Entity
{
    USHORT X, Y;
    UINT SERIAL;
    USHORT SpriteID;
    BYTE DIRECTION;
    USHORT TYPE;
    BOOL Cursed;
    BOOL Fased;
    BOOL IsPet;

    void AddAnimation(Animation &const  a)
    {
        Animations.push_back(a);
    }

    void ClearAnimations()
    {
        this->Animations.clear();
    }

private:
    vector<Animation> Animations;
};

我有这个导出的功能:

extern "C" __declspec(dllexport) Entity GetNearest(void)
{
    Entity & result = GetNearestEntity();
    return result;
}

是否可以使用它并在 c# 中获取实体值?

我试图这样做:

  [DllImport("FatBoy.dll", SetLastError = true)]
        internal static extern Entity GetNearest();

        public struct Entity
        {
            ushort X, Y;
            uint SERIAL;
            ushort SpriteID;
            byte DIRECTION;
            ushort TYPE;
            bool Cursed;
            bool Fased;
            bool IsPet;
        }

当我调用它时,我得到了错误:

Method's type signature is not PInvoke compatible.
4

1 回答 1

5

C++ 代码:

struct EntityValue
{
    USHORT X, Y;
    UINT SERIAL;
    USHORT SpriteID;
    BYTE DIRECTION;
    USHORT TYPE;
    BOOL Cursed;
    BOOL Fased;
    BOOL IsPet;
};

EntityValue* CopyValueOfEntity(Entity* source)
{
    EntityValue *result = new EntityValue();
    result->X = source->X;
    result->Y = source->Y;
    result->SERIAL = source->SERIAL;
    result->SpriteID = source->SpriteID;
    result->DIRECTION = source->DIRECTION;
    result->TYPE = source->TYPE;
    result->Cursed = source->Cursed;
    result->Fased = source->Fased;
    result->IsPet = source->IsPet;
    return result;
}

extern "C" __declspec(dllexport) EntityValue* GetNearest(void)
{
    Entity *result = GetNearestEntity();
    result->X = 1;
    result->Y = 2;
    result->SERIAL = 3;
    result->SpriteID = 4;
    result->DIRECTION = 5;
    result->TYPE = 6;
    result->Cursed = FALSE;
    result->Fased = FALSE;
    result->IsPet = FALSE;
    return CopyValueOfEntity(result);
}

C#代码:

class Program
{
    [DllImport("FatBoy.dll", SetLastError = true)]
    internal static extern IntPtr GetNearest();

    [StructLayout(LayoutKind.Sequential)]
    unsafe struct EntityValue
    {
        public ushort X, Y;
        public uint SERIAL;
        public ushort SpriteID;
        public byte DIRECTION;
        public ushort TYPE;
        public bool Cursed;
        public bool Fased;
        public bool IsPet;
    }

    static void Main(string[] args)
    {
        unsafe
        {
            EntityValue* data = (EntityValue*)GetNearest();

            Console.WriteLine(data->X);
            Console.WriteLine(data->Y);
            Console.WriteLine(data->SERIAL);
            Console.WriteLine(data->SpriteID);
            Console.WriteLine(data->DIRECTION);
            Console.WriteLine(data->TYPE);
            Console.WriteLine(data->Cursed);
            Console.WriteLine(data->Fased);
            Console.WriteLine(data->Fased);
        }
    }
}

结果如下:

证明

并且与本机 DLL 集成会很有用。

于 2012-12-29T07:11:46.460 回答