需要与C ++ DLL接口

时间:2010-05-05 15:58:07

标签: c# c++ marshalling

我需要从C#调用C ++ API。我已经能够调用API,但char []参数似乎没有正确编组。 这是C ++签名:

Create2ptModel(double modelPowers[2], 
               double modelDacs[2], 
               int pclRange[2],
               double targetPowers[32], 
               double *dacAdjustFactor,
               unsigned short powerRampFactors[32], 
               BOOL bPCLDacAdjusted[32],
               char calibrationModel[32],
               char errMsg[1024])

这就是我试图从C#中调用它的方式

[DllImport("AlgorithmsLib.dll", EntryPoint = "_Create2ptModel@36", 
 ExactSpelling = true, CallingConvention = CallingConvention.StdCall, 
 CharSet = CharSet.Auto)]
private static extern AlgorithmStatus Create2ptModel(
    double[] modelPowers, 
    double[] modelDacs, 
    int[] pclRange, 
    double[] targetPowers, 
    ref double dacAdjustFactor, 
    ushort[] powerRampFactors, 
    bool[] bPCLDacAdjusted, 
    /**/char[] calibrationModel, 
    char[] errMsg/**/);

我对如何正确编组它的想法? 提前谢谢!

1 个答案:

答案 0 :(得分:3)

  1. 不要使用CharSet.Auto知道lib的字符集,请使用它。如果你让机器猜测,它可能会猜错。

  2. 这些char[]参数是否已终止?他们是投入还是产出?如果它们是以空值终止的输入,则只需使用string而不是char[]