c# - 从 C# 调用 .DLL 的奇怪问题

标签 c# pinvoke htmltidy

我正在尝试从 C# 调用 HtmlTidy 库 dll。网上流传着一些例子,但没有确定的例子……而且我遇到了无穷无尽的麻烦。我很确定问题出在 p/invoke 声明上……但如果我知道我哪里出错了,那就危险了。

我从 http://www.paehl.com/open_source/?HTML_Tidy_for_Windows 得到了 libtidy.dll这似乎是当前版本。

这是一个演示我遇到的问题的控制台应用程序:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication5
{
    class Program
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct TidyBuffer
        {
            public IntPtr bp;         // Pointer to bytes
            public uint size;         // # bytes currently in use
            public uint allocated;    // # bytes allocated
            public uint next;         // Offset of current input position
        };

        [DllImport("libtidy.dll")]
        public static extern int tidyBufAlloc(ref TidyBuffer tidyBuffer, uint allocSize);


        static void Main(string[] args)
        {
            Console.WriteLine(CleanHtml("<html><body><p>Hello World!</p></body></html>"));
        }

        static string CleanHtml(string inputHtml)
        {
            byte[] inputArray = Encoding.UTF8.GetBytes(inputHtml);
            byte[] inputArray2 = Encoding.UTF8.GetBytes(inputHtml);

            TidyBuffer tidyBuffer2;
            tidyBuffer2.size = 0;
            tidyBuffer2.allocated = 0;
            tidyBuffer2.next = 0;
            tidyBuffer2.bp = IntPtr.Zero;

            //
            // tidyBufAlloc overwrites inputArray2... why? how? seems like
            // tidyBufAlloc is stomping on the stack a bit too much... but
            // how? I've tried changing the calling convention to cdecl and
            // stdcall but no change.
            //
            Console.WriteLine((inputArray2 == null ? "Array2 null" : "Array2 not null"));
            tidyBufAlloc(ref tidyBuffer2, 65535);
            Console.WriteLine((inputArray2 == null ? "Array2 null" : "Array2 not null"));
            return "did nothing";
        }
    }
}

总而言之,我有点难过。任何帮助将不胜感激!

最佳答案

您正在使用 TidyBuffer 结构的旧定义。新结构更大,因此当您调用分配方法时,它会覆盖 inputArray2 的堆栈位置。新的定义是:

    [StructLayout(LayoutKind.Sequential)]        
    public struct TidyBuffer        
    {
        public IntPtr allocator;  // Pointer to custom allocator            
        public IntPtr bp;         // Pointer to bytes            
        public uint size;         // # bytes currently in use            
        public uint allocated;    // # bytes allocated            
        public uint next;         // Offset of current input position        
    };        

关于c# - 从 C# 调用 .DLL 的奇怪问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/842612/

相关文章:

html - Notepad++ HTML 整洁

javascript - 通过javascript生成html : avoiding htmltidy error

c# - 将 html 字符串拆分为 N 部分

.net - 我是否需要 2 个不同的 PInvoke 来获取和设置鼠标速度?

c# - PDFLib dll 在 .net4.0 中不工作

c# - fatal error LNK1104 : cannot open file 'libboost_thread-vc90-mt-1_35.lib'

c# - C# 中的 PowerPoint 笔记

c# - free memory 不清除内存块

c# - 如何从 C 调用 C# 以传递具有数组作为成员的结构数组?

C# 按字母顺序排序 a - z,然后排序为 aa、ab - zz