Closed drvic10k closed 1 year ago
Sorry, I'm a little bit of context here. You are implementing you own AdsSymbolicServer here? Or what is the situation?
On first sight, hte SetupAds structure is missing the Memory Layout here (and therefore would use Default Marshalling, which doesn't work with dynamic length strings!).
Have you tried to add the StructLayout attribute on your class/struct like this?
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
struct SetupAds
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)]
public string MqttIpAddress { get; init; } = "";
[MarshalAs(UnmanagedType.I2)]
public short LogLevel { get; init; } = 0;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)]
public string ApiVersion { get; init; } = "";
}
You can check the correct marshalling size of your SetupAds struct with
int sz = System.Runtime.InteropServices.Marshal.SizeOf(typeOf(SetupAds));
which should be 164.
SetupAds structure is missing the Memory Layout here (and therefore would use Default Marshalling, which doesn't work with dynamic length strings!).
I was not aware of this, I will give it a try thanks
it works with the additional attributes, thanks
I have the following structure:
When I retrieve individual fileds, I get correct values back:
But retrieving the whole structure is not correct and the string is different every time: