我有一些 C 代码,我已将其转换为 DLL。我需要从 C# 程序调用 DLL。我收到错误“尝试读取或写入 protected 内存。这通常表明其他内存已损坏。”
所以,我相信我没有正确转换我的函数原型(prototype),这导致了内存泄漏。
C 中的函数原型(prototype)是
int __stdcall karacell_bridge( int argumentCount, char **argContent)
这是 C# DLL 调用中函数原型(prototype)的正确转换吗?
[DllImport("KaracellLib.dll", EntryPoint = "karacell_bridge@8", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true)]
public static extern int karacell_bridge(int argumentCount, [In, Out]string[] argContent);
最佳答案
没有。您需要整理第二个参数。我不确定是否有安全的 PInvoke 方法来编码可修改的字符串数组;看来这里会有很多故障模式。也许您可以尝试将字符串数组转换为 char *
数组。
关于c# - 函数定义,C 到 .NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14972084/