Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将QPDF与C结合使用#_C#_C++_Pinvoke_Qpdf - Fatal编程技术网

C# 将QPDF与C结合使用#

C# 将QPDF与C结合使用#,c#,c++,pinvoke,qpdf,C#,C++,Pinvoke,Qpdf,我正在尝试转换此qpdf命令: qpdf--qdf--object streams=禁用输入.pdf可编辑.pdf 使用qpdf dll(可从此处获得:)时需要的等效方法调用 我通过DIPBIN运行QPDF DLL以获得函数名,并且通过查看包含在C++项目中的头文件,我可以看到函数的参数。 例如,传递上述--object streams选项所需的函数(据我所知)是以下函数: void setObjectStreamMode(qpdf_object_stream_e); 从C++头文件中变为:

我正在尝试转换此qpdf命令:

qpdf--qdf--object streams=禁用输入.pdf可编辑.pdf

使用qpdf dll(可从此处获得:)时需要的等效方法调用

我通过DIPBIN运行QPDF DLL以获得函数名,并且通过查看包含在C++项目中的头文件,我可以看到函数的参数。 例如,传递上述--object streams选项所需的函数(据我所知)是以下函数:

void setObjectStreamMode(qpdf_object_stream_e);
<>从C++头文件中变为:

[DllImport("qpdf21.dll")]
static extern void _ZN10QPDFWriter19setObjectStreamModeE20qpdf_object_stream_e(int stateEnum);
在C#文件中

问题是当我使用上面的函数时,我得到一个

AccessViolationException:尝试读取或写入受保护的内存

错误,这使我认为我需要以某种方式创建QPDF对象,但我从未使用面向对象的pinvokes,因此我不知道如何在c#中访问该对象


如果有人已经熟悉了在C语言中使用DLL,或者甚至在C++中,并且可以告诉我正确的函数来调用命令,我会很感激的!p> 我已经设法弄明白了,下面的代码复制了该命令,结果我查错了头文件:

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

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

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

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

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

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

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

static void Main(string[] args)
{
    //call init
    IntPtr qpdfData = qpdf_init();

    //call read (which gets and processes the input file)
    //0 result == good
    int result = qpdf_read(qpdfData, @"scan.pdf", IntPtr.Zero);

    //call init_write
    result = qpdf_init_write(qpdfData, @"scanEditable.pdf");

    //set write options
    //disable object stream mode
    qpdf_set_qdf_mode(qpdfData, 1);
    qpdf_set_object_stream_mode(qpdfData, 0);

    //call write
    result = qpdf_write(qpdfData);

    //call cleanup
    qpdf_cleanup(ref qpdfData);
}

看来你找到了一个好答案。您已经发现了C API,它旨在帮助通过DLL从C++以外的语言使用QPDF。C API主要记录在中。您也可以在中找到一些信息。C API不公开QPDF的C++库的全部功能。如果您发现丢失的部分,请随时在上创建一个问题。我尝试在添加新接口时更新C API,但并不是对每个接口都这样做,CLI中的一些功能直接在工具中实现,并且不会映射到单个库函数


如果C API对用例不足够丰富,也可以编写自己的C++类,其中声明的函数为代码>外“C”<代码>,导出它们,并构建一个额外的DLL,可以用上面找到的方式使用。您可以将其作为一个示例来了解其工作原理。

解决内存问题后,我将删除编辑并回答问题。发现问题后,我的pinvoke清理错误。