简体   繁体   English

函数定义,从C到.NET

[英]Function definition, C to .NET

I have some C code which I have converted into a DLL. 我有一些C代码已转换为DLL。 I need to call the DLL from a C# program. 我需要从C#程序调用DLL。 I'm getting the error "Attempted to read or write protected memory. This is often an indication that other memory is corrupt." 我收到错误消息“尝试读取或写入受保护的内存。这通常表明其他内存已损坏。”

So, I believe that I haven't converted my function prototype right which is leading to memory leaks. 因此,我认为我没有正确转换函数原型,这会导致内存泄漏。

The function prototype in C is C中的函数原型是

int __stdcall karacell_bridge( int argumentCount, char **argContent)

Is this a correct conversion of the function prototype in C# DLL call? 这是C#DLL调用中函数原型的正确转换吗?

[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);

No. You'd need to marshal the second argument. 不。您需要整理第二个参数。 I'm not sure if there's a safe PInvoke way to marshal modifiable arrays of strings; 我不确定是否存在一种安全的PInvoke方法来封送可修改的字符串数组。 It seems like there would be many failure modes here. 似乎这里会有很多故障模式。 Perhaps you could try converting the array of strings to an array of char * . 也许您可以尝试将字符串数组转换为char *数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM