How to pass an array of doubles from managed to unmanaged code.

So I was working with someone on getting this to work and there aren't ANY samples anywere that I could find using what you would expect to use for searching (PInvoke, “array of doubles”, etc.).  I figured I'd post this up here so it would get indexed and people could benefit from it.  Thanks to AnantD for helping out!



// C# PInvoke Declaration

[DllImport("structTest.dll", EntryPoint="dblArray")]

public static unsafe extern double dblArray ( double [] s, long len);



' VB.NET PInvoke declaration

Public Declare Auto Function dblArray Lib "structtest.dll" (<[In](), Out()> ByVal d1() As Double, ByVal l As Long) As double



// Unmanaged Function

double dblArray(double x[], long n)


      for (int i = 0; i < n; i++)

            x[i] = i * 2.1;


      return 0;



Comments (3)

  1. S N says:

    Shouldn’t the C# PInvoke declarion take an additional parameter for length like VB.Net declaration?


  2. Robert Gruen says:

    Absolutely correct! I’ll will fix that momentarily!

  3. Uber Braner says:

    C# long => 64 bits

    C++ long => 32 bits

Skip to main content