1
Reply

How To Pass One Dimensional Array to C++ DLL?

Mukesh Karanwal

Mukesh Karanwal

Apr 1 2010 5:25 AM
4.3k

Hi,
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 array  ? I guess not, as it doesnt produce the desired result!!
Any suggestion is greatly appreciated.
Thanks,


Answers (1)