-2

I'm trying to call an unmanaged DLL's function in my C# app. The function declaration is this. The function writes to file the data from an acquisition board (i.e., from arrays real_data and imag_data).

I'm using the following declaration, but the file contains incorrect data and I think that the declaration is wrong:

[DllImport(@"C:\SpinCore\SpinAPI\lib32\spinapi.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int pb_write_ascii_verbose(string fname, int num_points, float SW, float SF, ref int[] real_data, ref int[] imag_data);

Usage:

pb_write_ascii_verbose(@"C:\Users\Public\direct_data_0.txt", numberOfPoints, (float)actualSW, (float)sequence.Frequency, ref idata, ref idata_imag);

Is this correctly declared? If so, what is the proper declaration?

Ðаn
  • 10,934
  • 11
  • 59
  • 95
Cristian M
  • 715
  • 2
  • 12
  • 32
  • hmm spincore does give .Net wrappers but they are beta version and it's not clear how new they are(at their site they talk about vs 2008 !!!!), anyway you may look here : http://www.spincore.com/support/net/ – niceman Apr 26 '17 at 14:07
  • Do you know what type of DLL this is, and are you getting an error when trying to make the call? Try changing your `CallingConvention` to `StdCall`. – Alex Apr 26 '17 at 14:21
  • @Alex Dmitry Egorov's answer worked. – Cristian M Apr 27 '17 at 06:20
  • 1
    @niceman Thank you for the suggestion. – Cristian M Apr 27 '17 at 06:20

2 Answers2

0

You need to give Interop a hint to marshal the arrays as LPArray. ref is not required in this case:

[DllImport(@"C:\SpinCore\SpinAPI\lib32\spinapi.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int pb_write_ascii_verbose(
    string fname, 
    int num_points, 
    float SW, 
    float SF, 
    [MarshalAs(UnmanagedType.LPArray)]
    int[] real_data, 
    [MarshalAs(UnmanagedType.LPArray)]
    int[] imag_data);
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
-1

You don't need ref int[], you're declaring "a pointer to an array".

Your DLL function expects an array.

[DllImport(@"C:\SpinCore\SpinAPI\lib32\spinapi.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int pb_write_ascii_verbose(string fname, int num_points, float SW, float SF, int[] real_data, int[] imag_data);
Oleh Nechytailo
  • 2,155
  • 17
  • 26