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 に答える