0

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);
4

1 回答 1

2

我很确定你不想要数组上的 ref 。这表明 C++ 中的类型例如是 int** 而不是 int*。

此外,如果您减少参数的数量(通过将它们放在某种结构中),您可能会提高代码的整体速度。处理器必须推送大量参数,当您拥有这么多参数时,这会花费大量时间。

于 2012-09-28T14:50:40.857 回答