views:

84

answers:

1

Hi, I need to call a C++ API from C#. I have been able to call the API, but the char[] parameters do not seem to be marshalling correctly. Here's the C++ signature:

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])

and this is how I am trying to call it from 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/**/);

Any idea of how I can marshall it correctly? Thanks in advance!

+3  A: 
  1. Don't use CharSet.Auto you know the character set of the lib, use that. If you let the machinery guess, it could guess wrongly.

  2. Are those char[] parameters null terminated? Are they inputs or outputs? If they are null-terminated inputs, then just use string instead of char[].

Frank Krueger
This is correct. CharSet.Ansi is required, the char[] must be string.
Hans Passant