封送结构字段,它是指向结构数组的指针

时间:2015-01-09 11:59:14

标签: c# arrays pinvoke marshalling

我花了一些时间思考这个问题,我需要你的帮助。 我的问题看起来有点类似于Stack Overflow上的许多问题,我浏览了很多,但仍未找到答案。

我需要编组结构字段,它是指向结构数组指针。 问题是我需要这个结构从用C编写的遗留外部库接收。 通过使用Marshal.AllocHGlobalMarshal.StructureToPtr锁定大小正确的内存,我发现了很多关于传递结构作为指针的提示。 我还找到了如何接收作为指针返回的结构的指南:您可以使用[MarshalAs]修饰符。 不幸的是,这无济于事,因为大多数[MarshalAs]修饰符似乎不适用于结构字段。

在下面的示例中,我能够通过两个步骤接收所需的数据:

  1. 接收MiddleStruct数据及其中的指针
  2. 使用PtrToStructure
  3. 读取指向数据

    问题是我想确保垃圾收集器不会修改接收指针引用的内存。我有什么方法可以使用一步读取InnerStructure中的数据吗?

    C结构

    typedef struct OuterStruct {
        MiddleStruct mStruct;
    } OuterStruct;
    
    typedef struct MiddleStruct {
        int count;
        InnerStruct FAR *innerData;
    } MiddleStruct;
    
    typedef struct InnerStruct {
        int number;
        char data[64];
    } InnerStruct;
    

    C#结构

    [StructLayout(LayoutKind.Sequential, Pack = 4)]
    public class OuterStruct
    {
        public MiddleStruct mStruct;
    };
    
    [StructLayout(LayoutKind.Sequential, Pack = 4)]
    public struct MiddleStruct
    {
        public int count;
        public IntPtr pIStruct; //pointer to array of InnerStruct
    }
    
    [StructLayout(LayoutKind.Sequential, Pack = 4)]
    public struct InnerStruct
    {
        int number;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
        public bytes[] data;
    };
    

    功能

    [DllImport("legacy.dll", CharSet = CharSet.Ansi)]
    public static extern short getData([In, Out, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(myCustomMarshaler))] OuterStruct sStruct);
    

    实施例

    OuterStruct oStruct;
    getData(out oStruct);
    int count = oStruct.mStruct.count;
    IntPtr pIStruct = oStruct.mStruct.pIStruct;
    InnerStruct[] iStructArray = new InnerStruct[count];
    int infoSize = Marshal.SizeOf(new iStructArray());
    for (int i = 0; i < count; i++)
    {
        IntPtr targetPtr = new IntPtr(pIStruct.ToInt32() + infoSize * i);
        iStructArray[i] = (InnerStruct)Marshal.PtrToStructure(targetPtr, typeof(InnerStruct));
    }
    WriteLine(iStructArray);
    

    更新

    我已经创建了自定义编组程序,但我想不出返回值的方法。 知道该数组是引用类型,有没有办法将intPtr转换为数组? 我尝试使用object而不是intPtr并相应地转换它,但是没有用。

    public class myCustomMarshaler : ICustomMarshaler
    {
        [ThreadStatic]
        private OuterStruct marshaledObj;
    
        private static myCustomMarshaler marshaler = null;
        public static ICustomMarshaler GetInstance(string cookie)
        {
            if (marshaler == null)
            {
                marshaler = new myCustomMarshaler();
            }
            return marshaler;
        }
    
        #region ICustomMarshaler Members
        public int GetNativeDataSize()
        {
            return Marshal.SizeOf(typeof(OuterStruct));
        }
    
        public object MarshalNativeToManaged(System.IntPtr pNativeData)
        {
            Marshal.PtrToStructure(pNativeData, this.marshaledObj);
            int count = this.marshaledObj.mStruct.count;
            IntPtr pIStruct = this.marshaledObj.mStruct.pIStruct;
            InnerStruct[] iStructArray = new InnerStruct[count];
            int dataSize = Marshal.SizeOf(new InnerStruct());
            for (int i = 0; i < count; i++)
            {
                iStructArray[i] = (InnerStruct)Marshal.PtrToStructure(new IntPtr(pIStruct.ToInt32() + dataSize * i), typeof(InnerStruct));
            }
    
            //*** how do I include iStructArray in return?
    
            return this.marshaledObj;
        }
    
        public System.IntPtr MarshalManagedToNative(object managedObj)
        {
            if (!(managedObj is OuterStruct))
            {
                throw new ArgumentException("Specified object is not a OuterStruct object.", "managedObj");
            }
            else
            {
                this.marshaledObj = (OuterStruct)managedObj;
            }
            IntPtr ptr = Marshal.AllocHGlobal(this.GetNativeDataSize());
            if (ptr == IntPtr.Zero)
            {
                throw new Exception("Unable to allocate memory to.");
            }
            Marshal.StructureToPtr(this.marshaledObj, ptr, false);
    
            return ptr;
        }
    
        public void CleanUpManagedData(object managedObj)
        {
        }
    
        public void CleanUpNativeData(System.IntPtr pNativeData)
        {
            Marshal.FreeHGlobal(pNativeData);
        }
    
        #endregion
    }
    

