A helper classes to help marshall struct/valuetypes to/from their byte[] representation
Restriction: |
---|
MicroFocus.COBOL.RuntimeServices are supported for .NET managed code only. |
Inheritance Hierarchy
MicroFocus.COBOL.RuntimeServicesMarshallUtils
Namespace: MicroFocus.COBOL.RuntimeServices
Assembly: MicroFocus.COBOL.RuntimeServices (in MicroFocus.COBOL.RuntimeServices.dll) Version: 1.2.3.4
Syntax
The MarshallUtils type exposes the following members.
Constructors
Name | Description | |
---|---|---|
![]() | MarshallUtils |
Methods
Name | Description | |
---|---|---|
![]() ![]() | GetBytes |
Get the byte[] representation of an structure
|
![]() ![]() | GetStruct |
Convert a byte[] into a structure
|
Examples
The example below shows how to marshall a C# struct/valuetype to a byte[] and use it is a parameter to RunUnit::Call and then marshall the byte[] back to a struct/valuetype.
C#:
C#
using System; using System.Collections.Generic; using System.Linq; using System.Text; using MicroFocus.COBOL.RuntimeServices; using System.Runtime.InteropServices; using MicroFocus.COBOL.Program; namespace ruTest { [Serializable] [StructLayout(LayoutKind.Sequential)] struct NameContainer { public int UseCounter; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] Name; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] Surname; public NameContainer(byte[] a, byte[] b) { this.UseCounter = 0; this.Name = new byte[8]; this.Surname = new byte[8]; // Note: range checking done or COBOL friendly space padding a.CopyTo(this.Name, 0); b.CopyTo(this.Surname, 0); } public string ToString(Encoding encoding) { return string.Format("{0}{1} [use={2}]", encoding.GetString(this.Name), encoding.GetString(this.Surname), this.UseCounter); } } class Program { static void Main(string[] args) { RunUnit MyRunUnit = new RunUnit(); MyProgram prog = new MyProgram(); try { NameContainer x = new NameContainer(MyRunUnit.GetBytes(prog, "A"), MyRunUnit.GetBytes(prog, "B")); // Get the bytes representation of NameContainer byte[] xInBytes = MarshallUtils.GetBytes(x); // Add the program to the rununit MyRunUnit.Add(prog); MyRunUnit.Call("MyProgram", xInBytes); // Re-Construct the struct x = (NameContainer)MarshallUtils.GetStruct(xInBytes, 0, typeof(NameContainer)); Console.WriteLine(x.ToString(Encoding.ASCII)); xInBytes = MarshallUtils.GetBytes(x); MyRunUnit.Call("MyProgram", xInBytes); // Re-Construct the struct x = (NameContainer)MarshallUtils.GetStruct(xInBytes, 0, typeof(NameContainer)); Console.WriteLine(x.ToString(Encoding.ASCII)); } finally { if (MyRunUnit != null && MyRunUnit.Active == true) { MyRunUnit.StopRun(); } } } } }
COBOL:
cobol
program-id. MyProgram. linkage section. 01 lnk-name-struct. 03 lnk-enter-counter binary-long. 03 lnk-Name PIC X(8). 03 lnk-Surname PIC X(8). procedure division using lnk-name-struct. add 1 to lnk-enter-counter move ALL "A" to lnk-Name move ALL "B" to lnk-Surname goback.
See Also