Question : calling C++ dll function from c#

I have to call a function contained in an existing dll written in c++, from my application that I have written in c#

My problem is that one of the parameters of that function is a pointer to a structure & I cannot figure a way of doing this from c#.

The c++ function prototype is

int FAR PASCAL FunctionName(Parameters* param)

the Parameters Struct is defined as


typedef struct{
                       int cp;
                       int tb;
                       int map[8];
                       double freq[8,16];
}Parameters;

I have successfully called dll functions using pointers before but never pointers to structures that contain arrays.

I have tried the following c# code


struct Prameters
{
  int cp;
  int tb;
  int[] map;
  double[] freq;
}

and I have the following function declaration

public static unsafe extern int FunctionName(Parameters* param);


My problem is that this gives a compilation error

Cannot take the address of, get the size of, or declare a pointer to a managed type

If I comment out the arrays in the struct it compiles Ok - but of course the function will fail

Any comments would be much appreciated.

Thanks

Answer : calling C++ dll function from c#

You can try follow code:
 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        struct Parameters
        {
            int cp;
            int tb;
            [MarshalAs(UnmanagedType.SysInt, SizeConst = 8)]
            int[] map;
            [MarshalAs(UnmanagedType.R8, SizeConst = 8)]
            double[] freq;          
        }
        [DllImport("MyDLL.Dll")]
        public extern static int FunctionName(ref Parameters param);
Random Solutions  
 
programming4us programming4us