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 5842489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:01:28+00:00 2026-05-22T12:01:28+00:00

I am writing a C# application in which it would call a DLL that

  • 0

I am writing a C# application in which it would call a DLL that was written in Delphi. I have the source code of Delphi DLL. I want to send a 2D array from C# to DLL and the DLL creates another 2D array and send it back to C# code. How can I do this?

  • 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-22T12:01:29+00:00Added an answer on May 22, 2026 at 12:01 pm

    I’ve written you some sample code for 2D arrays. It’s a bit messy because the marshaller won’t naturally handle 2D arrays. Instead I’ve opted for flattening the arrays, i.e. marshall them as 1D arrays. This won’t have great performance characteristics but perhaps that won’t be significant for you. Only you can know that.

    Delphi

    library mydll;
    
    var
      arr: array of array of Double;
    
    procedure ManagedToNative(RowCount, ColCount: Integer; Values: PDouble); stdcall;
    var
      r, c: Integer;
    begin
      SetLength(arr, RowCount, ColCount);
      for r := 0 to RowCount-1 do begin
        for c := 0 to ColCount-1 do begin
          arr[r,c] := Values^;
          inc(Values);
        end;
      end;
    end;
    
    procedure NativeToManaged(out RowCount, ColCount: Integer; Values: PDouble); stdcall;
    var
      r, c: Integer;
    begin
      RowCount := Length(arr);
      if RowCount>0 then begin
        ColCount := Length(arr[0]);
      end else begin
        ColCount := 0;
      end;
      if Assigned(Values) then begin
        for r := 0 to RowCount-1 do begin
          for c := 0 to ColCount-1 do begin
            Values^ := arr[r,c];
            inc(Values);
          end;
        end;
      end;
    end;
    
    exports
      ManagedToNative,
      NativeToManaged;
    
    begin
    end.
    

    C#

    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            [DllImport(@"mydll.dll")]
            private static extern void ManagedToNative(int RowCount, int ColCount, double[] Values);
    
            [DllImport(@"mydll.dll")]
            private static extern void NativeToManaged(out int RowCount, out int ColCount, double[] Values);
    
            static double[] Flattened(int RowCount, int ColCount, double[,] arr)
            {
                double[] result = new double[RowCount*ColCount];
                int i = 0;
                for (int r = 0; r < RowCount; r++)
                    for (int c = 0; c < ColCount; c++)
                    {
                        result[i] = arr[r,c];
                        i++;
                    }
                return result;
            }
    
            static double[,] Expanded(int RowCount, int ColCount, double[] arr)
            {
                double[,] result = new double[RowCount,ColCount];
                int i = 0;
                for (int r = 0; r < RowCount; r++)
                    for (int c = 0; c < ColCount; c++)
                    {
                        result[r,c] = arr[i];
                        i++;
                    }
                return result;
            }
    
            static void Main(string[] args)
            {
                const int RowCount = 6;
                const int ColCount = 9;
    
                double[,] arr = new double[RowCount,ColCount];
                for (int r = 0; r < RowCount; r++)
                    for (int c = 0; c < ColCount; c++)
                        arr[r,c] = r*c;
    
                ManagedToNative(RowCount, ColCount, Flattened(RowCount, ColCount, arr));
    
                int myRowCount, myColCount;
                NativeToManaged(out myRowCount, out myColCount, null);
                double[] flat = new double[myRowCount * myColCount];
                NativeToManaged(out myRowCount, out myColCount, flat);
                double[,] expanded = Expanded(myRowCount, myColCount, flat);
    
                for (int r = 0; r < RowCount; r++)
                    for (int c = 0; c < ColCount; c++)
                        System.Console.WriteLine(arr[r, c] - expanded[r, c]);
            }
        }
    }
    

    The code passes a 2D array from C# to Delphi where it is stored. Then the C# code asks for it back. The WriteLine() statements show that the same values are returned as were passed.

    I have arranged for NativeToManaged() to return the dimensions of the array even though this code already knows them. In reality your C# code is likely going to want to ask the Delphi code for the array size so that it can allocate memory in which to store the values.

    I won’t go on and on about why I’ve done certain things. From previous questions I think you have enough expertise to work it out from this code. If you do have any questions, leave a comment and I’ll do my best to shed some light!

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

Sidebar

Related Questions

I am writing a C# application which calls a function within an DLL written
I'm writing a c# application which uses automation to control another program. Naturally that
I'm writing an application in Python that is going to have a lot of
So I am recently writing a relatively complex application written in C# that performs
In my application i have a threaded commenting system which i call status messages.
I'm writing an application which reads large arrays of floats and performs some simple
I am writing an application which blocks on input from two istreams . Reading
I'm writing an application which has to be configurable to connect to Oracle, SQL
I've been writing an application which will need to expand environment strings in a
I'm writing an application which is quite graphically heavy and therefore I'm trying to

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.