c#

时间:2018-02-28 13:46:01

标签: c# c++ string marshalling dllimport

在C#项目中,我很难连接来自外部库的C ++函数。我想这是字符串编组问题。

在C ++中有以下功能:

int compute(const char names[][LENGTH_1], char values[][LENGTH_2], const int n);

目标是提供:

  • 包含" n"的只读数组字符串LENGTH_1个字符
  • 包含" n"的可写数组; LENGTH_2个字符的字符串

功能"计算"将写入数组"值"根据" names"中指定的内容。

在C#中尝试了两种不同的连接函数的方法

方法1

[DllImport("abcd.dll", EntryPoint="compute", CharSet=CharSet.Ansi)]
internal static extern int Compute(StringBuilder [] names, StringBuilder [] values, int n);

我这样称呼它:

var names = new StringBuilder[number];
var descriptions = new StringBuilder[number];
for (int i = 0; i < number; i++) {
  names[i] = new StringBuilder(LENGTH_1);
  descriptions[i] = new StringBuilder(LENGTH_2);
}
var error = Compute(names, descriptions, number);

方法2

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=4)]
internal struct StringName
{
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)] // LENGTH_1 = 64
  public string msg;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack=4)]
internal struct StringValue
{
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)] // LENGTH_2 = 128
  public string msg;
}

[DllImport("abcd.dll", EntryPoint="compute", CharSet=CharSet.Ansi)]
internal static extern int Compute(StringName[] names, ref StringValue[] values, int number);

我这样称呼它:

var names = new StringNames[number];
var values = new StringValue[number];
var error = Compute(names, ref values, number);

结果

它没有例外地崩溃,程序在功能&#34; Compute&#34;上被阻止。我仍然不确定问题是来自字符串还是来自外部库。

1 个答案:

答案 0 :(得分:0)

正如David所发现的,关键字&#34; ref&#34;错了。以下是方法2的更改。

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
internal struct StringName
{
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst=LENGTH_1)]
  public string msg;
}

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
internal struct StringValue
{
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst=LENGTH_2)]
  public string msg;
}

[DllImport("abcd.dll", EntryPoint="compute", CharSet=CharSet.Ansi)]
internal static extern int Compute([In] StringName[] names, [In, Out] StringValue[] values, int number);

我这样称呼它:

var names = new StringNames[number];
// ... here the initialization of "names" ... //
var values = new StringValue[number];
var error = Compute(names, values, number);
// ... here the processing of "values" ... //