Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的联合编组问题_C#_Marshalling - Fatal编程技术网

C# C语言中的联合编组问题

C# C语言中的联合编组问题,c#,marshalling,C#,Marshalling,我有一个内部结构的联合体,这个结构看起来像 struct tDeviceProperty { DWORD Tag; DWORD Size; union _DP value; }; typedef union _DP { short int i; LONG l; ULONG ul; float

我有一个内部结构的联合体,这个结构看起来像

struct tDeviceProperty {

    DWORD Tag;
    DWORD Size;     
    union _DP value;    
};

typedef union _DP
{
      short int           i;       
      LONG                l;      
      ULONG               ul;     
      float               flt;    
      double              dbl;    
      BOOL      b;        
      double              at;     
      FILETIME            ft;     
      LPSTR               lpszA;  
      LPWSTR              lpszW;  
      LARGE_INTEGER       li;     
      struct tBinary    bin;    
      BYTE                reserved[40]; 
} __UDP;


struct tBinary {
    ULONG size;     
    BYTE * bin;     
};
从tBinary结构箱必须转换为tImage结构,如下所示

struct tImage {
    DWORD x;
    DWORD y;
    DWORD z;
    DWORD Resolution;
    DWORD type;
    DWORD ID;
    diccid_t SourceID;
    const void *buffer;
    const char *Info;
    const char *UserImageID;
}; 
为了在c中使用同样的方法,我做了封送处理,但在将指针转换为结构时没有给出正确的值。C代码如下:

tBinary tBin = new tBinary();
IntPtr tBinbuffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(tBin));
Marshal.StructureToPtr(tBin.bin, tBinbuffer, false);


tDeviceProperty tDevice = new tDeviceProperty();
tDevice.bin = tBinbuffer;
IntPtr tDevicebuffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(tDevice));
Marshal.StructureToPtr(tDevice.bin, tDevicebuffer, false);

Battary tbatt = new Battary();
tbatt.value = tDevicebuffer;
IntPtr tbattbuffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(tbatt));
Marshal.StructureToPtr(tbatt.value, tbattbuffer, false);

result = GetDeviceProperty(ref tbattbuffer);

Battary v = (Battary)Marshal.PtrToStructure(tbattbuffer, typeof(Battary));

tDeviceProperty v2 = (tDeviceProperty)Marshal.PtrToStructure(tDevicebuffer, typeof(tDeviceProperty));

tBinary v3 = (tBinary)Marshal.PtrToStructure(tBinbuffer, typeof(tBinary));






[StructLayout(LayoutKind.Explicit)]
public struct tDeviceProperty
{
    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.U2)]
    public ushort i;
    [FieldOffset(2)]
    [MarshalAs(UnmanagedType.I4)]
    public int l;
    [FieldOffset(6)]
    [MarshalAs(UnmanagedType.U4)]
    public uint ul;
    [FieldOffset(10)]
    [MarshalAs(UnmanagedType.R4)]
    public float flt;
    [FieldOffset(14)]
    [MarshalAs(UnmanagedType.R8)]
    public double dbl;
    [FieldOffset(22)]
    [MarshalAs(UnmanagedType.I4)]
    public int b;
    [FieldOffset(26)]
    [MarshalAs(UnmanagedType.R8)]
    public double at;
    [FieldOffset(34)]
    //[MarshalAs(UnmanagedType.Struct)]
    public IntPtr ft;
    [FieldOffset(42)]
    //[MarshalAs(UnmanagedType.Struct)]
    public IntPtr lpszA;
    [FieldOffset(43)]
    //[MarshalAs(UnmanagedType.Struct)]
    public IntPtr lpszW;
    [FieldOffset(45)]
    [MarshalAs(UnmanagedType.U8)]
    public ulong li;
    [FieldOffset(53)]
    [MarshalAs(UnmanagedType.Struct)]
    public IntPtr bin;
    [FieldOffset(61)]
    //[MarshalAs(UnmanagedType.Struct)]
    public IntPtr reserved;
}


[StructLayout(LayoutKind.Sequential)]
public struct tBinary
{
    public int size;
    public IntPtr bin;
}

[StructLayout(LayoutKind.Sequential)]
public struct Battary
{
    public uint Tag;
    public uint Size;
    public IntPtr value;
}

[StructLayout(LayoutKind.Sequential)]
public struct tDiccBatteryStatus
{
    public uint RefreshWear;
    public uint TotalWear;
    public ushort Voltage;
    public ushort Battery;
    public int BatteryOK;
    public int NeedRefresh;
    public int NeedChange;
    public ushort Temperature;
    public int Charge;
    public byte State;
    public byte ExternalPowered;
    public int CapacityLeft;
}

我认为失败的原因是tDeviceProperty声明。实际上,其成员值是一个并集,但在tDeviceProperty中,应用的偏移量应不断增加,而它们的值应相同

这是因为联合成员从相同的偏移开始,共享其他成员的相同空间。联合的大小由联合中声明的字段的最大大小决定

实际上,使用您的代码:

[StructLayout(LayoutKind.Explicit, Size = )]
public struct tDeviceProperty
{
    [FieldOffset(0)]
    [MarshalAs(UnmanagedType.U2)]
    public ushort i;
    [FieldOffset(2)]
    [MarshalAs(UnmanagedType.I4)]
    public int l;
    [FieldOffset(6)]
    [MarshalAs(UnmanagedType.U4)]
    public uint ul;
    [FieldOffset(6)]
    [MarshalAs(UnmanagedType.R4)]
    public float flt;
    [FieldOffset(6)]
    [MarshalAs(UnmanagedType.R8)]
    public double dbl;
    [FieldOffset(6)]
    [MarshalAs(UnmanagedType.I4)]
    public int b;
    [FieldOffset(6)]
    [MarshalAs(UnmanagedType.R8)]
    public double at;
    [FieldOffset(6)]
    //[MarshalAs(UnmanagedType.Struct)]
    public IntPtr ft;
    [FieldOffset(6)]
    //[MarshalAs(UnmanagedType.Struct)]
    public IntPtr lpszA;
    [FieldOffset(6)]
    //[MarshalAs(UnmanagedType.Struct)]
    public IntPtr lpszW;
    [FieldOffset(6)]
    [MarshalAs(UnmanagedType.U8)]
    public ulong li;
    [FieldOffset(6)]
    [MarshalAs(UnmanagedType.Struct)]
    public IntPtr bin;
    [FieldOffset(6)]
    //[MarshalAs(UnmanagedType.Struct)]
    public IntPtr reserved;
}

同时检查我最近的。

未获得正确的值。。。从指针转换为结构时,值出现错误。可能是什么问题?