我有一个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/