Note that there are some explanatory texts on larger screens.

plurals
  1. POC# calling Delphi DLL that returns structure within structure
    primarykey
    data
    text
    <p>Yet another Delphi interop question......</p> <p>I have this Delphi code:</p> <pre><code>library DelphiDll; uses Dialogs, SysUtils, Classes; type TestEnum = (teOne, teTwo); TTestRecord = record end; TTestType = record MyTestRecords: array [1..255] of TTestRecord; MyTestEnum: TestEnum; end; {$R *.res} function DllFunction(var testType: TTestType): Boolean stdcall; export; begin testType.MyTestEnum := teTwo; Result := True; end; exports DllFunction; begin end. </code></pre> <p>And this C# code:</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; namespace DelpiDllTester { public enum TestEnum { One, Two } [StructLayout(LayoutKind.Sequential)] public struct TestType { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)] public TestRecord[] MyTestRecords; public TestEnum MyTestEnum; } [StructLayout(LayoutKind.Sequential)] public struct TestRecord { } class Program { [DllImport("DelphiDll.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)] public static extern bool DllFunction(ref TestType testType); static void Main(string[] args) { TestType testType = new TestType(); bool dllFunctionResult = DllFunction(ref testType); Console.WriteLine(dllFunctionResult); Console.WriteLine(testType.MyTestEnum); Console.ReadLine(); } } } </code></pre> <p>When I run the C# code, the console output for testType.MyTestEnum is always the enum value One even though it is clearly set to Two in the Delphi code.</p> <p>Now, if I simply change from using the array of TestRecord structures in the TestType structure to using a simple array of integers, all is well.</p> <p>Why does the array of integers work yet the array of structures does not?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload