将标准c++字符串编组为c#字符串时出现的问题

Problem at marshalling a standard C++ string to a C# string

本文关键字:字符串 问题 标准 c++      更新时间:2023-10-16

我正在为管理OCR设备的DLL编写包装器。DLL有一个签名如下的方法:

unsigned long aMethod(char **firstParameter, char **secondParameter);

方法返回指向所有参数的字符串指针。

我在C#中写了这个签名…它几乎是功能性的:

[DllImport(aDll.dll, CallingConvention = CallingConvention.Cdecl, 
    CharSet = CharSet.Auto)]
static unsafe extern ulong aMethod(ref IntPtr firstParameter, 
    ref IntPtr secondParameter);

我这样做调用:

aMethod(ref firstParameter, ref secondParameter);

与字符串相关的编组和解组操作如下:

Marshal.PtrToStringAnsi(firstParameter)
Marshal.PtrToStringAnsi(secondParameter)

显然,这种编组是根据DLL的API约定选择的。

现在,编组过程有一个问题。假设设备有一个字符串"abcdefg"的输入。如果我从纯c++代码使用DLL,我得到"abcdefg"作为输出。但是,如果我使用我编写的c#签名,字符串将失去它的第一个字符,看起来像"bcdefg"。

出什么事了?如何修复c#方法?

尝试将CharSet更改为CharSet = CharSet.Ansi

假设这些参数是应用程序提供给您的,为什么不直接使用:

[DllImport(aDll.dll, CallingConvention = CallingConvention.Cdecl, 
    CharSet = CharSet.Auto)]
static unsafe extern uint aMethod(out string firstParameter, 
    out string secondParameter);

如果您希望它们双向运行,您可以使用预分配大小的ref StringBuilder(如果C函数希望您管理自己的内存—您没有说—您也应该使用它)。