首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从C#程序调用外部DLL函数(QPDF库)时的访问冲突

从C#程序调用外部DLL函数(QPDF库)时的访问冲突
EN

Stack Overflow用户
提问于 2022-01-20 05:26:57
回答 1查看 229关注 0票数 0

构建环境: Visual 2019,Windows应用程序,.net 4.7.2

我试图使用QPDF .dll库来读取、加密和输出PDF文件。有关DLL头文件,请参见这里,该文件将所有公开函数作为声明列出。

备注:

  • QPDF_BOOL是'int‘的类型 qpdf_r3_print_e是一个整数枚举,所以我认为它也是'int‘。

我可以成功地初始化QPDF库,读取该文件,并通过调用必要的函数将其输出到一个新文件,但是当我在初始化和成功读取文件之后添加对qpdf_set_r6_encryption_parameters2()函数的调用时,它会引发访问冲突。这可能是因为参数类型不正确,但我已经尽可能地阅读了QPDF代码来使用正确的类型。

下面是我的DllImport代码中的C#:(所有DllImport行都遵循这个建议 )

代码语言:javascript
复制
[DllImport("qpdf28.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        static extern void qpdf_set_r6_encryption_parameters2(
            IntPtr qpdf, 
            [MarshalAs(UnmanagedType.LPStr)] string user_password,
            [MarshalAs(UnmanagedType.LPStr)] string owner_password,
            int allow_accessibility,
            int allow_extract,
            int allow_assemble,
            int allow_annotate_and_form,
            int allow_form_filling,
            int allow_modify_other,
            int print,
            int encrypt_metadata

            );

下面是C#代码后面的函数调用:

代码语言:javascript
复制
qpdf_set_r6_encryption_parameters2(qpdf,
                    txtPassword.Text,
                    txtPassword.Text,
                    0, 0, 0, 0, 0, 0,
                     0,
                     0
                    );

我很确定参数数据类型在某个地方是错误的,但我没有看到。谢谢你的指点!

更新:这是我试图运行的完整代码,如果这有帮助的话。注意,如果我禁用加密行,代码将完美地工作:它写入一个新的PDF文件,其中包含与输入PDF相同的信息。

代码语言:javascript
复制
namespace testqpdf
{
    public partial class Form1 : Form
    {
        enum qpdf_r3_print_e
        {
            qpdf_r3p_full = 0,
            qpdf_r3p_low = 1,
            qpdf_r3p_none = 2
        }

        [DllImport("qpdf28.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        static extern IntPtr qpdf_init();

        [DllImport("qpdf28.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        static extern void qpdf_cleanup(ref IntPtr qpdfData);

        [DllImport("qpdf28.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        static extern int qpdf_read(IntPtr qpdfdata, [MarshalAs(UnmanagedType.LPStr)] string fileName, IntPtr password);

        [DllImport("qpdf28.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        static extern void qpdf_set_object_stream_mode(IntPtr qpdf, int mode);

        [DllImport("qpdf28.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        static extern void qpdf_set_qdf_mode(IntPtr qpdf, int value);

        [DllImport("qpdf28.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        static extern int qpdf_init_write(IntPtr qpdf, [MarshalAs(UnmanagedType.LPStr)] string fileName);

        [DllImport("qpdf28.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        static extern int qpdf_write(IntPtr qpdf);

        [DllImport("qpdf28.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        static extern void qpdf_set_r6_encryption_parameters2(
            IntPtr qpdf, 
            [MarshalAs(UnmanagedType.LPStr)] string user_password,
            [MarshalAs(UnmanagedType.LPStr)] string owner_password,
            int allow_accessibility,
            int allow_extract,
            int allow_assemble,
            int allow_annotate_and_form,
            int allow_form_filling,
            int allow_modify_other,
            int print,
            int encrypt_metadata

            );




        public Form1()
        {
            InitializeComponent();
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            IntPtr qpdf = qpdf_init();

            int result = qpdf_read(qpdf, txtInput.Text, /* password */ IntPtr.Zero);

            Console.WriteLine("Read PDF file " + txtInput.Text + ", result = " + result.ToString());

            if (chkEncrypt.Checked)
            {
                qpdf_set_r6_encryption_parameters2(qpdf,
                    null,
                    null,
                    0, 0, 0, 0, 0, 0,
                     0,
                     0

                    );
            }


            result = qpdf_init_write(qpdf, txtOutput.Text);

            Console.WriteLine("Write PDF file " + txtOutput.Text + ", result = " + result.ToString());

            result = qpdf_write(qpdf);

            qpdf_cleanup(ref qpdf);
        }
    }
}
EN

回答 1

Stack Overflow用户

发布于 2022-09-18 02:35:47

试试这个:

代码语言:javascript
复制
[DllImport("qpdf29.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern IntPtr qpdf_init();

[DllImport("qpdf29.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern void qpdf_cleanup(ref IntPtr qpdfData);

[DllImport("qpdf29.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern int qpdf_read(IntPtr qpdfdata, [MarshalAs(UnmanagedType.LPStr)] string fileName, IntPtr password);

[DllImport("qpdf29.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern void qpdf_set_qdf_mode(IntPtr qpdf, int value);

[DllImport("qpdf29.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern int qpdf_init_write(IntPtr qpdf, [MarshalAs(UnmanagedType.LPStr)] string fileName);

[DllImport("qpdf29.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
static extern int qpdf_write(IntPtr qpdf);


IntPtr pdfData = qpdf_init();
qpdf_read(pdfData, @"C:\source file.pdf", IntPtr.Zero);
qpdf_init_write(pdfData, @"C:destination file.pdf");
qpdf_set_qdf_mode(pdfData, 1);
qpdf_write(pdfData);
qpdf_cleanup(ref pdfData);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70781189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档