C#调用haskell遭遇Attempted to read or write protected memory

1. Haskell的代码如下:

haskell调用pandoc的代码

上面的代码中readMarkdown与writeHtmlString是pandoc中的函数,newString的作用是将String转换为IO CString。

2. C语言的代码如下:

调用pandoc的C语言代码

上面的代码是dll的调用入口。

3. ghc编译出来的头文件如下:

haskell ghc编译出来的头文件

4. C#调用代码如下:

class Native
{
    [DllImport("libpandoc", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    public static extern IntPtr markdownToHtml(byte[] markdown);
}

public class Processor 
{
    public string Process(string text)
    {
        var intPtr = Native.markdownToHtml(System.Text.Encoding.UTF8.GetBytes(text));
        var html =  Marshal.PtrToStringAnsi(intPtr);
        return html;
    } 
}

5. 运行C#代码之后,出现错误:

An unhandled exception of type 'System.AccessViolationException' occurred in Unknown Module.

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

  对应的中文错误信息:

尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

6. 而在ghci中执行直接执行haskell代码能得到正确的结果:

被这个“Attempted to read or write protected memory. ”问题困扰了很长时间,一直未找到解决方法。

【更新】

后来找到了解决方法,详见:困扰多日的C#调用Haskell问题竟然是Windows的一个坑

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值