Beckhoff / TF6000_ADS_DOTNET_V5_Samples

Sample code for the Version 6.X series of the TwinCAT ADS .NET Packages
https://infosys.beckhoff.com/content/1033/tc3_ads.net/9407515403.html?id=6770980177009971601
BSD Zero Clause License
37 stars 15 forks source link

Array of structs marshaled as null #48

Closed drvic10k closed 11 months ago

drvic10k commented 1 year ago

I have a problem with marshaling this type

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct DataAds
{
    [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.Struct, SizeConst = 4)]
    public StructAds[] Structs;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public struct StructAds
{
    public int Field1;

    public int Field2;

    public int Field3;

    public int Field4;
}

when marshaling in AdsSymbolicServer like this: _symbolMarshaler.TryMarshal(symbol, value, span, out int _)

I get the property Structs=null

Am I missing some attributes?

RalfHeitmann commented 11 months ago

The SymbolMarshaler doesn't need external classes to Marshal the symbol. The Symbol itself contains all information how to marshal the data. I have extended the AdsSymbolicServerSample with additional code that demonstrates accessing structs.