2009-12-07 63 views
2

我在写一个使用.NET 2.0的c#应用程序。我需要使用旧库来进行专有压缩。我没有该库的源代码,并且它背后的开发人员已经很久没有。将DLLImport与包含空字符的输出char []一起使用

我的问题是生成的char []包含空值,并被截断。下面是该函数的声明:

[DLLImport("foo.dll")] 
public static extern bool CompressString(char[] inputValue, out char[] outputValue, uint inputLength, out uint outputLength); 

我如何可以声明输出的char []应该作为一个byte []来处理,而不是空值终止的?


更多信息:

我有头文件。这是声明:

BOOL CompressString(char *DecompBuff, char **RetBuff, unsigned long DecompLen, unsigned long *RetCompLen); 
+0

你难道没有一个声明'CompressString'功能或至少一些文件头文件描述函数的参数?你怎么知道签名? – 2009-12-07 17:50:05

+0

你不应该在任何地方在你的C#代码中使用'char',而是'byte'或'sbyte'(这相当于C++'char')。 – 2009-12-07 18:24:13

回答

3

看看MSDN article在P/Invoke中传递数组。我认为你可能想用SizeParamIndex来告诉编组人员哪个参数保存了传递数组的大小。

编辑:SizeParamIndex不幸的是不允许在outref参数。你可以,但是,手动将它复制:

[DLLImport("foo.dll")] 
public static extern bool CompressString([MarshalAs(UnmanagedType.LPArray, SizeParamIndex=2)] char[] inputValue, out IntPtr outputValue, uint inputLength, out uint outputLength); 

public static bool CompressStringInvoke(char[] inputValue, out char[] outputValue, uint inputLength) { 
    IntPtr outputPtr; 
    uint outputLen; 
    if (CompressString(inputValue, out outputPtr, inputLength, out outputLen)) { 
     outputValue = new char[outputLen]; 
     Marshal.Copy(outputPtr, outputValue, 0, (int)outputLen); 
     return true; 
    } 
       outputValue = new char[0]; 
    return false; 
} 
+0

我得到这个错误:无法封送'参数#2':无法使用SizeParamIndex作为ByRef数组参数。 – 2009-12-07 17:59:57

+0

对。用另一种解决方案编辑。 – Lucero 2009-12-07 18:20:25

+0

辉煌!谢谢! (我修复了一些小的语法问题) – 2009-12-07 18:22:55

相关问题