Dll imports C ++ into C # with arrays

I am assigned to assign some library procedures to C # so that our other application developers can access it, but I don’t know how to declare variables so that they enter the procedure correctly.

The problem is that when I look at input with C ++ code, I get all the distorted values

Attempted use:

double[] Par = { 8, 16, 8, 0.61, 0.00635, ... }; // 29 variables double[] Inlet = { 22.18, 43.31, 1.13, 2.81, 0.43 }; // 5 variables double[] Outlet = { 0, 0, 0, 0, 0, 0 }; // placeholder for 6 variables SteadyFor(ref Par, ref Inlet, ref Outlet, FileIn, FileOut); 

DLL Import

 [DllImport(MODELAPP, EntryPoint = "SteadyFor", ExactSpelling = false)] public static extern int SteadyFor( ref double[] par, ref double[] inlet, ref double[] outlet, [MarshalAs(UnmanagedType.LPStr)] string input, [MarshalAs(UnmanagedType.LPStr)] string output); 

C ++ file:

 extern "C" int SteadyFor(double Par[], double Inlet[], double Outlet[], char* FileIn, char* FileOut) { int n = (int)Par[0]; // Actual Reading: [0] int nt = (int)Par[1]; // Actual Reading: [0] int pass = (int)Par[2]; // Actual Reading: [0] double l = Par[3]; // Actual Reading: [2.9581223236733198e+174] double rTube = Par[4]; // Actual Reading: [2.121995790965e-314#DEN] double tTube = Par[5]; // Actual Reading: [5.432309224896e-312#DEN] double pl = Par[6]; // Actual Reading: [1.0253217546256438e-267] double pt = Par[7]; // Actual Reading: [4.60629e-308] // ... } 

Obviously, the values ​​that I get are incorrect - almost like uninitialized memory.

Can someone tell me what I am doing wrong and how to fix it?

Hi,

~ Joe

+6
arrays c # dllimport
source share
2 answers

Drop the keywords "ref" in the declaration, they are incorrect. That C ++ code is not crashing with AV is a little mysterious.

The [MarshalAs] attribute in rows is not needed.

+7
source share

Basically, you need to declare a sort attribute. In your case, this will be the MarshalAs attribute (UnmanagedType.LPArray) .

Look at here:
http://msdn.microsoft.com/en-us/library/z6cfh6e6%28VS.80%29.aspx#cpcondefaultmarshalingforarraysanchor3

+1
source share

All Articles