Skip to content

Latest commit

 

History

History
31 lines (21 loc) · 1.49 KB

File metadata and controls

31 lines (21 loc) · 1.49 KB

Interaction between Native and Managed Code for Arrays with Strings

This short guide explains how arrays of IEC strings are passed between managed C# code and native C++ using the eCLR. It follows the same patterns as for numeric arrays and structs, with string-specific handling in native code.

C# Class

StringArrayFB1.cs

  • Arrays are passed as pointers and must be declared as [InOut] to allow native code to read and modify them.
  • A [Native] method wrapper exposes the operation to managed code.

Native Implementation

StringArrayFB1NativeMethods-cli.cpp

  • Methods are exposed to managed code via the PInvoke prefix.
  • String elements are accessed through the array’s Anchor and copied using IEC string APIs.

C# Function Block

StringArrayFB2.cs

  • A [Native] function block can implement its logic entirely in native code; only explicitly marked [Managed] members run in C#.

Native Implementation

StringArrayFB2-cli.cpp

Notes and Best Practices

  • Arrays must be passed as pointers and marked [InOut] to enable native read/write access.
  • Always null-check pointers before use in native code.
  • Use GetLowerBound/UpperBound to determine array size.
  • For IEC strings (e.g. IecString200), copy contents with Assign(Begin(), GetLength()) rather than copying raw memory.
  • The [Native] attribute on classes/methods and the PInvoke prefix in C++ define the managed–native boundary.