【问题标题】:Printing on Windows isn't working在 Windows 上打印不起作用
【发布时间】:2018-06-26 06:24:36
【问题描述】:

我正在编写一个可直接在 Windows 上打印的 C++ 脚本。

目前我正在使用来自here的以下代码:

BOOL RawDataToPrinter(LPTSTR szPrinterName, LPBYTE lpData, DWORD dwCount)
{
    BOOL     bStatus = FALSE;
    HANDLE     hPrinter = NULL;
    DOC_INFO_1 DocInfo;
    DWORD      dwJob = 0L;
    DWORD      dwBytesWritten = 0L;

    bStatus = OpenPrinter(szPrinterName, &hPrinter, NULL);

    if (bStatus) {
        // Fill in the structure with info about this "document." 
        DocInfo.pDocName = (LPTSTR)_T("My Document");
        DocInfo.pOutputFile = NULL;
        DocInfo.pDatatype = (LPTSTR)_T("RAW");

        // Inform the spooler the document is beginning. 
        dwJob = StartDocPrinter(hPrinter, 1, (LPBYTE)&DocInfo);
        if (dwJob > 0) {
            // Start a page. 
            bStatus = StartPagePrinter(hPrinter);

            if (bStatus) {
                // Send the data to the printer. 
                bStatus = WritePrinter(hPrinter, lpData, dwCount, &dwBytesWritten);
                EndPagePrinter(hPrinter);
            }
            // Inform the spooler that the document is ending. 
            EndDocPrinter(hPrinter);
        }
        // Close the printer handle. 
        ClosePrinter(hPrinter);

        std::cout << GetLastError() << std::endl;
    }
    // Check to see if correct number of bytes were written. 
    if (!bStatus || (dwBytesWritten != dwCount)) {
        bStatus = FALSE;
    }
    else {
        bStatus = TRUE;
    }
    return bStatus;
}

我正在调用该方法:

std::string str = "Hello World";
BOOL blubb = RawDataToPrinter((LPTSTR)_T("PRINTER_NAME"), (LPBYTE) str.c_str(), str.size());

我遇到的问题是打印作业在我的打印机的打印队列中显示了几毫秒(刚好够看到),但他没有打印任何东西。

有人知道我做错了什么吗?

【问题讨论】:

  • sizeof(str)not 为您提供您所期望的(包含的字符串的长度)。为此使用str.size()
  • 我试过了,结果和之前一样
  • 为什么需要(LPTSTR)演员?只需_T("PRINTER_NAME") 就足够了。也不能选择使用CreateDC 进行直接打印吗?
  • 我使用了强制转换,因为示例代码提供了它。而且我不知道是否需要使用CreateDC
  • 我无法发现您的代码有错误。我的意思是,如果您只想绕过打印对话框,请使用HDC hdc = CreateDC(L"WINSPOOL", L"Printer Name", NULL, NULL); StartDoc, StartPage, TextOut(hdc,...)(这与 RAW 无关)

标签: c++ windows winapi printing


【解决方案1】:

在 2004 年,我遇到了很多从 C++ 程序打印文档的问题。我曾尝试使用 Windows MFC API,但效果不佳。所以我找到了另一个使用 Visual Studio 2017 在 2018 年继续工作的解决方案!

 XString sCmd;
 XString sDevice = "\\\\localhost\\DefaultPrinter";

 sCmd.Clear() << "net use LPT1: " << sDevice;
 iRetCode = system(sCmd);

 sCmd.Clear() << "print /D:LPT1 " << sFile;
 iRetCode = system(sCmd);

 sCmd = "net use LPT1: /delete";
 iRetCode = system(sCmd);

XString 类是 MFC CString 增强的克隆,因此代码可以在 Windows 和 BS2000 上运行(Siemens Operating System = Betrieb System in German)。

仅当您在必须运行 C++ EXE 的每台 PC 上定义名为“DefaultPrinter”的共享打印时,此代码才有效。

不使用 XString,在简化一些行的情况下,代码可以是:

std::string sDevice = "\\\\localhost\\DefaultPrinter";

system(string("net use LPT1: ") + sDevice);
system(string("print /D:LPT1 ") + sFileToPrint;
system("net use LPT1: /delete");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 2020-02-26
    • 2020-01-02
    相关资源
    最近更新 更多