c# - 从返回结构的 C++ 导入函数

标签 c#

我的 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.

最佳答案

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

结果如下:

Demonstrate

Integrating with Native DLLs会有用的。

关于c# - 从返回结构的 C++ 导入函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14079261/

相关文章:

c# 和 LINQ,其中集合成员的属性名称是通过函数传递的

c# - Visual Studio 扩展包是否支持异步操作

c# - Mono PrivateFontCollection.AddFontFile 错误的解决方法

c# - linux wine Word 互操作 application.Documents.Open 在 word 中打开文档但不将文档返回给客户端

c# - 已经有一个打开的 DataReader 与此命令关联,必须先在 C# 中将其关闭

c# - 为什么 C# 编译器重载解析算法将具有相同签名的静态成员和实例成员视为相等?

c# - Linq:对列表进行分组,对其进行排序并获得最高的 x 值?

JavaScript 代码并不总是有效

c# - 在代码隐藏中将参数传递给 RelayCommand

C# XSLT 快速转换大型 XML 文件