Hi All,

I have a C++ DLL, which has a function called INPUT and it is defined as follows in DLL:

extern "C" __declspec(dllexport) char* _cdecl  INPUT(int a,int b,double D[2],double Z[2],double e,double f);

__declspec(dllexport) char* _cdecl input_balr2(int a,int b, double *D,double *Z,double e,double f)
 //Do Something

Now, i want to use this DLL in my C# Application, I can pass int and double variables very well but there is some error in passing arrays!

following is the code: made a class called DllClass.cs:

using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Dll_Chart
  class DllClass
     private const string dllfilename = @"C:\INPUTdll.dll"; 
     [DllImport(dllfilename, EntryPoint = "INPUT", CharSet = CharSet.Ansi)] 

public static extern IntPtr INPUT(int a, int b, double[] D, double[] Z, double e, double f); 

ON A Button_Click event of Form1.cs:

private void button1_Click(object sender, EventArgs e) 
   int a1 = 23; 
   int b1 = 24; 
   double[] array1 = new double[2]; 
   double[] array2 = new double[2];
   array1[0] = 5.1;
   array1[1] = 5.2;
   array2[0] = 5.3;
   array2[1] = 5.4;
   double e1 = 1.1; 
   double f1 = 1.2; 

  DllClass.INPUT(a1, a2, array1, array2, e1, f1);

Is it the right way to pass arrays?
i guess not, as dll is not producing desired results!!
Any suggestion is greatly appreciated.

>Is it the right way to pass arrays?

Add unsafe in the function declaration:

public static unsafe extern IntPtr INPUT(int a, int b, double *D, double *Z, double e, double f);

and also add /unsafe switch to compile your program.

I added "unsafe" to the function;
but if i change parameter to double *D, double *Z, it throws error:

" cannot convert from 'double[]' to 'double*' "

one more thing i want to know is that, are my array1 and array2 values passing correctly to dll function?


This article has been dead for over six months. Start a new discussion instead.