c# - 使用 char** 参数的 PInvoke 中出现问题,字符串数组不起作用

标签 c# .net .net-4.0 pinvoke

我有一个DLL文件写入 C 。我尝试在托管代码中使用它,但有些功能无法正常工作。这是C代码。

int preProcessImagesC (char *p_trainingFilePath,
                       char **p_vecImageFilesOrDirs);  

该功能运行良好。

托管代码:

unsafe private static extern int preProcessImagesC(
    //Works perfact 
    String p_trainingFilePath,
    //char** thise parameter is taking junk values , String Array is not working
    [MarshalAs(UnmanagedType.SafeArray)] ref String[] p_vecImageFilesOrDirs);

只有第一个参数工作正常。我应该用什么 char **p_vecImageFilesOrDirs托管代码中的参数。请帮我在C#中编写兼容的代码.

最佳答案

你尝试过吗:

private static extern int preProcessImagesC(
    string p_trainingFilePath,
    string[] p_vecImageFilesOrDirs
);

编码器自动使用:

[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPTStr)]

这就是你所需要的。

您应该小心这一点,因为您的非托管代码无法确定传递数组的实际大小。您必须将数组的实际大小作为另一个参数传递给非托管函数,或者在两个地方都使用固定大小。

关于c# - 使用 char** 参数的 PInvoke 中出现问题,字符串数组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15920370/

相关文章:

c# - 使用命令行解析器库的列表/数组的默认值

c# - 调用 TLS 1.0 和 TLS 1.2 Web 服务的 ASP.NET 应用程序

c# - 相对 SourceURI 不起作用

c# - 在 .net 4 上使用异步等待

c# - 使用 .NET 3.5 的 SFTP

c# - 使用 HAP (HTML Agility Pack) 从页面获取数据

c# - 关于C#中引用字符串的问题

c# - 方法调用超时

c# - 使用Datarow读取LINQ查询的结果

c# - “无法访问已处置的上下文实例。”从存储库调用方法时出错