Convert Delphi Option Record to C #

How can I convert this code from Delphi to C #? I need a framework for interacting with unmanaged code.

TDataTypeParam = packed record
 dtType : integer;
 case integer of
   cInt     :(dtInt      : integer);
   cFloat   :(dtFloat    : real);
   cLongInt :(dtLongInt  : Int64);
   cDateTime:(dtDateTime : TDateTime);
   cShortStr:(dtShortString : ShortString);
end;

TDataParam =   packed record
 NumberParam : integer;
 Param       : array [1..MaxParam] of TDataTypeParam;
end;

TEvData =   packed record
 dm       : TDateTime;
 CodeEV   : integer;
 IDCAM    : integer;
 Reserv1  : integer;

 Data     : TDataParam;
end;

TArrSrvData =   packed record
 NumberPack : integer;
 Address  : Cardinal;
 tpCL     : integer;
 tpEv     : integer;
 Reserv   : integer;
 Packs      : array [1..MaxPacks] of TEvData;
end;

This code is throw System.TypeLoadException:

//TDataTypeParam = packed record
//dtType : integer;//data type
// case integer of
//   cInt     :(dtInt      : integer);
//   cFloat   :(dtFloat    : real);
//   cLongInt :(dtLongInt  : Int64);
//   cDateTime:(dtDateTime : TDateTime);
//   cShortStr:(dtShortString : ShortString);
//end;

[StructLayout(LayoutKind.Explicit)]
[Serializable]
internal struct DataTypeParam
{
    [FieldOffset(0)]
    public DataType dtType;
    [FieldOffset(4)]
    public int dtInt;
    [FieldOffset(4)]
    public double dtFloat;
    [FieldOffset(4)]
    public long dtLongInt;
    [FieldOffset(4)]
    public double dtDateTime;
    [FieldOffset(4)]
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    public byte[] dtShortString;
};

//TDataParam =   packed record
//  NumberParam : integer;
//  Param       : array [1..MaxParam] of TDataTypeParam;
// end;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
[Serializable]
internal struct DataParam
{
    public int NumberParam;
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = Consts.MaxParam)]
    public DataTypeParam[] Param;
};

// TEvData =   packed record
//  dm       : TDateTime;
//  CodeEV   : integer;
//  IDCAM    : integer;
//  Reserv1  : integer;

//  Data     : TDataParam;
// end;
[StructLayout(LayoutKind.Sequential, Pack = 1)]
[Serializable]
internal struct EvData
{
    public DateTime dm;
    public int CodeEV;
    public int IDCAM;
    public int Reserv1;
    public DataParam Data;
}

// TArrSrvData =   packed record
//  NumberPack : integer;
//  Address  : Cardinal;
//  tpCL     : integer;
//  tpEv     : integer;
//  Reserv   : integer;
//  Packs      : array [1..MaxPacks] of TEvData;
// end;
// PArrSrvData = ^TArrSrvData;

[StructLayout(LayoutKind.Sequential, Pack = 1)]
[Serializable]
internal struct ArrSrvData
{
    public int NumberPack;
    public uint Address;
    public int tpCL;
    public int tpEv;
    public int Reserv;
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = Consts.MaxPacks)]
    public EvData[] Param;
}

Thi problem is cShortStr: (dtShortString: ShortString); conversion ...

Here is the actual code:

    void mySink_NewEvent(ref object Comm)
    {
        byte[] arr = Comm as byte[];
        if(arr == null) return;

        var data = (ArrSrvData)MarshalSerializer.RawDeserialize(arr, typeof(ArrSrvData));

    }

And the RawDeserialize code:

    public static object RawDeserialize(byte[] rawData, Type type)
    {
        if (rawData == null)
            throw new ArgumentNullException(MethodBase.GetCurrentMethod().GetParameters()[0].Name);

        int rawsize = Marshal.SizeOf(type);
        if (rawsize > rawData.Length)
            return null;

        object retobj;

        GCHandle handle = GCHandle.Alloc(rawData, GCHandleType.Pinned);
        try
        {
            IntPtr buffer = handle.AddrOfPinnedObject();
            retobj = Marshal.PtrToStructure(buffer, type);
        }
        finally
        {
            handle.Free();
        }

        return retobj;
    }
+4
source share
1 answer

You have a couple of questions.

First of all, C # DateTimedoes not map to Delphi TDateTime. You will need to use doubleC # in the code and copy the C # date / time mapping to the Delphi date / time.

, , . Delphi ShortString 256 . , 255 - .

#. . , , . : # Platform-invoke, c-style union . , , .

MSDN :

.

. , . . , unsafe . : .

, . byte[] UnmanagedType.ByValArray.

[StructLayout(LayoutKind.Sequential)]
[Serializable]
internal struct DataTypeParam
{
    public DataType dtType;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
    public byte[] dtUnion;
};

/ / . :

[StructLayout(LayoutKind.Sequential)]
[Serializable]
internal struct DataTypeParam
{
    public int dtType;
    [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
    private byte[] dtUnion;

    public int cInt
    {
        get { return BitConverter.ToInt32(dtUnion, 0); }
        set { dtUnion = BitConverter.GetBytes(value); }
    }

    public double cFloat
    {
        get { return BitConverter.ToDouble(dtUnion, 0); }
        set { dtUnion = BitConverter.GetBytes(value); }
    }
};

.

+5

Source: https://habr.com/ru/post/1523603/


All Articles