canisrufus
canisrufus

Reputation: 693

What is wrong with my marshaling for the C# DLL import of this C++ function?

I am trying to invoke a function in a C++ DLL from C#/.net. I believe there is something wrong with how I am marshaling the data. The function call runs but the return values (the last 7 variables) are all set to 0, and the arrays are set to length of 1 (I assume this is the DLL's intended behavior when it receives data it can't work with).

The C++ DLL is compiled as 32bit.

Here is the C++ header:

extern "C" __declspec(dllexport) void __stdcall Merchandize(int SppCode,
                                                                  double DBH, 
                                                                  double TotHt, 
                                                                  double CR, 
                                                                  double BTR,
                                                                  int *minDIBs,     //array
                                                                  int *minLens,     //array
                                                                  int *minVols,     //array
                                                                  int NumProducts, //array length
                                                                  int maxLen,       
                                                                  double trimLen,
                                                                  double stumpHt,
                                                                  int logStep,
                                                                  int *logLens,     //array, understood to be 50
                                                                  double *logSEDs,  //array, understood to be 50
                                                                  double *logVols,  //array, understood to be 50
                                                                  int *logGrades,   //array, understood to be 50
                                                                  int *NumberOfLogs,
                                                                  double *merchHt,
                                                                  int *errorFlag)

And here is the C# dll import:

    [DllImport("C:\\Users\\Public\\Documents\\FVSNEMerchandizer.dll")]
      static extern void MerchandizeTree(int SppCode, 
                                            double DBH,
                                            double TotHt,
                                            double CR,
                                            double BTR,
                                            ref int[] minDIBs,
                                            ref int[] minLens, 
                                            ref int[] minVols, 
                                            int NumProducts,
                                            int maxLen, 
                                            double trimLen, 
                                            double stumpHt, 
                                            int logStep,
                                            ref int[] logLens, 
                                            ref double[] logSEDs, 
                                            ref double[] logVols,
                                            ref int[] logProds, 
                                            ref int[] numLogs, 
                                            ref double merchHt,
                                            ref int errorFlag);

Upvotes: 0

Views: 301

Answers (1)

CrazyCasta
CrazyCasta

Reputation: 28362

I'm pretty sure you don't want the ref on the arrays. That would suggest the type in C++ would be int** for instance instead of int*.

Also, you would probably increase your code's overall speed if you reduced the number of parameters (by putting them in some sort of struct). The processor has to push a ton of arguments which takes a lot of time when you have that many of them.

Upvotes: 2

Related Questions