Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:29:14+00:00 2026-05-26T15:29:14+00:00

Im trying to read an alarm structure from a Beckhoff – PLC into a

  • 0

Im trying to read an alarm structure from a Beckhoff – PLC into a c# class.
First i have to make the exact same structure in c# and it currently looks like this:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
    public class Alarm
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)]
        public string text;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
        public string objectName;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
        public string[] instancePath = new string[6];
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
        public string timeStamp;
        public int priority;
        [MarshalAs(UnmanagedType.I1)]
        public bool acknowledge;
        [MarshalAs(UnmanagedType.I1)]
        public bool disabled;
        [MarshalAs(UnmanagedType.I1)]
        public bool alarmIn;
    }

Whats causing me problems is the “instancePath” field.
When the field is a string i can use the “UnmanagedType.ByValTStr” attribute with SizeConst and when it’s an array “UnmanagedType.ByValArray” but when i want to use a string[] i don’t know what to do.

I’ve tried creating a new class:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
    public class InstancePathDefinition
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
        public string instancePath;
    }

And used in my alarm class:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
    public class Alarm
    {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 81)]
        public string text;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
        public string objectName;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
        public InstancePathDefinition[] instancePath = new InstancePathDefinition[6];
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 24)]
        public string timeStamp;
        public int priority;
        [MarshalAs(UnmanagedType.I1)]
        public bool acknowledge;
        [MarshalAs(UnmanagedType.I1)]
        public bool disabled;
        [MarshalAs(UnmanagedType.I1)]
        public bool alarmIn;
    } 

But when I use Marshal.SizeOf on my Alarm-class it gives me a size or 147 bytes instead of 189 bytes as I would expect.

EDIT:
I think the reason for the difference in size is that only the array gets initiated and the class “InstancePathDefinition” doesn’t.

I tried changing it from a class to a struct and now the sizes match.

I still find it strange though that I can’t combine both UnmanagedType.ByValArray and UnmanagedType.ByValTStr as a sub type with different SizeConst.

Next I will need to create an array of the alarm class and that will get me into the same trouble again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:29:15+00:00Added an answer on May 26, 2026 at 3:29 pm

    Make it an array of structures instead of an array of class objects.

    [StructLayout(LayoutKind.Sequential)]
    public struct InstancePathDefinition {
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11)]
        public string path;
    }
    

    Marshal.SizeOf() returned 189 when I tried it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to read in some information from the google maps api into my application
Im trying to read and write to/from the same file, is this possible? Here
am trying to read few lines from a txt file using JS,and i have
Im trying to read some data from a binary file into a buffer allocated
Im trying to read a column of String values from my DB. Im trying
I'm trying to read some data on the Serial Port but I doesn't have
I am trying to read the text content of a pdf file into a
I'm trying to read a TIFF image from file using BufferedImage. The following is
I'm trying to read a text file (Using BufferedReader and FileReader) from my file
Trying to read registry keys remotly (from a host on local intranet)... All the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.