首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用ReadPrinter方法读取打印作业内容

如何使用ReadPrinter方法读取打印作业内容
EN

Stack Overflow用户
提问于 2017-10-05 12:58:04
回答 1查看 1.1K关注 0票数 0

我想把一份文件的内容寄给印刷。谷歌表示,唯一能做到这一点的方法是使用WinAPI方法ReadPrinter()。我已经完成了一个草图,但没能成功。一个问题是ReadPrinter()方法总是什么都不返回。

请告诉我出了什么问题。

简化代码如下:

代码语言:javascript
运行
复制
string printerName = "Microsoft XPS Document Writer";
const uint firstJob = 0u;
const uint noJobs = 10u;
const uint level = 1u;
uint bytesNeeded;
uint returned;
uint bytesCopied;
uint structsCopied;

// Open printer
IntPtr printerHandle = OpenPrinterW(printerName.Normalize(), out printerHandle, IntPtr.Zero);

// Get byte size required for a data
EnumJobsW(printerHandle, firstJob, noJobs, level, IntPtr.Zero, 0, out bytesNeeded, out returned);

// Now we know how much memory we need to read the data (bytesNeeded value)
IntPtr pJob = Marshal.AllocHGlobal((int)bytesNeeded);

// Read the data
EnumJobsW(printerHandle, firstJob, noJobs, level, pJob, bytesNeeded, out bytesCopied, out structsCopied);

// Convert pJob to jobInfos
JOB_INFO_1W[] jobInfos = null;

// ... actual convert code missed ...

//  Iterate through the jobs and try to get their content
foreach (JOB_INFO_1W jobInfo in jobInfos)
{
    // Open print job
    string printJobName = string.Format("{0}, Job {1}", printerName, jobInfo.JobId);
    IntPtr printJobHandle;

    OpenPrinterW(printJobName.Normalize(), out printJobHandle, IntPtr.Zero);

    // Read print job
    const int printJobBufLen = 1024;
    StringBuilder printJobSb = new StringBuilder(printJobBufLen);
    int printJobBytesRead = 0;

    while (printJobBytesRead == 0)
    {
        ReadPrinter(printJobHandle, printJobSb, printJobBufLen, out printJobBytesRead);

        // !!! printJobBytesRead is 0 and printJobSb is empty
    }

    // Close print job
    ClosePrinter(printJobHandle);
}

// Close printer
ClosePrinter(printerHandle);

P/援引签名:

代码语言:javascript
运行
复制
[DllImport("winspool.drv", EntryPoint = "OpenPrinterW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int OpenPrinterW(
    [In] string pPrinterName,
    [Out] out IntPtr phPrinter,
    [In] IntPtr pDefault);

[DllImport("spoolss.dll", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int ClosePrinter(
    [In] IntPtr hPrinter);

[DllImport("winspool.drv", EntryPoint = "EnumJobsW", SetLastError = true, CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int EnumJobsW(
    [In] IntPtr hPrinter,
    [In] uint FirstJob,
    [In] uint NoJobs,
    [In] uint Level,
    [Out] IntPtr pJob,
    [In] uint cbBuf,
    [Out] out uint pcbNeeded,
    [Out] out uint pcReturned);

[DllImport("spoolss.dll", EntryPoint = "ReadPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern int ReadPrinter(
    [In] IntPtr hPrinter,
    [Out] StringBuilder data,
    [In] Int32 cbBuf,
    [Out] out Int32 pNoBytesRead);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-07 07:42:38

这是驱动程序打印处理器组件中的代码吗?如果没有,我不认为它会起作用。

所以要么使用打印驱动程序组件,要么从磁盘上的假脱机文件中读取。看这里。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46586214

复制
相关文章

相似问题

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