c# - 为什么后续调用时不能使用IntPtr

标签 c# c pinvoke

我的程序:

class Program {
    [DllImport("libiconvD.dll", CallingConvention = CallingConvention.Cdecl)]
    public static extern IntPtr libiconv_open([MarshalAs(UnmanagedType.LPStr)]
                                          string tocode, 
                                          [MarshalAs(UnmanagedType.LPStr)] 
                                          string fromcode);

    [DllImport("libiconvD.dll", CallingConvention = CallingConvention.Cdecl)]
    static extern ulong libiconv(IntPtr icd,
                             ref StringBuilder inbuf, ref ulong inbytesleft,
                             out StringBuilder outbuf, out ulong outbytesleft);

    [DllImport("libiconvD.dll", CallingConvention = CallingConvention.Cdecl)]
    static extern int libiconv_close(IntPtr icd);

    static void Main(string[] args) {
        var inbuf = new StringBuilder("Rule(s): Global Tag – Refer to Print Rules – General Requirements");
        ulong inbytes = (ulong)inbuf.Length;
        ulong outbytes = inbytes;
        StringBuilder outbuf = new StringBuilder((int)outbytes);

        IntPtr icd = libiconv_open("utf8", "windows-1252");
        var rcode1 = libiconv(icd, ref inbuf, ref inbytes, out outbuf, out outbytes);
        Debug.WriteLine(rcode1);
        var rcode2 = libiconv_close(icd);
        Debug.WriteLine(rcode2);
    }//Main()
}//Program CLASS

第一次调用 libiconv_open() 有效并返回指向 icd 的指针。 当第二次调用 libiconv() 运行时,它会在 icd 指针上发生访问冲突。

这里是被调用的 C 代码:

size_t iconv (iconv_t icd,
              ICONV_CONST char* * inbuf, size_t *inbytesleft,
              char* * outbuf, size_t *outbytesleft)
{
  conv_t cd = (conv_t) icd;
  if (inbuf == NULL || *inbuf == NULL)
    return cd->lfuncs.loop_reset(icd,outbuf,outbytesleft);
  else
    return cd->lfuncs.loop_convert(icd,
                                   (const char* *)inbuf,inbytesleft,
                                   outbuf,outbytesleft);
}

enter image description here 似乎无法访问指针指向的结构中定义的函数。是否需要对返回的指针执行一些特殊操作才能在后续调用中使用。

谢谢

最佳答案

事实证明,C# 不需要使用 libiconv 库。只需使用 Encoding 类即可。

        static void Main(string[] args) {
            UTF8Encoding utf8 = new UTF8Encoding();
            Encoding w1252 = Encoding.GetEncoding(1252);

            string inbuf = "Rule(s): Global Tag – Refer to Print Rules – General Requirements";
            byte[] bytearray = utf8.GetBytes(inbuf);
            byte[] outbytes = Encoding.Convert(utf8, w1252, bytearray);

            Debug.WriteLine("*************************");
            Debug.WriteLine(String.Format("  Input: {0}", inbuf));
            Debug.WriteLine(String.Format(" Output: {0}", utf8.GetString(outbytes)));
            Debug.WriteLine("*************************");

        }//Main()

*************************
  Input: Rule(s): Global Tag – Refer to Print Rules – General Requirements
 Output: Rule(s): Global Tag – Refer to Print Rules – General Requirements
*************************

关于c# - 为什么后续调用时不能使用IntPtr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55910528/

相关文章:

c - Eclipse CDT插件问题

c - STM32 DMA 传输错误同时设置 FIFO 和传输错误标志

c# - 如何在 C# 中确定当前关注的进程名称和版本

c# - 回发后隐藏的表单字段未出现在 MVC 模型中

c# - 如何在 C# 中将类型为 "IBuffer"的值读取为字符串?

c# - 使用 xpath 获取定义类型的节点父节点

c# - 通用存储库问题

c - 将文件中的旧字符串修改为新字符串

c# - 如何在 C# 中创建这个结构?

C# pinvoke 具有 union 和数组的结构