1 个答案:

答案 0 :(得分:1)

我通过使用另一个类将其包装在oStruct周围并添加字典字段来返回其他数据来解决我的问题。虽然它对我有用,但它使用起来非常不方便,并且不能很好地扩展。 无论如何,这可能对某人有用:

包装类

public class WrapperClass
{
    public OuterStruct oStruct;
    public Dictionary<string, object> auxData;

    public WrapperClass()
    {
        this.oStruct = new OuterStruct();
        this.auxData = new Dictionary<string, object>();
    }
}

自定义封送程序

public class myCustomMarshaler : ICustomMarshaler
{
    [ThreadStatic]
    private WrapperClass marshaledObj;

    private static myCustomMarshaler marshaler = null;
    public static ICustomMarshaler GetInstance(string cookie)
    {
        if (marshaler == null)
        {
            marshaler = new myCustomMarshaler();
        }
        return marshaler;
    }

    public int GetNativeDataSize()
    {
        return Marshal.SizeOf(typeof(OuterStruct));
    }

    public object MarshalNativeToManaged(System.IntPtr pNativeData)
    {
        Marshal.PtrToStructure(pNativeData, this.marshaledObj);
        int count = this.marshaledObj.mStruct.count;
        IntPtr pIStruct = this.marshaledObj.mStruct.pIStruct;

        InnerStruct[] iStructArray = new InnerStruct[count];
        int dataSize = Marshal.SizeOf(new InnerStruct());
        for (int i = 0; i < count; i++)
        {
            iStructArray[i] = (InnerStruct)Marshal.PtrToStructure(new IntPtr(pIStruct.ToInt32() + dataSize * i), typeof(InnerStruct));
        }
        // Add additional data to wrapper
        this.marshaledObj.auxData.Add("iStructArray", iStructArray);
        return this.marshaledObj;
    }

    public System.IntPtr MarshalManagedToNative(object managedObj)
    {
        if (!(managedObj is WrapperClass))
        {
            throw new ArgumentException("Specified object is not a WrapperClass object.", "managedObj");
        }
        else
        {
            this.marshaledObj = (WrapperClass)managedObj;
        }
        IntPtr ptr = Marshal.AllocHGlobal(this.GetNativeDataSize());
        if (ptr == IntPtr.Zero)
        {
            throw new Exception("Unable to allocate memory to.");
        }
        Marshal.StructureToPtr(this.marshaledObj.oStruct, ptr, false);

        return ptr;
    }

    public void CleanUpManagedData(object managedObj)
    {
    }

    public void CleanUpNativeData(System.IntPtr pNativeData)
    {
        Marshal.FreeHGlobal(pNativeData);
    }
}

用法

[DllImport("legacy.dll", CharSet = CharSet.Ansi)]
public static extern short getData([In, Out, MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(myCustomMarshaler))] OuterStruct wrapper);