如何编组二维TCHAR数组

How to marshal two dimensional TCHAR array?

本文关键字:二维 TCHAR 数组 何编组      更新时间:2023-10-16

我有一个将文件转换为另一种格式的DLL函数。该函数生成多个文件作为输出。因此,它用输出文件的路径填充第二个参数。

c++函数定义如下:

int Convert(LPTSTR lpSource, TCHAR outputFileName[][MAX_PATH]);

我如何mashal第二个参数,使我的c#应用程序可以正确接收输出文件路径?

[DllImport("Convert.dll")]
private static extern int Convert(
  [MarshalAs(UnmanagedType.LPTStr)] string lpszSource,
  ????
);

我会使用 c++/CLI(它非常擅长在本地C/c++代码和托管代码之间构建桥接层)来使事情变得更简单。

基本上,您可以编写一个瘦c++/CLI层,它暴露了一个方法,该方法在其主体中调用本机函数,然后将返回的本机字符串复制到gcnew -ly创建的 array<String^> 中,并将其返回给c#托管调用者。

我终于想通了。我把我的c++函数改成如下:

int Convert(LPTSTR lpSource, LPTSTR *plpOutputFileName, int size);

c#声明为:

[DllImport("Convert.dll")]
private static extern int Convert(
    [MarshalAs(UnmanagedType.LPTStr)] string lpszSource,
    [In, Out] String[] outputFileName,
    int size
);

谢谢大家的帮助。