【问题标题】:Need to interface to a C++ DLL需要连接到 C++ DLL
【发布时间】:2010-05-05 15:58:07
【问题描述】:

我需要从 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/**/);

知道如何正确编组它吗? 提前致谢!

【问题讨论】:

  • C#中calibrationModel左边的两个星号是错字吗?
  • 我认为他试图突出它们。我已经格式化了代码。

标签: c# c++ marshalling


【解决方案1】:
  1. 不要使用CharSet.Auto 你知道库的字符集,使用它。如果你让机器猜测,它可能会猜错。

  2. 那些char[] 参数是否以空值终止?它们是输入还是输出?如果它们是以 null 结尾的输入,则只需使用 string 而不是 char[]

【讨论】:

  • 这是正确的。 CharSet.Ansi 是必需的,char[] 必须是字符串。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
相关资源
最近更新 更多